![]() |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Sometimes you want a function to be able to receive a variable amount of arguments. There's two ways of doing this and it can be discussed if it's correct to put both explanations in this chapter, but it's sort of logical to do so and not too hard to find.
A function that is defined varargs will be able to receive a
variable amount of arguments. The variables that aren't specified
at the call will be set to 0.
varargs void
myfun(int a, string str, float c);
{
}
|
The call myfun(1); will set a to 1, str and
c to 0. Make sure you test the potentially unused variables
before you try to use them so that they do contain a value you can use.
There's another way as well. You can specify default values to the
variables that you're uncertain about. Then you don't have to declare
the function varargs and you will have proper default values
in the unused argument variables as well.
void
myfun(int a, string str = "pelle", float c = 3.0);
{
}
|
This function must be called with at least one argument, the first, as
it wasn't given a default value. The call myfun(1, "apa"); will
set a to 1, str to "apa" and c to
3.0.