Function Signature in Perl
Last Updated :
11 Jul, 2025
A
Perl function or
subroutine is a group of statements that together perform a specific task. In every programming language user want to reuse the code. So the user puts the section of code in function or subroutine so that there will be no need to write code again and again. In Perl, the terms function, subroutine, and method are the same but in some programming languages, these are considered different. The word subroutines is used most in Perl programming because it is created using keyword sub. Whenever there is a call to the function, Perl stops executing all its program and jumps to the function to execute it and then returns back to the section of code that it was running earlier. One can avoid using the return statement.
Defining Subroutines: The general form of defining the subroutine in Perl is as follows-
sub subroutine_name
{
# body of method or subroutine
}
Function Signature: When a Function is defined, a set of parameters is also defined within the parentheses to define the type of arguments it will be receiving on the function call. This function signature may hold one parameter or a list of parameters. A subroutine or Function whose signature is defined can receive arguments only of the type defined in the subroutine. It will generate an error if the arguments passed to the subroutine varies from its signature.
A function signature tells a lot about the type of subroutine. It enables the user to make different subroutines of the same name but with different signature i.e. with different parameters. In Perl, subroutine name can be the same for different subroutines but their parameters must be different.
Example:
sub example_func($variable)
{
statement;
}
sub example_func($variable1, $variable2)
{
statement;
}
In the above example, the name of the subroutines is the same but their argument count is different and hence, they will not be considered as different subroutines and your Perl code will not generate any error.
Passing arguments of a different type than the signature: When a function is defined with a signature then that subroutine is bound to accept the arguments of the same type as of its signature. If an argument other than the function signature is passed then it will generate an error, which results in the compilation failure of the code.
Example:
Perl
#!/usr/bin/perl
# Defining Function Signature
sub example(Int $variable)
{
return $variable / 2;
}
# Function Call
print example(44);
Output:
22
If we pass an argument of the type other than the function signature then it will generate an error as shown below:
Perl
#!/usr/bin/perl
# Defining Function Signature
sub example(Int $variable)
{
return $variable / 2;
}
# Function Call with
# string type parameter
print example("44");
Error while compiling prog.pl
Calling example(Str) will never work with declared signature (Int $variable)
at prog.pl:7
Difference in Number of arguments:
When a Function signature is defined, it also holds the number of arguments that can be passed to it, along with the type of arguments. If we call the function with a different number of arguments, then it will result in an error as Perl holds a different meaning for functions with different signatures.
Example:
Perl
#!/usr/bin/perl
# Defining Function Signature
sub example(Int $variable)
{
return $variable / 2;
}
# Function Call with
# two arguments
print example(44, 29);
Output:
Error while compiling prog.pl
Calling example(Int, Int) will never work with declared signature (Int $variable)
at prog.pl:10
Function Signature is a useful aspect but it sometimes becomes very annoying for some programmers because defining a Function Signature stricts the use of the Function to a specific type of parameters. If the Function is defined without a Signature then there is no restriction on the type of parameters that can be passed to it.
Example:
Perl
#!/usr/bin/perl
# Defining Function Signature
sub example($variable)
{
return $variable / 2;
}
# Function Call with Integer argument
# written as a string type
print example("44");
Output:
22
In the above code, a function is declared with no such specific argument type and hence when an integer value is passed to it as a string then it automatically converts the argument into the integer form and gives the result. But, if we call the function with a string argument and the operation to be performed on it requires an integer value then it will result in an error as shown below:
Perl
#!/usr/bin/perl
# Defining Function Signature
sub example($variable)
{
return $variable / 2;
}
# Function Call with
# string type argument
print example("Geeks");
Output:
Cannot convert string to number: base-10 number must begin with valid digits or '.'
in sub example at prog.pl line 4
in block at prog.pl line 11
Actually thrown at:
in sub example at prog.pl line 4
in block at prog.pl line 11
Similar Reads
Perl | sin() Function This function is used to calculate sine of a VALUE or $_ if VALUE is omitted. This function always returns a floating point. Syntax: sin(VALUE) Parameters: VALUE in the form of float Returns: Function returns sine of VALUE. Example 1: Perl #!/usr/bin/perl # Calling sin() function $var = sin(5); # Pr
1 min read
Perl | sprintf() Function sprintf() function in Perl uses Format provided by the user to return the formatted string with the use of the values in the list. This function is identical to printf but it returns the formatted string instead of printing it. Syntax: sprintf Format, List Returns: a formatted scalar string Example
1 min read
Perl | split() Function split() is a string function in Perl which is used to split or you can say to cut a string into smaller sections or pieces. There are different criteria to split a string, like on a single character, a regular expression(pattern), a group of characters or on undefined value etc.. The best thing abou
8 min read
Perl | values() Function values() Function in Perl returns the list of all the values stored in a Hash. In a scalar context it returns the number of elements stored in the Hash. Note: Values returned from the value() Function may not always be in the same order. Syntax: values Hash Returns: list of values in the list contex
2 min read
Perl | return() Function return() function in Perl returns Value at the end of a subroutine, block, or do function. Returned value might be scalar, array, or a hash according to the selected context. Syntax: return Value Returns: a List in Scalar Context Note: If no value is passed to the return function then it returns an
2 min read
Perl | rindex() Function rindex() function in Perl operates similar to index() function, except it returns the position of the last occurrence of the substring (or pattern) in the string (or text). If the position is specified, returns the last occurrence at or before that position. Syntax: # Searches pat in text from given
2 min read