November 2001
Beginner
320 pages
5h 53m
English
Functions in Perl are defined using the sub statement:
sub FUNC
{
}
The equivalent in Python is the def statement:
def FUNC():
# Statements
Note that the parentheses in the Python statement are compulsory.
Arguments are passed to a function in Perl as part of the @_ array. For example, the add() function can be defined like this:
sub add
{
my ($x, $y) = @_;
my $result = $x+$y;
return $result;
}
I've deliberately made this function definition quite long so as to help when comparing it against the Python version.
Within Python, the function definition defines the arguments that the function accepts. For example, we can rewrite the above statement as:
def add(x,y):
result = x+y
return result
The x and y arguments ...