Difference between revisions of "PERL/C2/Functions-in-Perl/English"
(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…') |
Nancyvarkey (Talk | contribs) |
||
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.''' | ||
− | + | ||
+ | |- | ||
+ | | 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 | + | | 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''' | ||
− | |||
− | |||
|- | |- | ||
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 | + | 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
|
|
Slide | Welcome to the spoken tutorial on Functions in Perl. |
Slide: Learning Objectives | In this tutorial, we will learn about -
|
Slide: System Requirements | For this tutorial, I am using
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.
|
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.
|
Slide | Note:
|
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
|
In gedit
printText();
print “This is illustration of simple Perl Function\n”; } |
Type the code as displayed on the screen.
<<pause>>
Then, the execution control is passed to that function.
This function will print out the given text. |
Save your file. | |
Switch to terminal
|
Then switch to the terminal and execute the Perl script by typing
|
Highlight the output on the terminal
|
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
|
Gedit
addVariables(10, 20);
($var1, $var2) = @_; $addition = $var1 + $var2; print “Addition is: $addition\n”; } |
Type the following piece of code as shown on the screen.
<<pause>>
|
Save your file. | |
Slide | Note:
$var1 = shift @_; $var2 = shift @_;
|
Terminal
|
Now, switch to terminal and execute the script
by typing -
and press Enter
<pause>> |
Now, let us look at a functions which returns a single value. | |
Slide | sub return_single_value {
#piece of code
} |
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
$addition = addVariables(10, 20); print “Addition is: $addition\n”;
($var1, $var2) = @_; $add = $var1 + $var2; return $add; } |
Open a file in your text editor and type the following piece of code as shown.
|
Save the file. | |
Terminal
perl funcWithSingleRtrnVal.pl
|
Now let us execute the script.
So, switch to terminal and type-
and press Enter.
<pause>> |
Now, let us see a function which returns multiple values. | |
Slide | sub return_multiple_values {
#piece of code
} |
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
use Data::Dumper;
($var1, $var2, $addition) = addVariables(10, 20); print "Addition of $var1 and $var2 is: $addition\n";
($var1, $var2) = @_; $add = $var1 + $var2; return ($var1, $var2, $add); }
print "Array\n"; print Dumper \@marksArray;
print "Hash\n"; print Dumper \%marksHash;
@mArray = (98, 96, 95); return @mArray; }
%mHash = ( 'English' => 98, 'Physics' => 96, 'Chemistry' => 95 ); return %mHash; } |
Now, type the following piece of code as shown.
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
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 -
and press Enter.
<pause>>
|
Slide | Perl provides several inbuilt functions.
We learnt some of them in earlier tutorials. For eg- Arrays, Hash, sort, scalar, each, keys etc..
|
Slide: Summary | Let us summarize.
In this tutorial, we have learnt -
using sample programs. |
Slide: Assignment | Here is assignment for you -
|
About the Project | Watch the video available at the following link
download and watch it |
Spoken Tutorial Workshops | The Spoken Tutorial Project Team
test
contact at spoken hyphen tutorial dot org |
Acknowledgment | Spoken Tutorial Project is a part of the Talk to a
Teacher project
Education through ICT, MHRD, Government of India.
spoken hypen tutorial dot org slash NMEICT hyphen Intro |
Hope you enjoyed this Perl tutorial.
This is Amol Brahmankar signing off.
|