User Function
(Created page with "<pre> Function <name> ( <Parameters> ... ) { statements ... } </pre> === Description === === Features === === Remarks === === Examples === ==== Important Examples ==== ==...") |
(→Unlimited Function Parameters) |
||
Line 19: | Line 19: | ||
===== Unlimited Function Parameters ===== | ===== Unlimited Function Parameters ===== | ||
− | If you’ve most likely noticed that several function such as println() can take an indeterminate number of arguments. Normally when defining a function you specify each argument in the function declaration. Obviously it would be impossible to define an infinite number of arguments in such a way. Sputnik does, however, allow you to accomplish this through the array | + | If you’ve most likely noticed that several function such as println() can take an indeterminate number of arguments. Normally when defining a function you specify each argument in the function declaration. Obviously it would be impossible to define an infinite number of arguments in such a way. Sputnik does, however, allow you to accomplish this through the array @args. |
− | + | @args is an array consisting of all of the arguments passed to a function. Using this method you can bypass the conventional method of defining parameters in the function definition all-together. Here is an example: | |
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
Line 29: | Line 29: | ||
{ | { | ||
my $Total = 0; | my $Total = 0; | ||
− | foreach( | + | foreach( @args as $item ) |
{ | { | ||
$Total += $item; | $Total += $item; | ||
Line 39: | Line 39: | ||
If for whatever reason you need to know the total number of arguments passed to a function, You can simply use UBound($args) within the function. | If for whatever reason you need to know the total number of arguments passed to a function, You can simply use UBound($args) within the function. | ||
− | When retrieving arguments in this manner it is important to remember that | + | When retrieving arguments in this manner it is important to remember that @args only contains an array of arguments passed by the user. It does not account for default values etc. |
+ | |||
+ | Note - If outside a function @args will return an empty array. | ||
==== General Examples ==== | ==== General Examples ==== |
Revision as of 01:19, 20 November 2011
Function <name> ( <Parameters> ... ) { statements ... }
Contents |
Description
Features
Remarks
Examples
Important Examples
Unlimited Function Parameters
If you’ve most likely noticed that several function such as println() can take an indeterminate number of arguments. Normally when defining a function you specify each argument in the function declaration. Obviously it would be impossible to define an infinite number of arguments in such a way. Sputnik does, however, allow you to accomplish this through the array @args.
@args is an array consisting of all of the arguments passed to a function. Using this method you can bypass the conventional method of defining parameters in the function definition all-together. Here is an example:
println( Add(1, 2, 3) ); // will return 6 Function Add() { my $Total = 0; foreach( @args as $item ) { $Total += $item; } return $Total; }
If for whatever reason you need to know the total number of arguments passed to a function, You can simply use UBound($args) within the function.
When retrieving arguments in this manner it is important to remember that @args only contains an array of arguments passed by the user. It does not account for default values etc.
Note - If outside a function @args will return an empty array.