Difference between revisions of "PERL/C2/Functions-in-Perl/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 13: Line 13:
 
|-
 
|-
 
|00:10
 
|00:10
|* '''PERL functions'''  
+
|* PERL '''functions'''  
 
   
 
   
 
|-
 
|-
Line 93: Line 93:
 
|-
 
|-
 
|01:39
 
|01:39
|Open a file in your text editor and name it as ''' simpleFunction dot pl'''
+
|Open a file in your text editor and name it as '''simpleFunction dot pl'''
 
   
 
   
 
|-
 
|-
Line 105: Line 105:
 
|-
 
|-
 
|01:55
 
|01:55
|Here, we are just calling a ''' function''', which we have defined.  
+
|Here, we are just calling a function which we have defined.  
 
    
 
    
 
|-
 
|-
 
|02:00
 
|02:00
|Then, the execution control is passed to that ''' function.'''
+
|Then the execution control is passed to that function.
 
   
 
   
 
|-
 
|-
 
|02:06
 
|02:06
|This is the declaration & definition of the ''' function.'''  
+
|This is the declaration & definition of the '''function.'''  
 
   
 
   
 
|-
 
|-
 
|02:10
 
|02:10
|This ''' function''' will print out the given text.  
+
|This function will print out the given text.  
  
 
|-
 
|-
Line 125: Line 125:
 
|-
 
|-
 
|02:17
 
|02:17
|Then switch to the terminal and execute the ''' Perl script''' by typing  
+
|Then switch to the terminal and execute the PERL '''script''' by typing  
  
 
|-
 
|-
 
|02:24
 
|02:24
|''' perl simpleFunction dot pl'''
+
|'''perl simpleFunction dot pl'''
  
 
|-
 
|-
Line 137: Line 137:
 
|-
 
|-
 
|02:30
 
|02:30
|The output will be as shown on the terminal
+
|The output will be as shown on the terminal.
  
 
|-
 
|-
 
|02:38
 
|02:38
|Now, let us see a ''' function''' with '''arguments.'''
+
|Now, let us see a function with arguments.
 
   
 
   
 
|-
 
|-
Line 149: Line 149:
 
|-
 
|-
 
|  02:48
 
|  02:48
| Open a file in your text editor and name it as '''functionWithArgs dot pl'''
+
| Open a file in your text editor and name it as '''functionWithArgs dot pl'''.
 
   
 
   
 
|-
 
|-
 
|  02:57
 
|  02:57
|Here is my ''' functionWithArgs''' script in '''gedit. '''
+
|Here is my '''functionWithArgs''' script in '''gedit. '''
  
 
|-
 
|-
Line 161: Line 161:
 
|-
 
|-
 
|03:07
 
|03:07
|Here, we are calling a function with ''' arguments, 10'''  and '''20'''.  
+
|Here, we are calling a function with arguments, 10 and 20.  
 
   
 
   
 
|-
 
|-
 
| 03:13
 
| 03:13
| The passed '''arguments''' are caught in ''' $var1''' & ''' $var2.'''
+
| The passed arguments are caught in '$var1' & '$var2'.  
 
   
 
   
 
|-
 
|-
 
| 03:20
 
| 03:20
|@_ is a special '''Perl variable.''' We will cover its details in future tutorials.  
+
|'''@_''' is a special PERL variable. We will cover its details in future tutorials.  
  
 
|-
 
|-
 
|03:29
 
|03:29
|This '''function''' performs the addition of 2 '''variables''' and prints the answer.  
+
|This function performs the addition of 2 variables and prints the answer.  
 
   
 
   
 
|-
 
|-
Line 181: Line 181:
 
|-
 
|-
 
|  03:42
 
|  03:42
| ''' @_ '''is a special Perl array.  
+
| '''@_'''is a special PERL array.  
 
   
 
   
 
|-
 
|-
 
|03:46
 
|03:46
|This ''' array''' is used to store the passed '''arguments'''.   
+
|This array is used to store the passed arguments.   
 
   
 
   
 
|-
 
|-
 
|  03:51
 
|  03:51
| Similarly, we can catch the passed ''' arguments''' in ''' variables''' as  
+
| Similarly, we can catch the passed arguments in variables as:
  
 
|-
 
|-
 
|  03:56
 
|  03:56
|  ''' $var1 space = space  shift @_ semicolon'''
+
|  '''$var1 space = space  shift @_''' semicolon
 
   
 
   
 
|-
 
|-
 
|  04:04
 
|  04:04
|''' $var2 space  = space  shift @_ semicolon'''
+
|'''$var2 space  = space  shift @_''' semicolon
  
 
|-
 
|-
 
| 04:12
 
| 04:12
| '''shift @_ removes the element at first position from @_ array '''
+
| '''shift @_''' removes the element at first position from '''@_ array '''
  
 
|-
 
|-
 
| 04:21
 
| 04:21
| and assigns it to a ''' variable '''
+
| and assigns it to a variable.
  
 
|-
 
|-
 
|04:24
 
|04:24
|Another way is; ''' $var1 space  =  space dollar underscrore open square bracket zero close square bracket semicolon '''
+
|Another way is: '''$var1 space  =  space dollar underscore open square bracket zero close square bracket''' semicolon
  
 
|-
 
