"Sputnik" help  
Sputnik Help
embedded "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

Embedded functions in Sputnik are primarily used to conditionally define and spawn functions based on specific conditions. They offer a way to create functions only if a certain condition is met, adding flexibility to your code execution.

By using the embedded keyword followed by the function definition, you can define an embedded function. These functions are typically used within conditional statements or control structures to dynamically add functionality when required.

Embedded functions are particularly useful when you want to conditionally create functions based on runtime conditions or user-defined criteria. You can check for the existence of a function using functions like FuncExists() or ClassFuncExists() and define the embedded function only if it does not already exist.

The ability to conditionally spawn functions through embedded functions allows for more dynamic code execution. You can selectively add functions to classes, override existing functions, or extend the behavior of your code based on specific runtime conditions.

Using embedded functions can help keep your code concise and focused by defining functions only when necessary, avoiding unnecessary clutter in your codebase. They provide a way to organize and structure your code based on conditions, making it more readable and maintainable.

In addition to the previous remark, embedded functions can be effectively used to check if a function already exists and define it if needed. This feature adds an extra layer of flexibility and control to your code execution.

By using the embedded keyword followed by the function definition within conditional statements or control structures, you can conditionally create functions based on the existence of other functions. This approach allows you to dynamically define functions only if they are not already defined, preventing redundant function declarations and potential conflicts.

You can use functions like FuncExists() or ClassFuncExists() to check if a function already exists before defining an embedded function. If the function does not exist, you can proceed to define it within the embedded function block.

This capability is particularly useful in scenarios where you want to extend the functionality of your code or add custom behavior only if certain conditions are met. It enables you to adapt your code at runtime, dynamically defining functions as needed based on the specific requirements of your application.

The ability to check and define functions on the fly using embedded functions provides a powerful mechanism for code organization and modularity. It allows you to encapsulate logic and functionality within the scope of the embedded function, improving the maintainability and readability of your code.

In summary, embedded functions offer the flexibility to check if a function exists and define it if needed. This feature enables dynamic code execution and allows you to add custom behavior to your program based on runtime conditions, promoting code modularity and enhancing the flexibility of your application.

Related

User Functions
Function Rule Add To Class

Example

Example:

//Test(); // This would crash since it does not exist
// Create function if it does not exist
if (!FuncExists("Test"))
{
    Embedded Function Test()
    {
        say "Hello from Test!";
    };
}
// Call it
Test();

Using this you can choose to create functions only if they do not exist.

Using the 'c' option/rule you can cause the function to get added to a class instead of global like so:

Class Test
{
    Function Cat()
    {
        if (!FuncExists("Dog"))
        {
            Embedded "c" Function Dog()
            {
                say "Woof! from class->" . GetClassName();
            };
        }
        Dog();
    }
}
$a = new Test();
$a->Cat();
// PRINTS
// Woof! from class->test

Using the 'c:ClsName' option/rule you can cause the function to get added to a class instead of global like so:

Class Test
{
    Function Cat()
    {
        Dog();
    }
}


if (!ClassFuncExists("Test", "Dog"))
{
    Embedded "c:Test" Function Dog()
    {
        say "Woof! from class->" . GetClassName();
    };
}

$a = new Test();
$a->Cat();
// PRINTS
// Woof! from class->test

In bother examples the function was added to the class and proved it by printing the class name of the currently running function.


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