Sei sulla pagina 1di 15

PERL FUNCTIONS

C. Prayline Rajabai
Assistant Professor
SENSE
VIT University
INTRODUCTION
A Perl subroutine or function is a group of statements that
together perform a task.

Perl uses the terms subroutine, method and function


interchangeably.

It is the user defined function and allows the code reuse.

Defined once, used multiple times.

2
DEFINING AND CALLING A SUBROUTINE
Subroutines can be defined using the sub command
Syntax:
sub sub_name {
# Body of the subroutine
#...
}

Subroutine can be called using & followed by the subroutine


name. However & is optional.

Syntax:
&sub_name;
&gcd($val1,$val2); # passing two parameters
3
HELLO WORLD EXAMPLE USING SUBROUTINE
Two steps

Define the subroutine

Call the subroutine

# Function definition
sub Hello{
print "Hello, World!\n";
}

# Function call
Hello();
4
PASSING ARGUMENTS TO A SUBROUTINE
Various arguments can be passed to a subroutine .

They can be accessed inside the function using the special


array @_.

First argument to the function is in $_[0], the second is in


$_[1].

5
RETURN STATEMENT
Use of return statement in a subroutine is optional.

If return is omitted, then perl returns the last evaluated


value.

A subroutine can also return a non-scalar value.

You can return arrays and hashes from the subroutine like
any scalar.

Returning more than array or hash normally causes them to


lose their separate identities.

6
RETURN STATEMENT EXAMPLE
sub Average{ # get total number of arguments passed.
$n = scalar(@_);
$sum = 0;
foreach $item (@_){
$sum += $item;
}
$average = $sum / $n;
return $average;
}
# Function call
$num = Average(10, 20, 30);
print "Average for the given numbers : $num\n;
7
Output : Average for the given numbers : 20
PASSING LIST TO SUBROUTINES
Example:
# Function definition
sub PrintList{
my @list = @_;
print "Given list is @list\n";
}
$a = 10;
@b = (1, 2, 3, 4);

# Function call with list parameter


PrintList($a, @b);

8
Output : Given list is 10 1 2 3 4
PASSING HASHES TO SUBROUTINES
When you supply a hash to a subroutine or operator that accepts
a list, then the list is automatically translated into a list of
key/value pairs.

sub PrintHash{
my (%hash) = @_;
foreach my $key ( keys %hash ){
my $value = $hash{$key};
print "$key : $value\n";
}} Output: name : Tom
%hash = ('name' => 'Tom', 'age' => 19); age : 19

# Function call with hash parameter 9


PrintHash(%hash);
PRIVATE VARIABLES IN A SUBROUTINE
By default, all variables in Perl are global variables.

They can be accessed from anywhere in the program.

You can create private variables called lexical variables with


the my operator.

It confines a variable to a particular region of code in which


it can be used and accessed.

Outside that region, this variable can not be used or


accessed.

This region is called its scope.


10
A lexical scope is usually a block of code.
PRIVATE VARIABLES IN A SUBROUTINE

Example :
$a = 3.14159;
{ Output :
my $a = 3; In block, $a = 3
print "In block, \$a = $a\n"; In block, $::a = 3.14159
print "In block, \$::a = $::a\n"; Outside block, $a = 3.14159
} Outside block, $::a = 3.14159
print "Outside block, \$a = $a\n";
print "Outside block, \$::a = $::a\n";

$::a refers to $a in the 'global' namespace. 11


TEMPORARY VALUES VIA LOCAL
The local is mostly used when the current value of a
variable must be visible to called subroutines.

A local just gives temporary values to global


variables.

This is known as dynamic scoping.

Note : Lexical scoping is done with my keyword

12
TEMPORARY VALUES VIA LOCAL
Example :
# Global variable
$string = "Hello, World!";
sub PrintHello{
# Private variable for PrintHello function
local $string;
$string = "Hello, Perl!";
PrintMe();
print "Inside the function PrintHello $string\n";}
sub PrintMe{
print "Inside the function PrintMe $string\n";}
# Function call
PrintHello(); 13

print "Outside the function $string\n;


TEMPORARY VALUES VIA LOCAL

Output :

Inside the function PrintMe Hello, Perl!

Inside the function PrintHello Hello, Perl!

Outside the function Hello, World!

14
EXERCISE
Write two subroutines. Both use a variable
defined in the body of the program. The first
sub multiplies the variable by ten, while the
second sub divides the number by two. Prompt
the user for the number, and then call both subs
and display the results from the Perl script (not
the subs).
Write a subroutine that expects three numbers
passed as arguments, and multiply the three
together, returning the result from the
subroutine. Call the subroutines from inside a
Perl script that asks the user for all three
numbers. Use names for each argument inside
the subroutine itself. 15

Potrebbero piacerti anche