"Sputnik" help  
Sputnik Help
Parameters As Pointers

Description

This demonstrates how to create a function that uses pointers with it's parameters.

Before reading this section make sure to read all User Functions and understand it.

You can pass variables as a pointer to a function so the function can modify them variables directly rather than work on a copy of them.

The only real benefit of doing this is it speeds up execution time by not having to copy the variable.

To make a pointer you use & like in C++ and place it in parameter section of the function see the examples.

Remarks

The concept of using parameters as pointers in functions introduces a way to directly modify variables passed to a function without creating copies. By using the & flag on the parameter, you can pass variables by reference, allowing the function to operate on the original variables and reflect any modifications outside the function.

Passing variables as pointers provides a performance benefit by eliminating the need to copy the variable, resulting in faster execution time. This approach is particularly useful when working with large data structures or when you want changes made within the function to persist globally.

It's important to note that when a function parameter has the & flag, the variables passed to it are sent as references. However, these references are automatically resolved before the function begins executing its statements. Essentially, the & flag causes the variable to appear inside the function as if it were passed directly.

If you encounter a situation where you need to pass a direct reference variable to a function, you can utilize the &* flag to pass and resolve the reference within the function. This ensures that modifications made to the referenced variable take effect.

The examples provided demonstrate how to define functions with parameters as pointers and showcase their usage in practical scenarios. By passing variables as pointers, you can directly modify their values within the function, enabling you to manipulate data more efficiently and effectively.

Using parameters as pointers offers a way to work with variables directly, avoiding unnecessary copying and ensuring that changes made within the function have a global impact. Incorporating pointers in function parameters can enhance performance and provide greater flexibility in handling data structures and complex operations.

Related

User Functions

Example

Full example:

// Make a string
my $MyString = "Hello";
 
// Print it
say $MyString; // Prints "Hello"
 
// Fail to modify it
Test($MyString);
 
// Print to prove fail
say $MyString; // Prints "Hello"
 
// Correctly modify it
Test2($MyString);
 
// Print modified string
say $MyString; // Prints "Hello world!"
 
// A function that does not use a pointer
// so any modifications to the variable
// do not leave the function since it is
// only playing with a *copy*
Function Test($str)
{
    $str .= " world!";
}
 
// A function that does use a pointer so
// any modifications that take place do
// indeed change the variable
Function Test2("&" $str)
{
    $str .= " world!";
}

When a function param has the "&" flag it causes variables passed to it to be sent as a ref however they are automatically resolved before the function begins executing statements so bascially the '&' just causes the variable to appear inside the function as if it was really there..

However what if you pass a direct ref variable to a function?

Function Test($str)
{
    $str .= " world!";
}
$a = "Hello";
$b = & $a;
Test($b);
say $a;
// PRINTS
// Hello

See how it failed to modify the ref to $a ? To solve this problem we need to first use the "&" flag to pass it directly then use the "*" flag to cause it to resolve the ref for example:

Function Test("&*" $str)
{
    $str .= " world!";
}
$a = "Hello";
$b = & $a;
Test($b);
say $a;
// PRINTS
// Hello World!

This is how we send a ref of a variable as a ref to a function and resolve it and modify it.

The code provided demonstrates the usage of a function called SumAndProduct. This function takes an array of numbers, $numbers, and two variables, $sum and $product, as parameters passed by reference.

Function SumAndProduct($numbers, "&" $sum, "&" $product)
{
    foreach ($number in $numbers)
    {
        $sum += $number;
        $product *= $number;
    }    
    return "Sum: $sum, Product: $product";
}

my $numbers = array(2, 4, 6, 8, 10);
my $sum = 0;
my $product = 1;
say "ORIG: $sum, $product";
say "Final " . SumAndProduct($numbers, $sum, $product);
say "CHANGED: $sum, $product";
// PRINTS
// ORIG: 0, 1
// Final Sum: 30, Product: 3840
// CHANGED: 30, 3840

Within the function, a foreach loop iterates over each number in the $numbers array. The value of each number is added to $sum and multiplied with $product. By using pass-by-reference, the changes made to $sum and $product inside the function are reflected outside the function as well.

In the example, an array of numbers [2, 4, 6, 8, 10] is created, and initial values of $sum and $product are set to 0 and 1 respectively. The original values of $sum and $product are printed, followed by the result of calling the SumAndProduct function with the array and the two variables. Finally, the updated values of $sum and $product are printed, demonstrating that they have been modified within the function.

The output of the code shows the original values of $sum and $product, the final values after calling the function, and confirms that the changes made within the function have affected the values of the variables outside the function.

 


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