Perl – Difference between Functions and Subroutines

28 Jul, 2020
In Perl, when a code-script gets bigger in size i.e. contains hundreds of lines of code, it becomes difficult to manage such a code-script. To avoid this difficulty, Perl provides its users with the concept of Functions and subroutines. Functions and subroutines break up complex/large amounts of code into smaller more concise parts to make the program more readable.
They reduce the size of the application and debugging time by allowing us to reuse the code that has been written in the program earlier. Perl functions and subroutines are used to reuse code in a program. We can use a function at several places in our application with different parameters.
A function is something which takes up a number of arguments, does something with them and then returns a value. It can either be built into the programming language or it can be provided by the user.
In Perl, when we define a function, we specify the name and the sequence of statements. Later, when we want to perform a computation, we can ‘call’ the function by name which will run the sequence of statements contained in the function definition.There are many built-in functions in Perl which are quite handy too. For example, ‘say’ is a built-in function. Perl even allows us to build up our own function which can help us to perform the task that we want to do.
Subroutines (or sub) give us the ability to give a name to the section of the code so that when we want to use it again in the program, we can use it by just calling its name.
Subroutines help us do programming in Perl in two major ways:
First, they let us reuse the code again in the program which makes it easier to find and fix bugs, making it faster to write programs.
Second, they allow us to chunk our code into organizational sections. Each subroutine is responsible for a specific task.
In Perl, there are two cases when a piece of code is put into the subroutine:
When we know that the code would be used for calculation or action that’s going to happen more than once. For example, while putting a strong into a specific format or while turning an incoming data record into a hash, etc.
When there are logical units of our program that we want to break into pieces making our program easier to understand.
Below is a table of differences between a Function and a Subroutine:
The basic format of a function is :
$myvalue = myfunction(parameter, parameter);
The basic format of subroutine is :
sub subroutine_name
{
   # body of subroutine
}
A Sample program for reversing a string using a Function and a Subroutine:
Function
Subroutine
#!/usr/bin/perl# Perl program to reverse a string# using pre-defined function  # Creating a string$string = "GeeksForGeeks"; print "Original string: $string", "\n";print "Reversed string: ";   # Calling pre-defined functionprint scalar reverse("$string"), "\n";
#!/usr/bin/perl# Perl program to reverse a string# using a subroutine  # Creating a stringmy $string = 'GeeksforGeeks';  print 'Original string: ', $string, "\n";print 'Reversed using Subroutine: ',     reverse_in_place($string), "\n";  # Creating a subroutinesub reverse_in_place{    my ($string) = @_;    my @array = split //, $string;    my $n = scalar @array;      for (0 .. $n / 2 - 1)    {        my $tmp    = $array[$_];        $array[$_] = $array[$n - $_ - 1];        $array[$n - $_ - 1] = $tmp;    }    return join('', @array);}
Original string: GeeksforGeeks
Reversed using Subroutine:  skeeGrofskeeG

perl-basics
Perl-function
Picked
Perl
Perl
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.