"Sputnik" help  
Sputnik Help
Infinite Parameters

Description

This demonstrates how to create a function with infinite parameters.

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

If you’ve most likely noticed that several function such as Say() can take an indeterminate number of arguments. Normally when defining a function you specify each argument in the function declaration. Obviously it would be impossible to define an infinite number of arguments in such a way. Sputnik does, however, allow you to accomplish this through the ... flag on the last argument of the function.

Flag ... produces an array consisting of all of the arguments passed to a function at its point of origin and beyond.

Remarks

The concept of infinite parameters in functions introduces a powerful capability to handle an indefinite number of arguments. By using the ... flag on the last parameter of a function, you can create an array that captures all the arguments passed to the function beyond that point. This allows for flexible and dynamic processing of multiple values within the function.

Infinite parameters provide the flexibility to handle a varying number of arguments, enabling functions to adapt to different use cases. Whether it's calculating sums, concatenating strings, or performing any other operation that involves multiple values, the ... flag empowers you to work with an arbitrary number of arguments.

It's important to remember that the ... flag specifically captures the user-provided arguments and doesn't account for default values or other function parameters. By incorporating appropriate logic, such as loops, you can iterate over the array of arguments and perform operations accordingly.

The examples provided demonstrate how to define functions with infinite parameters and showcase their usage in practical scenarios. Embracing the concept of infinite parameters opens up a wide range of possibilities for designing versatile and adaptable functions.

By leveraging infinite parameters, you can create functions that efficiently handle various inputs, offering a dynamic and robust solution to your programming needs.

Related

User Functions

Example

Basic Example:

my $value = AddInts(10, 20, 30, 40, 50, 60, 70);
say "Value is $value";
Function AddInts("..." $values)
{
    my $count = (count)$values;
    say "Found '$count' Value(s)";
    say "Adding them up!";
    my $sum = 0;
    foreach(my $var in $values)
        $sum += $var;
    say "Returning....";
    return $sum;
}
// PRINTS
// Found '7 ' Value(s)
// Adding them up!
// Returning....
// Value is 280

When retrieving arguments in this manner it is important to remember that ... flag only contains an array of arguments passed by the user. It does not account for default values etc.

Here is an example with a normal param and then infinite:

my $value = AddInts("Cat", 10, 20, 30, 40, 50, 60, 70);
say "Value is $value";
Function AddInts($normal, "..." $values)
{
    say "Normal is: $normal";
    my $count = (count)$values;
    say "Found '$count' Value(s)";
    say "Adding them up!";
    my $sum = 0;
    foreach(my $var in $values)
        $sum += $var;
    say "Returning....";
    return $sum;
}
// PRINTS
// Normal is: Cat
// Found '7 ' Value(s)
// Adding them up!
// Returning....
// Value is 280

As you can see the ... flag has to go on the last function argument.

In a side note just . dot will work for example:

my $value = AddInts(10, 20, 30, 40, 50, 60, 70);
say "Value is $value";
Function AddInts("." $values)
{
    my $count = (count)$values;
    my $sum = 0;
    foreach(my $var in $values)
        $sum += $var;
    return $sum;
}
// PRINTS
// Value is 280

It just looks nicer to do 3 of them.


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