Open In App

Perl – Difference between Functions and Subroutines

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.



What is a Function?

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.



What is a Subroutine?

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:

In Perl, there are two cases when a piece of code is put into the subroutine:

Below is a table of differences between a Function and a Subroutine:

FUNCTION 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 function in Perl means something built into Perl. The main reference documentation for Perl built-ins is called perlfunc. A subroutine in Perl is a section of code that can take arguments, perform some operations with them, and may return a meaningful value, but don’t have to. However, they’re always user defined rather than built-ins.
Function are provided to us by Perl. Subroutines are chunks of code that we provide to Perl.
Functions are similar to subroutines, except that they return a value. 
A function commonly carries out some calculations and reports the result to the caller.
 Subroutines perform a task but do not report anything to the calling program.
A function cannot change the value of the actual arguments . A subroutine can change the value of the actual argument.
A function has deterministic output and no side effects.
 
A subroutine doesn’t have any such restrictions.

A Sample program for reversing a string using a Function and a 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 function
print scalar reverse("$string"), "\n";




#!/usr/bin/perl
# Perl program to reverse a string
# using a subroutine
  
# Creating a string
my $string = 'GeeksforGeeks';
  
print 'Original string: ', $string, "\n";
print 'Reversed using Subroutine: '
    reverse_in_place($string), "\n";
  
# Creating a subroutine
sub 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);
}

Output:
Original string: GeeksforGeeks
Reversed using Subroutine:  skeeGrofskeeG

Article Tags :