sub
Defines a function (subprogram).
Syntax
sub name { ... return ...; }Description
Defined with sub. Arguments come in the @_ array: $_[0] is the first, $_[1] is the second. Values are returned with return.
Example
Loading editor…
Expected output
17
When to use?
Preventing code repetition.
Common mistakes
- ⚠️ Arguments are retrieved from the @_ array: my ($a, $b) = @_;