-
Optional arguments
Is it possible to create a function that doesn't require some of it's arguments? Like a function
void dosomething(int a, int b, bool c);
that can be called like dosomething(1,5,true); or dosomething(1,5);?
The reason I need this is because I need a function I've already written and used to accept a slightly different input, but I don't want to track down all calls to the function to make sure they match the new defenition. Is this possible witout using an ellips (ellips='...'), like in the tutorials on this site?
-
Yes, they're called default arguments -
void dosomething(int a, int b, bool c=true);
Due to the way in which arguments are passed to functions you always have to specify these default arguments in right to left order with no gaps so you couldn't do -
void dosomething(int a=1, int b, bool c);
without giving the other two arguments default values.
-
Not even like this?
void dosomething(int a=1, int b, bool c);
dosomething( ,5,true);
Anyway, that pretty much solved my problem. Thanks!
-
dosomething( ,5,true);
No.. you need to have the default arguments as right most arguments.