Core Function Function Variables

From Sputnik Wiki
Jump to: navigation, search
$var = function(<args>){<stmList>};
$var(<params>);

Contents

Description

Dynamically create function source code and place it in a variable and call it from the variable

$var

The variable to place the function or call the function from.

args

The functions arguments

stmList

A list of statements to execute inside the function

params

Parameters to use when calling the function

Return Value

Depends what the function returns.

Remarks

None.

Example

Basic example

my $Func =  Function(){ println("Hello"); };
$Func();
// Prints
// Hello

This time with some params

my $Func =  Function( $a, $b ){ println("Value is: " . ($a + $b)); };
$Func(10, 20);
// Prints
// Value is: 30

Using an array

my $FuncArray = array();
$FuncArray[] = Function(){ println("Hello"); };
$FuncArray[] = Function(){ println("Test"); };
$FuncArray[] = Function(){ println("Cat"); };
 
foreach($FuncArray as $f)
{
	$f(); // Call it
}
// Prints
// Hello
// Test
// Cat

Using the @Args variable for infinite paramters

my $Func =  [Args("true")]Function(){ printr @Args; };
$Func(1, 3, 8, "Hi");
// Prints
// Array
// (
//     [0] => 1
//     [1] => 3
//     [2] => 8
//     [3] => Hi
// )

Of course it doesn't have to be on one line

my $Func =
[Args("true")]
Function()
{
	$count = count(@Args);
	say "There are $count param(s)";
	printr @Args;
};
 
 
 
$Func(1, 3, 8, "Hi");
// Prints
// There are 4 param(s)
// Array
// (
//     [0] => 1
//     [1] => 3
//     [2] => 8
//     [3] => Hi
// )
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox