Difference between revisions of "PERL/C2/Functions-in-Perl/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
Line 69: | Line 69: | ||
|- | |- | ||
|01:07 | |01:07 | ||
− | | And, it does not | + | | And, it does not return anything. |
|- | |- |
Revision as of 10:29, 13 November 2015
Time | Narration |
00:01 | Welcome to the spoken tutorial on Functions in PERL. |
00:06 | In this tutorial, we will learn about: |
00:10 | * PERL functions |
00:11 | * functions with arguments |
00:13 | * functions with return values. |
00:16 | For this tutorial, I am using: |
00:18 | * Ubuntu Linux 12.04 operating system |
00:22 | * Perl 5.14.2 and |
00:24 | * gedit Text Editor. |
00:27 | You can use any text editor of your choice. |
00:31 | You should have basic knowledge of variables, comments, loops, conditional statements and data Structures in PERL. |
00:41 | Please go through the relevant spoken tutorials on the Spoken Tutorial website. |
00:47 | We will first see some simple PERL functions. |
00:51 | In PERL, functions, also called as subroutines, are declared with sub keyword. |
00:57 | The definition of a declared function is written between curly braces. |
01:03 | This function does not take any arguments. |
01:07 | And, it does not return anything. |
01:10 | Note: function definition can be written anywhere in the script or in another module. |
01:17 | This module must then be included in the script, to use this function. |
01:24 | To include the module file in the script, one has to use the following syntax: |
01:31 | use ModuleFileName semicolon |
01:35 | Let us understand this using a sample program. |
01:39 | Open a file in your text editor and name it as simpleFunction dot pl. |
01:46 | Here is my simpleFunction dot pl file in gedit. |
01:51 | Type the code as displayed on the screen. |
01:55 | Here, we are just calling a function which we have defined. |
02:00 | Then the execution control is passed to that function. |
02:06 | This is the declaration & definition of the function. |
02:10 | This function will print out the given text. |
02:14 | Save your file. |
02:17 | Then switch to the terminal and execute the PERL script by typing: |
02:24 | perl simpleFunction dot pl |
02:28 | and press Enter. |
02:30 | The output will be as shown on the terminal. |
02:38 | Now, let us see a function with arguments. |
02:44 | Let us understand this function using a sample program. |
02:48 | Open a file in your text editor and name it as functionWithArgs dot pl. |
02:57 | Here is my functionWithArgs script in gedit. |
03:02 | Type the following piece of code as shown on the screen. |
03:07 | Here, we are calling the function with arguments 10 and 20. |
03:13 | The passed arguments are caught in '$var1' & '$var2'. |
03:20 | @_ is a special Perl variable. We will cover its details in future tutorials. |
03:29 | This function performs the addition of 2 variables and prints the answer. |
03:37 | Save your file. |
03:42 | @_is a special Perl array. |
03:46 | This array is used to store the passed arguments. |
03:51 | Similarly, we can catch the passed arguments in variables as: |
03:56 | $var1 space = space shift @_ semicolon |
04:04 | $var2 space = space shift @_ semicolon |
04:12 | shift @_ removes the element at first position from @_ array |
04:21 | and assigns it to a variable. |
04:24 | Another way is: $var1 space = space dollar underscore open square bracket zero close square bracket semicolon. |
04:38 | $var2 space = space dollar underscore open square bracket 1 close square bracket semicolon. |
04:49 | The above mentioned way is similar to the fetching elements of @_ array using index. |
04:59 | Now, switch to terminal and execute the script by typing: |
05:06 | perl functionWithArgs dot pl and press Enter . |
05:14 | The output is as displayed on the screen. |
05:23 | Now, let us look at a function which returns a single value. |
05:32 | Let us understand the same, using a sample program. |
05:35 | Let me switch to funcWithSingleRtrnVal dot pl script in gedit. |
05:46 | Open a file in your text editor and type the following piece of code as shown. |
05:52 | Here, we are calling addVariables function with parameters 10 and 20. |
06:01 | The return value of the function is caught in '$addition' variable. |
06:09 | This function does the addition of the passed parameters and returns the answer. |
06:15 | Save the file. |
06:17 | Now, let us execute the script. |
06:20 | So, switch to terminal and type: |
06:24 | perl funcWithSingleRtrnVal dot pl and press Enter. |
06:35 | The output is as displayed on the terminal. |
06:43 | Now, let us see a function which returns multiple values. |
06:48 | Let us understand the same, using a sample program. |
06:53 | In gedit, I have opened a file and named it as funcWithMultipleRtrnVals dot pl. |
07:04 | Please do like-wise in your text editor. |
07:08 | Now, type the following piece of code as shown. |
07:13 | Here, we are calling addVariables function with parameters 10 and 20. |
07:21 | The return values of the function are caught in variables $var1, $var2 and $addition. |
07:31 | This function does the addition and returns the passed parameters and the resultant answer. |
07:42 | This illustration demonstrates how we can return an array from a function. |
07:53 | Similarly, this demonstrates how hash can be returned from a function. |
08:00 | Save your file. |
08:03 | Now let us execute the Perl script on the terminal by typing: |
08:10 | perl funcWithMultipleRtrnVals dot pl |
08:18 | and press Enter. |
08:20 | The output is as displayed on the terminal. |
08:32 | Perl provides several inbuilt functions. |
08:36 | We learnt some of them in earlier tutorials. For e.g- arrays, Hash, sort, scalar, each, keys etc. |
08:49 | Calling inbuilt functions is similar to calling any other function which we define. |
08:57 | e.g sort open bracket @arrayName close bracket semicolon. |
09:04 | Try incorporating some inbuilt functions in the sample programs we used. |
09:10 | And observe their outputs. |
09:13 | Let us summarize. |
09:15 | In this tutorial, we have learnt: |
09:17 | * functions in Perl |
09:19 | * functions with arguments and |
09:22 | * functions which return values using sample programs. |
09:27 | Here is an assignment for you: |
09:29 | Write a function which takes 3 arguments. |
09:33 | Perform some action on these arguments. |
09:37 | Return the result of the action performed on the arguments and print the same. |
09:43 | Watch the video available at the following link. |
09:47 | It summarizes the Spoken Tutorial project. |
09:51 | If you do not have good bandwidth, you can download and watch it. |
09:56 | The Spoken Tutorial project team: * Conducts workshops using spoken tutorials. |
10:02 | * Gives certificates to those who pass an online test. |
10:07 | For more details, please write to:
contact at spoken hyphen tutorial dot org. |
10:14 | "Spoken Tutorial" project is a part of the "Talk to a Teacher" project. |
10:19 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
10:28 | More information on this mission is available at: spoken hyphen tutorial dot org slash NMEICT hyphen Intro. |
10:40 | Hope you enjoyed this Perl tutorial. |
10:43 | This is Amol, signing off. |
10:46 | Thanks for joining. |