BASIC FUNCTION DEFINITION

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

Get Perl To Python Migration now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.