![]() |
VOOZH | about |
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:
In Perl, there are two cases when a piece of code is put into the 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. |
Original string: GeeksforGeeks Reversed using Subroutine: skeeGrofskeeG