This topic is now archived and is closed to further replies.

Procedures/functions with optional parameters?

This topic is 6166 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do you specify which are optional parameters in a procedure? I would like to be able to make two separate procedure calls: DrawCursor(1.0,&cursdata); DrawCursor(&cursdata); If the '1.0' (or any other value) is included it should be used accordingly inside the procedure, otherwise the code that uses the value should not be executed (or ignored). I haven't done this before and I can't get rid of the 'Procedure DrawCursor does not take 1 parameter(s)' error when I try to omit the optional parameter in the call. void DrawCursor(double colour, cursorpos *data) Thankyou, Paulcoz. Edited by - paulcoz on January 28, 2001 9:27:42 PM

Share this post

Link to post
Share on other sites
If you want to do that your gonna have to switch the parameters first b/c optional parameters have to be after any non-optional parameters. Then change the declaration so that the last parameter is equal to a value.

void DrawCursor(cursorpos *data, int colour = 1)

BTW, you can have more than one optional parameters but they must be after any non-optional parameters.

Digital Radiation

Share this post

Link to post
Share on other sites

What does the 'int colour = 1' mean? It won't change the value I pass into the procedure, will it?


Edited by - paulcoz on January 28, 2001 10:12:41 PM

Share this post

Link to post
Share on other sites
What the "int colour = 1" means is that if you don''t pass arguments that are optional, this is the value they will have in the function. If you pass a value as an argument, then the value sent will be the one used.

Hope this helps


Share this post

Link to post
Share on other sites
Heres the scoop on all that cool stuff. Say you have a function, and you know that a value that will always work for a function is 1, but it can change sometimes. Then you do this:
void Function (int OptionalParameter = 1);

That way, you can call it two ways:

Function (4); // OptionalParameter = 4
Function (); // OptionalParameter = 1

Do you get it?

One catch, any optional parameters must come after non-optional parameters. So heres an example:

void Function (int NonOptional, int Optional = 1); // Legal
void Function (int Optional = 1, int NonOptional); // Illegal

One more cool thing. Function Overloading. You can write more than one function with the same name, the only thing is, there has to be a difference in the number of parameters, or the parameter types must be different. Example:

void Function (int IntParm, float FloatParm);
void Function (int IntParm);
void Function (float FloatParm);
// All of those will work.
int Function (int IntParm);
float Function (int IntParm);
// Those won''t work together, because different return types don''t matter, except maybe in the case of void/non-void. That should get you on your way.


Share this post

Link to post
Share on other sites