"Sputnik" help  
Sputnik Help
$var = "options" function( <args> ){ <stmList> };
$var(<params>);

Description

Make sure you have read and understand all of User Functions before you read this section.

Parameters

<rules>

Optional; Here you can define the rules for the function to use.

To learn about rules go read User Functions since everything that applies there also applies here.

<args>

Optional; These arguments (or parameters) are used to define the variables the function accepts.

To learn about this go read User Functions since everything that applies there also applies here.

<stmList>

Optional; A list of statements to execute as part of the function.

You must at least include { } it does not need anything inside it but it will be useless without some code to execute.

Return Value

Depends what the function returns.

Remarks

User Variable Functions in Sputnik allow you to assign functions to variables, providing flexibility in defining and calling functions within your code.

With user variable functions, you can assign a function definition to a variable. This means that the variable itself becomes a callable entity, acting as a function. It allows you to invoke the function by using the variable name followed by parentheses, similar to calling a regular function.

User variable functions offer a powerful and dynamic way to handle functions in your code. They can be particularly useful in scenarios where you need to define functions on-the-fly or conditionally based on runtime conditions.

By storing functions in variables, you gain the ability to pass them as arguments to other functions, store them in arrays or data structures, or even modify them during runtime. This flexibility enables advanced programming techniques such as higher-order functions and callbacks.

User variable functions provide a concise and expressive syntax, reducing the need for separate function definitions and allowing for more compact and readable code.

However, it's important to use user variable functions judiciously and ensure that the code remains maintainable and comprehensible. Proper documentation and clear variable naming are key to ensuring code readability and avoiding confusion.

Related

User Functions

Example

Basic example:

my $Func =  Function(){ say "Hello"; };
$Func();
// PRINTS
// Hello

This time with some params:

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

Using an array:

my $FuncArray = array();
$FuncArray[] = Function(){ say "Hello"; };
$FuncArray[] = Function(){ say "Test"; };
$FuncArray[] = Function(){ say "Cat"; };
foreach(my $f in $FuncArray)
{
    $f(); // Call it
}
// PRINTS
// Hello
// Test
// Cat

Of course you can call all functions in an Array directly (and it will skip non-functions):

my $FuncArray = array();
$FuncArray[] = Function(){ say "Hello"; };
$FuncArray[] = Function(){ say "Test"; };
$FuncArray[] = Function(){ say "Cat"; };
$FuncArray(); // Call it
// PRINTS
// Hello
// Test
// Cat
You can take this a little step further and get the return value of each function!
my $FuncArray = array();
$FuncArray[] = Function($a, $b){ return $a + $b; };
$FuncArray[] = Function($a, $b){ return $a * $b; };
$FuncArray[] = Function($a, $b){ return $a ** $b; };
$ret = $FuncArray(10, 20); // Call it
printr $ret;
// PRINTS
// Array
// (
//     [0] => 30
//     [1] => 200
//     [2] => 7.76627963145224E+18
// )

Using infinite paramters:

my $Func =  Function('.' $args){ 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 =
    Function('.' $args)
    {
        say "There are " . (Count)$args . " param(s)";
        printr $args;
    };
$Func(1, 3, 8, "Hi");
// PRINTS
// There are 4 param(s)
// Array
// (
//     [0] => 1
//     [1] => 3
//     [2] => 8
//     [3] => Hi
// )

To delete a function from a variable just unset() like:

my $Func =
    Function($a, $b)
    {
        say "Hello";
    };
unset($Func);
$Func(10, 20); // ERROR

You can also use functions by ref using the FuncRef function like:

Function Cat($a, $b)
{
    say "Meow! Value is: " . ($a + $b);
}
$Func = FuncRef("Cat");
$Func(10, 20);
// PRINTS
// Meow! Value is: 30

You can also use the & operator to ref a function like:

// Create a function
Function Cat($a, $b)
{
    say "Meow! Value is: " . ($a + $b);
}
// Create a ref to it using the &
$Func = &Cat;
// Call it
$Func(10, 20);
// PRINTS
// Meow! Value is: 30

 


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org