Jump to content

Function definition: Difference between revisions

→‎{{header|Perl}}: simpler example and more info
(Pascal - corrected header)
(→‎{{header|Perl}}: simpler example and more info)
Line 470:
 
=={{header|Perl}}==
The most basic form:
<lang perl>sub multiply { return $_[0] * $_[1] }</lang>
Arguments in Perl subroutines are passed in the <code>@_</code> array, and they can be accessed directly, first one as <code>$_[0]</code>, second one as <code>$_[1]</code>, etc. When the above function is called with only one or no arguments then the missing ones have an undefined value which is converted to 0 in multiplication.
 
This is an example using [http://perldoc.perl.org/perlsub.html#Prototypes subroutine prototypes]:
<lang perl>sub multiply( $$ )
{
Line 475 ⟶ 480:
return $a * $b;
}</lang>
The above subroutine checks whether it was called with exactly two [http://perldoc.perl.org/perldata.html#Scalar-values scalar values] (two dollar signs in the signature) but those values may be not numbers or not even defined. The <code>@_</code> array is unpacked into <code>$a</code> and <code>$b</code> lexical variables, which are used later.
 
=={{header|Perl 6}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.