Welcome to the spoken tutorial on Functions in Perl. In this tutorial, we will learn about – Perl functions functions with arguments function with return values For this tutorial, I am using Ubuntu Linux12.04 operating system Perl 5.14.2 and gedit Text Editor You can use any text editor of your choice. You should have basic knowledge of variables, comments, loops, conditional statements and Data Structures in Perl. Please go through the relevant spoken tutorials on the spoken tutorial website. We will first see some simple Perl functions. In Perl, functions, also called as subroutines, are declared with sub keyword. The definition of a declared function is written between curly braces. This function does not take any arguments. And, it does not return anything. Note: function definition can be written anywhere in the script or in another module. This module, must then be included in the script, to use this function. To include the module file in the script, one has to use the following syntax- use ModuleFileName semicolon Let us understand this using a sample program. Open a file in your text editor and name it as simpleFunction dot pl Here is my simpleFunction dot pl file in gedit. Type the code as displayed on the screen. Here, we are just calling a function, which we have defined. Then, the execution control is passed to that function. This is the declaration & definition of the function. This function will print out the given text. Save your file. Then switch to the terminal and execute the Perl script by typing perl simpleFunction dot pl and press Enter. The output will be as shown on the terminal Now, let us see a function with arguments. Let us understand this function using a sample program. Open a file in your text editor and name it as functionWithArgs dot pl Here is my functionWithArgs script in gedit. Type the following piece of code as shown on the screen. Here, we are calling a function with arguments, 10 and 20. The passed arguments are caught in $var1 & $var2. @_ is a special Perl variable. We will cover its details in future tutorials. This function performs the addition of 2 variables and prints the answer. Save your file. @_ is a special Perl array. This array is used to store the passed arguments. Similarly, we can catch the passed arguments in variables as $var1 space=space shift @_ semicolon $var2 space =space shift @_ semicolon shift @_ removes the element at first position from @_ array and assigns it to a variable Another way is; $var1 space = space dollar underscrore open square bracket zero close square bracket semicolon $var2 space = space dollar underscrore open square bracket 1 close square bracket semicolon The above mentioned way is similar to fetching elements of @_ array using index. Now, switch to terminal and execute the script by typing – perl functionWithArgs dot pl and press Enter The output is as displayed on the screen Now, let us look at a functions which returns a single value. Let us understand the same using a sample program. Let me switch to funcWithSingleRtrnVal dot pl script in gedit. Open a file in your text editor and type the following piece of code as shown. Here, we are calling addVariables function with parameters 10 and 20. The return value of the function is caught in $addition variable. This function does the addition of the passed parameters and returns the answer. Save the file. Now let us execute the script. So, switch to terminal and type- perl funcWithSingleRtrnVal dot pl and press Enter. The output is as displayed on the terminal. Now, let us see a function which returns multiple values. Let us understand the same, using a sample program. In gedit, I have opened a file and named it as funcWithMultipleRtrnVals dot pl Please do like wise in your text editor Now, type the following piece of code as shown. Here, we are calling addVariables function with parameters 10 and 20. The return values of the function are caught in variables $var1, $var2 and $addition This function does the addition and returns the passed parameters and the resultant answer. This illustration demonstrates how we can return an array from a function. Similarly, this demonstrates how hash can be returned from a function. Save your file. Now let us execute the Perl script on the terminal by typing – perl funcWithMultipleRtrnVals dot pl and press Enter. The output is as displayed on the terminal. Perl provides several inbuilt functions. We learnt some of them in earlier tutorials. For eg- Arrays, Hash, sort, scalar, each, keys etc. Calling inbuilt functions, similar to calling any other function, which we define. E.g sort open bracket @arrayName close bracket semicolon Try incorporating some inbuilt functions in the sample programs we used. And observe their outputs. Let us summarize. In this tutorial, we have learnt – Functions in Perl functions with arguments and functions which return values using sample programs. Here is assignment for you – Write a function which takes 3 arguments. Perform some action on these arguments Return the result of the action performed on the arguments and print the same. Watch the video available at the following link It summaries the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it The Spoken Tutorial Project Team Conducts workshops using spoken tutorials Gives certificates to those who pass an online test For more details, please write to contact at spoken hyphen tutorial dot org Spoken Tutorial Project is a part of the Talk to a Teacher project It is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this Mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro Hope you enjoyed this Perl tutorial. This is Amol signing off. Thanks for joining.