|-
 
|04:38
 
|04:38
| ''' $var2 space  =  space dollar underscrore open square bracket 1 close square bracket semicolon'''
+
| ''' $var2 space  =  space dollar underscore open square bracket 1 close square bracket''' semicolon
 
   
 
   
 
|-
 
|-
 
|04:49
 
|04:49
|The above mentioned way is similar to fetching ''' elements''' of '''@_ array''' using '''index.'''
+
|The above mentioned way is similar to fetching '''elements''' of '''@_ array''' using '''index.'''
  
 
|-
 
|-
 
|04:59
 
|04:59
|Now, switch to terminal and execute the script by typing -
+
|Now, switch to terminal and execute the script by typing:
  
 
|-
 
|-
 
|05:06
 
|05:06
|''' perl functionWithArgs dot pl''' and press '''Enter '''
+
|''' perl functionWithArgs dot pl''' and press '''Enter '''.
  
 
|-
 
|-
 
|05:14
 
|05:14
|The output is as displayed on the screen
+
|The output is as displayed on the screen.
 
   
 
   
 
|-
 
|-
 
| 05:23
 
| 05:23
| Now, let us look at a '''functions''' which returns a single value.  
+
| Now, let us look at a functions which returns a single value.  
  
 
|-
 
|-
Line 253: Line 253:
 
|-
 
|-
 
|06:01
 
|06:01
|The return value of the ''' function''' is caught in ''' $addition variable.'''
+
|The return value of the function is caught in ''' $addition''' variable.
  
 
|-
 
|-
Line 269: Line 269:
 
|-
 
|-
 
|  06:20
 
|  06:20
|So, switch to terminal and type-
+
|So, switch to terminal and type:
  
 
|-
 
|-
Line 289: Line 289:
 
|-
 
|-
 
|  06:53
 
|  06:53
| In gedit, I have opened a file and named it as ''' funcWithMultipleRtrnVals dot pl'''  
+
| In gedit, I have opened a file and named it as ''' funcWithMultipleRtrnVals dot pl'''.
  
 
|-
 
|-
 
|  07:04
 
|  07:04
|Please do like wise in your text editor
+
|Please do like wise in your text editor.
  
 
|-
 
|-
Line 305: Line 305:
 
|-
 
|-
 
|  07:21
 
|  07:21
| The return values of the function are caught in variables ''' $var1, $var2 and  $addition'''  
+
| The return values of the function are caught in variables ''' $var1, $var2 and  $addition'''.
 
   
 
   
 
|-
 
|-
Line 313: Line 313:
 
|-
 
|-
 
|07:42
 
|07:42
|This illustration demonstrates how we can return an ''' array'''  from a '''function'''.
+
|This illustration demonstrates how we can return an array from a function.
  
 
|-
 
|-
Line 325: Line 325:
 
|-
 
|-
 
|08:03
 
|08:03
| Now let us execute the ''' Perl''' script on the terminal by typing -
+
| Now let us execute the PERL script on the terminal by typing:
 
   
 
   
 
|-
 
|-
Line 341: Line 341:
 
|-
 
|-
 
|  08:32
 
|  08:32
|'''Perl''' provides several '''inbuilt functions.'''
+
|PERL provides several inbuilt functions.
  
 
|-
 
|-
Line 349: Line 349:
 
|-
 
|-
 
|08:49
 
|08:49
|Calling '''inbuilt functions''', similar to calling any other function, which we define.
+
|Calling inbuilt functions, similar to calling any other function which we define  
  
 
|-
 
|-
 
|08:57
 
|08:57
|E.g ''' sort open bracket @arrayName close bracket semicolon'''
+
|e.g '''sort open bracket @arrayName close bracket''' semicolon.
 
 
 
 
 
|-
 
|-
 
|09:04
 
|09:04
| Try incorporating some ''' inbuilt functions''' in the sample programs we used.  
+
| Try incorporating some inbuilt functions in the sample programs we used.  
  
 
|-
 
|-
Line 369: Line 369:
 
|-
 
|-
 
|09:15
 
|09:15
|In this tutorial, we have learnt -
+
|In this tutorial, we have learnt
 
   
 
   
 
|-
 
|-
 
|09:17
 
|09:17
|''' Functions in Perl'''  
+
|* '''functions''' in PERL
  
 
|-
 
|-
 
|09:19
 
|09:19
| ''' functions''' with '''arguments''' and   
+
|* '''functions''' with '''arguments''' and   
  
 
|-
 
|-
 
|09:22
 
|09:22
|'''functions''' which '''return values''' using sample programs.  
+
|* '''functions''' which '''return''' values using sample programs.  
  
 
|-
 
|-
 
|09:27
 
|09:27
|Here is assignment for you:   
+
|Here is an assignment for you:   
  
 
|-
 
|-

Revision as of 20:56, 13 May 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 * function 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 a 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 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 functions 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 eg- Arrays, Hash, sort, scalar, each, keys etc.
08:49 Calling inbuilt functions, 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 summaries 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.

Contributors and Content Editors

Gaurav, PoojaMoolya, Sandhya.np14