"Sputnik" help  
Sputnik Help
local "options" function( <args> ){ <stmList> }

Description

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

Parameters

<options>

Optional; Here you can define the options/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

Local functions in Sputnik, defined using the local keyword, provide a means to encapsulate functionality within the scope of another function. The example demonstrates the usage of a local function within the main function Foo.

Function Foo($a, $b)
{
    local Function Bar($a)
    {
        return $a * 2;
    }
    return $a + Bar($b);
}
say Foo(5, 10);
// PRINTS
// 25

Encapsulation and Modularity
Local functions, such as Bar in this example, allow you to define a specific piece of functionality that is only accessible within the parent function (Foo). This encapsulation enhances code modularity by keeping the local function's logic isolated and not polluting the global scope.

Scope Isolation
The variables and parameters defined in the parent function are accessible within the local function, creating a well-defined scope for the encapsulated logic. In the example, Bar can access the parameter $a from the parent function Foo.

Dynamic Function Call
Local functions are invoked within the parent function just like any other function. In this case, Bar($b) is called within the return statement of Foo, demonstrating how local functions contribute to the overall computation of the parent function.

Improved Readability
By using local functions, you can break down complex tasks into smaller, more manageable components. This improves code readability and makes it easier to understand the purpose and flow of each part of the program.

Limited Visibility
Local functions are only accessible within the scope of the function in which they are defined. They are not visible outside of this scope, contributing to a cleaner and more organized code structure.

Use Cases
Local functions are particularly useful when a specific piece of functionality is required only within a certain context. They allow you to structure your code in a way that reflects the logical relationships between different tasks within a function.

In summary, local functions in Sputnik provide a mechanism for encapsulating and organizing code within the scope of a parent function. They contribute to code modularity, readability, and scope isolation, allowing for a more structured and maintainable codebase.

Related

User Functions

Example

Example of usage:

Function Foo($a, $b)
{
    local Function Bar($a)
    {
        return $a * 2;
    }
    return $a + Bar($b);
}
say Foo(5, 10);
// PRINTS
// 25

Accessing local functions from the outside:

Function Foo()
{
    local Function Bar($a)
    {
        return $a * 2;
    }
    local Function Foo($a)
    {
        return $a * 4;
    }
}
say FuncRef(id Foo)->Bar(20);
say FuncRef(id Foo)->Foo(20);
// PRINTS
// 40
// 80

A complex example showing many ways to use local functions:

Function CatLcl()
{
    local Function DogLcl($a)
    {
        return $a * 2;
    }
    printr FuncExists("DogLcl", @FuncExistLocal);
    printr FuncList(m/DogLcl/i);
    printr FuncInfo("DogLcl");
    my $fn = &CatLcl;
    say $fn->DogLcl(100);
    say (&CatLcl)->DogLcl(100);
    say FuncRef(id CatLcl)->DogLcl(100);
    say FuncRef("CatLcl")->DogLcl(100);
    return DogLcl(777);
}
my $fn = &CatLcl;
say $fn->DogLcl(100);
say (&CatLcl)->DogLcl(100);
say FuncRef(id CatLcl)->DogLcl(100);
say FuncRef("CatLcl")->DogLcl(100);
say CatLcl();

// PRINTS
// 200
// 200
// 200
// 200
// true
// Array
// (
//     [0] => Array
//         (
//             [Name] => doglcl
//             [Type] => Local
//         )
// )
// Array
// (
//     [Name] => doglcl
//     [ArgsBase] => a
//     [Args] => Array
//         (
//             [0] => Array
//                 (
//                     [Name] => a
//                 )
//         )
//     [FlagReturnTo] => true
// )
// 200
// 200
// 200
// 200
// 1554

 


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