Suppose I have this function:
void func() {}
When I call func
with some parameters (e.g. func(132)
), the C++
compiler yields an error, while the C
compiler doesn't.
What is the difference between the two compilers in this case? And what advantages/disadvantages the C++
has by arising this error?
If a function takes no parameters, the parameters may be left empty. The compiler will not perform any type checking on function calls in this case. A better approach is to include the keyword "void" within the parentheses, to explicitly state that the function takes no parameters.
C static code analysis: Functions without parameters should be declared with parameter type "void"
What should be passed in parameters when function does not require any parameters? Explanation: When we does not want to pass any argument to a function then we leave the parameters blank i.e. func() – function without any parameter.
Nothing will happen- meaning you won't get an error or a warning as passing the parameters in javascript is optional. All the parameters that weren't "supplied" will have the undefined value.
There are no advantages or disadvantages. C supports this for compatibility with K&R C from the 1980s. You might like this feature if you are still using code you wrote in the 1980s. You might dislike this feature if you want better diagnostics from your compiler.
void func();
In C, this means that func
takes unspecified parameters.
If you need to specify that the function takes no parameters, write it this way:
void func(void);
In C++ the two prototypes are the same. (In C, only the second one is a prototype.) If you compile with GCC/Clang's -Wstrict-prototypes
option, you will get warnings for using void func();
in C, as you should.
This is only about function declarations. In both languages, the following function definitions are the same:
// These two are the SAME
void func() { }
void func(void) { }
// These two are DIFFERENT
void func();
void func(void);
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