"Sputnik" help  
Sputnik Help
"options" Function <name> ( <parameters> ... )
{
<statements>
...
}

Description

Sputnik allows programmers to define their own functions.

A more advanced thing you can do with a function is convert a variable into a function using the User Variable Functions.

Also make sure to read all the stuff in related since it deals with additional functionality of the functions.

Parameters

<name> The name of the function it can be made of alphanumeric characters and _ characters.
<parameters>

This defines the params the function will accept there are many types and even defaults.

<statements> Optional; None or more statements to be executed by the function.
<rules> Optional; You can add special rules to your function to tell it how to operate using the [] brackets before the word "Function".

Here is a list of the possible rules:

TYPE                   WHAT IT DOES
"..."                  Allows infinite parameters
"o" Override (Replace) a CORE function with this name and forces all
call to that function to route to this function instead "r" Replace a USER DEFINED function with this name and forces all call to that function to route to this function instead "s" Sets the function so only one thread can be running this function at a time "c" Causes the function to add to a class when used with the "embedded" keyword instead of placing the function on the global function roster Can be written as 'c:ClassName' to find and add function to it "f" If this function is inherited by a class yet the class contains a function with the same name override that function with this function "^" If the function finds another function with the same name during creation it will prefix its code to the top of that function effectively merging them "$" If the function finds another function with the same name during creation it will suffix its code to the bottom of that function effectively merging them "m" Makes it so the function doesnt have its own stack so it executes directly this basically makes it so the functions code executes "where is" as if the code was physically at the call instead of inside a function "g" Makes this function into a Generator that suppors Yield and YieldFrom "a" Makes this function Abstract "ao" Makes this function override an Abstract
"b" Makes this function a bind function that can extend primitive types "rd" Makes this function process the return statement after the defers not before "mf" Makes this function a magic function so it can be overridden by a class __ function with same name "mfe" Same as "mf" but running this function after the __ magic function with @Magic return value of it
   

Return Value

You decide.

Remarks

The "Function" statement in Sputnik empowers programmers to define their own custom functions, unlocking a world of possibilities for code organization and reusability. By creating functions, you can encapsulate a series of statements into a single entity that can be easily invoked and executed whenever needed.

To define a function, you start with the keyword "Function" followed by the name of the function, which can consist of alphanumeric characters and underscores. This allows you to give meaningful names to your functions that reflect their purpose or functionality.

Functions can have parameters, specified within parentheses after the function name. These parameters define the inputs that the function expects when it is called. Sputnik supports various parameter types, and you can even assign default values to parameters, providing flexibility and versatility in how the function is used.

Within the function body, you can write one or more statements that constitute the instructions executed when the function is invoked. This allows you to perform calculations, manipulate data, or carry out any desired operations within the function's scope.

The return value of a function is determined by the programmer. You can explicitly specify the value to be returned using the "return" keyword, or the function can implicitly return a value based on the executed statements. This enables you to design functions that perform specific tasks and produce meaningful results.

Functions in Sputnik support modularity and code reuse, allowing you to define a function once and call it multiple times from different parts of your codebase. This promotes cleaner and more organized code, as complex operations can be abstracted into functions, enhancing readability and maintainability.

By utilizing the "Function" statement, you can harness the power of structured programming and create well-organized, efficient, and reusable code that enhances the overall functionality of your Sputnik programs.

Related

User Variable Functions
Embedded Functions
Local Functions
Infinite Parameters
Parameters As Pointers
Parameter Types
Return Types
Generators
Context
Abstract Functions
Rule Override
Rule Replace
Rule Prefix
Rule Suffix
Rule Synchronized
Rule No Stack
Rule Add To Class
Rule Override If Inherited
Rule Override Abstract
Rule Bind
Rule Magic
Rule Return Defer

Example

The following is a definition of a function which given the co-ordinates of a point (x,y) will return its distance from the origin:

// Returns the distance of (x, y) from origin
Function PointDist($x, $y)
{
    my $dist = sqrt($x * $x + $y * $y);
    return $dist;
}
say PointDist(10.0, 20.0);
// PRINTS
// 22.3606797749979

Another basic example:

say Add(1, 2); // will return 3
Function Add($a, $b)
{
    return $a + $b;
}

Using a default value:

say Add(2); // will return 52
say Add(2, 4); // will return 6
Function Add($a, $b = 50)
{
    return $a + $b;
}

Using two default values:

say Add(); // will return 60
say Add(2); // will return 52
say Add(2, 4); // will return 6
Function Add($a = 10, $b = 50)
{
    return $a + $b;
}

You can use undef to specify an argument you wish to skip over and still use the default of:

Function Cat($x, $y = 10, $z = 20)
{
    say "X=$x, Y=$y, Z=$z";
}

Cat(2);
Cat(2, 4);
Cat(2, 4, 6);
Cat(2, undef, 6);

