Why isn't it possible to call a function which takes no arguments with a function call as argument which does not return any value (which IMHO is equivalent to calling a function which takes no arguments with no arguments).
For example:
void foo(void) {...}
void bar(void) {...}
foo(bar())
Don't get me wrong, I know void
is not a value and that it cannot be treated like one.
With my logic it would make sense and it should be possible to do that. I mean, why not? Any argument why that should not be possible?
Example 1: No Argument Passed and No Return Value The return type of the function is void .
The C specification says that a function declared without any argument (not even a void argument) takes an unspecified number of arguments. If you want to explicitly say that a function takes no arguments, you need to specify a single void argument type without a name.
Void functions are created and used just like value-returning functions except they do not return a value after the function executes. In lieu of a data type, void functions use the keyword "void." A void function performs a task, and then control returns back to the caller--but, it does not return a value.
To call a function which takes no arguments, use an empty pair of parentheses. Example: total = add( 5, 3 );
I'm not convinced that any of the reasons I've heard are good ones.
See, in C++, you can return a void
function's result:
void foo() {
// ...
}
void bar() {
// ...
return foo();
}
Yes, it's exactly the same as:
foo();
return;
but is much more consistent with generic programming, so that you can make a forwarding function work without having to worry about whether the function being forwarded has void
return.
So, if a similar system applied so that a void
return constituted a nullary call in a function composition scenario, that could make function composition more generic too.
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