In Python, I'm used to things like
def send_command(command, modifier = None):
and then the modifier argument is optional, and the absence of the argument can be differentiated from an argument of 0. Is there similar functionality in C? I'm inexperienced with C, and Googling, but can't find a clear statement of how to use optional parameters in C. It seems you can assign them similarly, like this:
void send_command(uint8_t command, uint8_t modifier = 0) {
so the second argument is optional and defaults to 0 if not used? (Edit: No, this is invalid C anyway)
But can the function distinguish between send_command(SOMETHING)
and send_command(SOMETHING, 0)
? Ideally, the second parameter could be any uint8 value, including 0.
Maybe NULL is different from 0?
void send_command(uint8_t command, uint8_t modifier = NULL) {
The C Programming Language has no optional parameters.
You can pass NULL as a function parameter only if the specific parameter is a pointer. The only practical way is with a pointer for a parameter.
You don't pass option parameters. You pass optional arguments! For more explicit control than that provided by reserving sentinel values, check out boost::optional<>.
It means we call method without passing the arguments. The optional parameter contains a default value in function definition. If we do not pass optional argument value at calling time, the default value is used.
C does not support optional parameters. Nor does it support function overloading which can often be used to similar effect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With