Tutorial 9.2 (Variadic functions) - Aerll/rpp GitHub Wiki

Variadic functions

While the name might sound scary, variadic functions are a very simple concept. They are essentially normal functions with a signature that has a special meaning. This signature allows them to accept any amount of values convertible to a certain type.

Making a function variadic

So how do we create one? The rule is very simple, any function with a single parameter which is an array of any type will be treated as a variadic function. For example, we could create a function that sums up all given values:

function->int Add(array int values)
    int sum = 0;
    for (i = 0 to values.last)
        sum = sum + values[i];
    end
    return sum;
end

We can now call this function and give it any amount of values:

warning(Add(1, 2, 3, 4, 5).str()); // 15
warning(Add(1, -1).str()); // 0
warning(Add(2, 3_7, [4, 2], 53.2, Rect([1, 0], [2, 1])).str()); // 154

Important thing to note here is that we're not limited to only int. We can actually pass any value convertible to int or array int, therefore this function will accept int, float, coord, range, object and array int.