Reputation:
Example:
void Function(int Number)
{
process.....
**return;**
}
Is it mandatory to use "return" at the end of each function or not?
Example 2:
void Function(**void**)
{
process...
}
Is the use of "void" necessary in the argument list if I do not receive any values?
Some say no and others say yes. What is right for a perfect understanding for the compiler and best practice in C?
Upvotes: 6
Views: 8481
Reputation: 133929
The omission of void
in parameters means that the function takes any number of arguments:
Suppose a program:
void func() {
}
void func2(void) {
}
int main(void) {
func(2);
func2(2);
}
Now compiling it with gcc -std=c11 -Wall -pedantic test.c
, you get errors from func2
only:
test.c: In function ‘main’:
test.c:9:5: error: too many arguments to function ‘func2’
func2(2);
^
test.c:4:6: note: declared here
void func2(void) {
That is, it is not a compile time error with GCC to call void func();
with arguments, while it is compile time error to call void func2(void)
with arguments. Even though the function does not have any parameters, it is still possible to call it with any number of arguments.
However, even if this does compile, the 6.5.2.2 Function calls says that "If the number of arguments does not equal the number of parameters, the behavior is undefined." (and func
was called with 1 argument, but has no parameters).
The C11 standard n1570 working draft says the following:
6.11.6 Function declarators
- The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.
(Fun fact: the standard itself uses int main()
in its examples).
As for the return
statement, it can be omitted from a function returning void, if it is the last statement. Return has 2 uses - terminate the execution of the function and specify the value returned to a caller.
The standard draft says:
- A return statement terminates execution of the current function and returns control to its caller. A function may have any number of return statements.
Any here is taken to mean that both functions returning a value or functions that do not return a value (returning void
) are allowed to have no return
statements.
The 6.9.1 in the draft on function declarations says:
- If the
}
that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined
Thus omitting a return statement is undefined behaviour if the function returns a value (not void
), and the caller uses the value. (As an exception the standard also says that it is specified behaviour that omitting a return
statement in main()
is now equivalent to returning 0).
Upvotes: 9
Reputation: 310990
According to the C Standard (6.7.6.3 Function declarators (including prototypes)
- The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.
- An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.145)
Footnote 145 refers to "Future directions §6.11.6" and "The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature."
Thus declaration
void f( void );
means that the function has no parameters.
Declaration
void f();
means that the number and types of parameters are unknown.
Declaration that at the same time is a definition
void f()
{
}
means that the function does not have parameters.
As for the return statement in a function that has return type void
then if it is the last statement of the function then usually it is omitted. Using the last return statement can only confuse the reader of the code because he has to be sure that the absence of an expression in the return statement is not a typo.
Upvotes: 7
Reputation: 4349
The only real reason to use return
for a void
function is to exit the function early. A return value of 0
is assumed for main
without a return
according to C99 §5.1.2.2.3 Program termination:
reaching the } that terminates the main function returns a value of 0
But it is always better to specify exactly what value is intended to be returned.
In response to cool guys comment the following code compiles using gcc
with the flags -std=c89
, -std=c99
and -std=c11
:
#include <stdio.h>
int main(void) {
printf("Hello world\n");
}
As for void
as a parameter that is necessary to inform the C compiler that you are using the new function declaration style and not the old style. It technically can be omitted but use caution, void
is the preferred method.
Upvotes: 4
Reputation: 4023
When using void
as a return type, it is not necessary to write the return
statement, as essentially void implies not returning anything. However, you can use return
if you want to explicitly exit from the function.
void
as a parameter is not really mandatory, it only signifies that no parameters are being passed.
Upvotes: 4