Function Multiple arguments in Perl

Find this useful? Support us: Star on GitHub 6
Category: Error handling | Language: Perl

In Perl, you can pass multiple arguments or parameters to a function or method. To do so, you simply include the additional arguments in the function call.

Here is an example of a function that takes two arguments in Perl:

sub multiply {
    my ($num1, $num2) = @_;
    my $result = $num1 * $num2;
    return $result;
}

my $product = multiply(5, 10);
print "The product is $product\n";

In this example, the multiply function takes two arguments $num1 and $num2 which are stored in an array reference @_. The function calculates the product of these two arguments and returns it.

To call the multiply function, we pass in two values 5 and 10. The function then multiplies these two values and returns the product which is stored in the $product variable. Finally, we print the value of $product which is 50.

You can pass any number of arguments to a Perl function or method by including them in the function call. Here's an example of a function that takes three arguments:

sub calculate_sum {
    my ($num1, $num2, $num3) = @_;
    my $sum = $num1 + $num2 + $num3;
    return $sum;
}

my $total = calculate_sum(10, 20, 30);
print "The total is $total\n";

In this example, the calculate_sum function takes three arguments $num1, $num2, and $num3, and calculates their sum. We call the function with three values 10, 20, and 30. The function calculates their sum and returns it, which is stored in the $total variable. Finally, we print the value of $total which is 60.

Hope this helps!