Function Signature in Perl

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Defining Function Signature
sub example(Int $variable)
{
    return $variable / 2;
}
  
# Function Call
print example(44);

chevron_right


Output:

22

If we pass an argument of the type other than the function signature then it will generate an error as shown below:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Defining Function Signature
sub example(Int $variable)
{
    return $variable / 2;
}
  
# Function Call with 
# string type parameter
print example("44");

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Defining Function Signature
sub example(Int $variable)
{
    return $variable / 2;
}
  
# Function Call with 
# two arguments
print example(44, 29);

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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");

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Defining Function Signature
sub example($variable)
{
    return $variable / 2;
}
  
# Function Call with 
# string type argument
print example("Geeks");

chevron_right


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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.