// PRINTS
// X=2, Y=10, Z=20
// X=2, Y=4, Z=20
// X=2, Y=4, Z=6
// X=2, Y=10, Z=6

This allows for the use of undef as an argument while leveraging default parameter values in function declarations. When a function is called with an undef argument, and the corresponding parameter has a default value, the default value is automatically used in place of undef. This feature enhances flexibility, enabling developers to omit specific arguments while ensuring that the function gracefully falls back to default values when needed. The provided examples showcase how Sputnik intelligently handles undef in function calls, seamlessly incorporating default values for a smoother and more concise coding experience.

You can also skip arguments you want to use the default by simply adding nothing inside the argument like so:

Function Cat($x, $y = 10, $z = 20)
{
    say "X=$x, Y=$y, Z=$z";
}

Cat(2);
Cat(2, 4);
Cat(2, 4, 6);
Cat(2, , 6);

// PRINTS
// X=2, Y=10, Z=20
// X=2, Y=4, Z=20
// X=2, Y=4, Z=6
// X=2, Y=10, Z=6

This can be demonstrated with a larger function call:

Function Cat($x, $y = 10, $z = 20, $cat = "meow", $dog = "woof")
{
    say "X=$x, Y=$y, Z=$z, Cat=$cat, Dog=$dog";
}

Cat(2, , , , "wuff");

// PRINTS
// X=2, Y=10, Z=20, Cat=meow, Dog=wuff

Default args don't have to be on the end. This code is valid:

Function Test($i = 4, $j, $k = 5)
{
    printf("%X %X %X\n", $i, $j, $k);
}
  
Test(,3);
// PRINTS
// 4 3 5

Test(,3,);
// PRINTS
// 4 3 5

We can even do operators within the arguments for example lets use += in there:

Function Testy($x += 10)
{
    say "X=$x";
}

Testy();
Testy(5);

// PRINTS
// X=10
// X=15

The function Testy has a default parameter $x initialized with a value of 10. When the function is called without providing a specific argument (Testy()), the default value of $x is automatically set to 10. However, if an argument is provided (Testy(5)), the operation += 10 is applied to the provided value, resulting in X=15 being printed.

We can take this to its extreme conclusion:

Function Shifter($x <<= 6)
{
    return $x;
}
Function Unshifter($x >>= 6)
{
    return $x;
}

my $a = Shifter(1);
my $b = Shifter(2);
my $c = Shifter(3);

say $a;
say $c;
say $b;

$a = Unshifter($a);
$b = Unshifter($b);
$c = Unshifter($c);

say $a;
say $c;
say $b;

// PRINTS
// 64
// 192
// 128
// 1
// 3
// 2

The provided code showcases the utilization of bitwise left (<<=) and right (>>=) shifting operators in Sputnik, offering a novel approach to arithmetic operations. The Shifter function employs left shifting to effectively multiply the input value by 64, resulting in outputs of 64, 128, and 192 for input values of 1, 2, and 3, respectively. Conversely, the Unshifter function utilizes right shifting to restore the original values, illustrating the reversibility of the operations. This demonstrates how Sputnik's bitwise shifting capabilities can be leveraged for efficient arithmetic transformations, providing developers with unique and powerful tools for numerical manipulation.

This unique feature simplifies the creation of functions that apply consistent operations to their parameters, offering concise and expressive code. Whether it's incrementing, multiplying, or performing other operations, using operators in function parameters allows for more flexible and efficient code in Sputnik.

Sputnik supports a powerful feature that allows you to use list statements in function arguments, providing a convenient way to unpack arrays. This feature is particularly useful when you want to pass multiple values from an array as separate arguments to a function without manually unpacking the array.

To use a list statement in a function argument, you can define the argument with the list keyword followed by the variable names that correspond to the values you want to extract from the array.

Function Cat($a, list($b, $c), $d)
{
    return "$a $b $c $d";
}

Function Dog($a, list($b, list($bb, $cc), $c), $d)
{
    return "$a $b $bb $cc $c $d";
}

say Cat(100, array(200, 300), 400);
say Dog(100, array(200, array(250, 350), 300), 400);

// PRINTS
// 100 200 300 400
// 100 200 250 350 300 400

By utilizing list statements in function arguments, you can easily extract values from arrays and work with them individually within the function.

It is also possible to call a function by using array elements as it's arguments:

Function Add($a, $b)
{
    return $a + $b;
}
my $arr = array(100, 200);
say Add(**$arr);
// PRINTS
// 300

Function References

Using functions:

Function Bar()
{
    say "bar";
}

my $bar = FuncRef(id bar);

$bar();
// PRINTS
// bar

Using &:

Function Bar()
{
    say "bar";
}

my $bar = &bar;

$bar();
// PRINTS
// bar


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