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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title Of Script: Functions in Perl''' '''Author: Amol Brahmankar''' '''Keywords: Functions in Perl, video tutorial, sub, subroutine, function without arguments or return val…')
 
 
Line 69: Line 69:
 
* '''function '''definition can be written anywhere in the script or in another '''module.'''
 
* '''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.'''
 
* 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-
+
 
 +
|-
 +
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide
 +
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"|
 +
To include the '''module '''file in the script, one has to use the following syntax-
  
 
'''use ModuleFileName;'''  
 
'''use ModuleFileName;'''  
Line 75: Line 79:
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"|  
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"|  
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us understand this using asample program.
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us understand this using a sample program.
  
 
|-
 
|-
Line 111: Line 115:
  
  
This is the declaration & definition of the '''function'''. This '''function''' will print out the given text.
+
This is the declaration & definition of the '''function'''.  
 +
 
 +
This '''function''' will print out the given text.
  
 
|-
 
|-
Line 197: Line 203:
  
  
* The passed arguments are caught in '''$var1 '''& '''$var2.'''
+
* The passed '''arguments''' are caught in '''$var1 '''& '''$var2.'''
 
* '''@_''' 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.  
 
* 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 219: Line 225:
 
'''$var2 = shift @_;'''
 
'''$var2 = shift @_;'''
  
* '''shift @_ '''removes the '''element '''at '''first position '''from '''@_ array''' and assigns it to a '''variable'''.
+
* '''shift @_ '''removes the '''element '''at '''first position '''from '''@_ array'''  
 +
* and assigns it to a '''variable'''
 
* Another way is;<br/> '''<nowiki>$var1 = $_[0];</nowiki>'''<br/> '''<nowiki>$var2 = $_[1];</nowiki>'''
 
* Another way is;<br/> '''<nowiki>$var1 = $_[0];</nowiki>'''<br/> '''<nowiki>$var2 = $_[1];</nowiki>'''
 
