Search⌘ K

Solution Review: Sum of Two Numbers

Understand how to handle parameters in Perl subroutines by exploring a solution that sums two numbers. This lesson explains accessing function arguments using @_ and returning their sum effectively.

We'll cover the following...

Let’s look at the solution first, before jumping into the explanation:

Perl
sub sum{
return @_[0] + @_[1];
}
print sum(10, 5);

Explanation

To access the parameters of the function, we can use @_. To access the first parameter, we use @_[0], and to get the second parameter, we use @_[1]. Then, we simply return their sum.