0% found this document useful (0 votes)
13 views2 pages

Subroutine

The document contains Perl code that defines and calls several subroutines: 'Hello' for printing a greeting, 'Average' for calculating the average of given numbers, and 'area' for computing the area of a square. It demonstrates how to pass parameters to subroutines using special list array variables. The code includes function calls and prints the results of the calculations.

Uploaded by

atharva.dc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Subroutine

The document contains Perl code that defines and calls several subroutines: 'Hello' for printing a greeting, 'Average' for calculating the average of given numbers, and 'area' for computing the area of a square. It demonstrates how to pass parameters to subroutines using special list array variables. The code includes function calls and prints the results of the calculations.

Uploaded by

atharva.dc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#!

/usr/bin/perl

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

# Function call
Hello();

#!/usr/bin/perl

# Function definition
sub Average {
# get total number of arguments passed.
$n = scalar(@_);
$sum = 0;

foreach $item (@_) {


$sum += $item;
}
$average = $sum / $n;

print "Average for the given numbers : $average\n";


}

# Function call
Average(10, 20, 30);

Passing parameters to subroutines: This is used to pass the values as


arguments.This is done using special list array variables ‘$_’. This will
assigned to the functions as $_[0], $_[1] and so on.

# Perl Program to demonstrate the


# Passing parameters to subroutines

#!/usr/bin/perl

# defining subroutine
sub area
{
# passing argument
$side = $_[0];

return ($side * $side);


}

# calling function
$totalArea = area(4);

# displaying result
printf $totalArea;

You might also like