* 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.'''
Line 315: Line 322:
  
 
'''Addition is: 30'''
 
'''Addition is: 30'''
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now let us execute the script. So, switch to '''terminal '''and type-
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now let us execute the script.  
 +
 
 +
So, switch to '''terminal '''and type-
  
  
 
'''perl funcWithSingleRtrnVal dot pl'''
 
'''perl funcWithSingleRtrnVal dot pl'''
  
and press '''Enter'''
+
and press '''Enter'''.
  
  
The output is as displayed on the '''terminal'''
+
The output is as displayed on the '''terminal'''.
  
 
<nowiki><pause>></nowiki>
 
<nowiki><pause>></nowiki>
Line 350: Line 359:
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| In '''gedit, '''I have opened a file and named it as '''funcWithMultipleRtrnVals dot pl'''
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| In '''gedit, '''I have opened a file and named it as '''funcWithMultipleRtrnVals dot pl'''
  
 
Pls do likewise in your text editor.
 
  
 
|-
 
|-
Line 425: Line 432:
  
 
Here, we are calling '''addVariables function''' with '''parameters''' '''10''' and '''20'''.  
 
Here, we are calling '''addVariables function''' with '''parameters''' '''10''' and '''20'''.  
 +
  
  
 
The return values of the '''function''' are caught in '''variables''' '''$var1, $var2''', '''$addition'''.
 
The return values of the '''function''' are caught in '''variables''' '''$var1, $var2''', '''$addition'''.
 +
  
  
 
This '''function''' does addition and returns the passed parameters and the resultant answer.
 
This '''function''' does addition and returns the passed parameters and the resultant answer.
 +
 +
 +
  
  
Line 436: Line 448:
  
  
Similarly this demonstrates how '''hash''' can be returned from a '''function.'''
+
 
 +
 
 +
 
 +
 
 +
 
 +
Similarly, this demonstrates how '''hash''' can be returned from a '''function.'''
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
  
 
|-
 
|-
Line 499: Line 542:
  
  
Try some incorporating some''' inbuilt functions''' in the sample programs we used.
+
Try incorporating some''' inbuilt functions''' in the sample programs we used.
  
  

Latest revision as of 18:23, 7 January 2014

Title Of Script: Functions in Perl

Author: Amol Brahmankar

Keywords: Functions in Perl, video tutorial, sub, subroutine, function without arguments or return value, function with arguments, function with a single return value, function with a multiple return values


Visual Cue
Narration
Slide Welcome to the spoken tutorial on Functions in Perl.
Slide: Learning Objectives In this tutorial, we will learn about -
  • Perl functions
  • functions with arguments
  • function with return values


Slide: System Requirements For this tutorial, I am using
  • Ubuntu Linux12.04 operating system
  • Perl 5.14.2
  • and the gedit Text Editor

You can use any text editor of your choice.

Slide: Prerequisites 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.
Slide sub simple_func {

# piece of code

}

Slide * 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.


Slide 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.
Slide

To include the module file in the script, one has to use the following syntax-

use ModuleFileName;

Let us understand this using a sample program.
Directly switch to the file in gedit Open a file in your text editor and name it as simpleFunction dot pl


Here is my simpleFunction dot pl file in gedit.

In gedit


#!/usr/bin/perl


# Call the function

printText();


sub printText {

print “This is illustration of simple Perl Function\n”;

}

Type the code as displayed on the screen.

<<pause>>


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.
Switch to terminal


perl simpleFunction.pl


Then switch to the terminal and execute the Perl script by typing


perl simpleFunction dot pl


and press Enter.

Highlight the output on the terminal


This is illustration of simple Perl Function

The output will be as shown <<pause>>



Now, let us see a function with arguments.
Slide sub func_with_parameters {

($variable1, $variable2,...$variableN) = @_;

# Action to be perform on the arguments pass to this function

}

Let us understand this function using a sample program.
Directly switch to the file in gedit Open a file in your text editor and name it as functionWithArgs dot pl


Here is my functionWithArgs script in gedit.

Gedit


#!/usr/bin/perl


# Call the function

addVariables(10, 20);


sub addVariables {

($var1, $var2) = @_;

$addition = $var1 + $var2;

print “Addition is: $addition\n”;

}

Type the following piece of code as shown on the screen.

<<pause>>


Here, we are calling a function with two 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.
Slide Note:
  • @_ 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 = shift @_;

$var2 = shift @_;

  • shift @_ removes the element at first position from @_ array
  • and assigns it to a variable
  • Another way is;
    $var1 = $_[0];
    $var2 = $_[1];
  • The above mentioned way is similar to fetching elements of @_ array using index.


Terminal


Addition is: 30

Now, switch to terminal and execute the script

by typing -


perl functionWithArgs dot pl

and press Enter


The output is as displayed on the terminal.

<pause>>

Now, let us look at a functions which returns a single value.
Slide sub return_single_value {

#piece of code


return $variable;

}

Let us understand the same using a sample program.
Directly switch to the file in gedit Let me switch to funcWithSingleRtrnVal dot pl script in gedit.
Gedit


#!/usr/bin/perl


# Call the function

$addition = addVariables(10, 20);

print “Addition is: $addition\n”;


sub addVariables {

($var1, $var2) = @_;

$add = $var1 + $var2;

return $add;

}

Open a file in your text editor and type the following piece of code as shown.


<<pause>>


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.
Terminal

perl funcWithSingleRtrnVal.pl


Addition is: 30

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.

<pause>>

Now, let us see a function which returns multiple values.
Slide sub return_multiple_values {

#piece of code


return ($variable1, $variable2,...$variableN);

}

Let us understand the same, using a sample program.
Directly switch to the file in gedit In gedit, I have opened a file and named it as funcWithMultipleRtrnVals dot pl


Gedit


#!/usr/bin/perl

use Data::Dumper;


# Call the function

($var1, $var2, $addition) = addVariables(10, 20);

print "Addition of $var1 and $var2 is: $addition\n";


sub addVariables {

($var1, $var2) = @_;

$add = $var1 + $var2;

return ($var1, $var2, $add);

}


@marksArray = getMarksArray();

print "Array\n";

print Dumper \@marksArray;


%marksHash = getMarksHash();

print "Hash\n";

print Dumper \%marksHash;


sub getMarksArray {

@mArray = (98, 96, 95);

return @mArray;

}


sub getMarksHash {

%mHash = (

'English' => 98,

'Physics' => 96,

'Chemistry' => 95

);

return %mHash;

}

Now, type the following piece of code as shown.


<<pause>>


Here, we are calling addVariables function with parameters 10 and 20.


The return values of the function are caught in variables $var1, $var2, $addition.


This function does 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.














Press ctrl + s Save your file.
Terminal


Addition of 10 and 20 is: 30

Array

$VAR1 = [

98,

96,

95

];

Hash

$VAR1 = {

'Chemistry' => 95,

'English' => 98,

'Physics' => 96

};

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.

<pause>>



Slide 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(@arrayName);


Try incorporating some inbuilt functions in the sample programs we used.


And observe their outputs.

Slide: Summary Let us summarize.

In this tutorial, we have learnt -

  • Functions in Perl
  • functions with arguments and
  • functions which return values

using sample programs.

Slide: Assignment Here is assignment for you -
  1. Write a function which takes 3 arguments.
  2. Perform some action on these arguments
  3. Return the result of the action performed on the arguments and print the same.


About the Project 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

Spoken Tutorial Workshops 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

Acknowledgment


http://spoken-tutorial.org\NMEICT-Intro

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

spoken hypen tutorial dot org slash NMEICT hyphen Intro

Hope you enjoyed this Perl tutorial.

This is Amol Brahmankar signing off.


Thanks for joining

Contributors and Content Editors

AmolBrahmankar, Nancyvarkey