6

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    When you are using void as function return type then you don't need to return any value. In other words if you want to return some thing then you should have to use specific datatype – UFFAN AHMED Mar 29 '15 at 06:03
  • 1
    When `void` is used, it means that there is no value that is to be returned. So you wont have to return any **value** , `return;` is as good as returning nothing. when you do not mention `void`, in some compilers the default is taken as `int` therefore you will have to `return` something. – bayblade567 Mar 29 '15 at 06:03
  • As for the second half it depends on the standard you are using. Omitting the `void` as the parameter defaults to the old style C declarations/ – carloabelli Mar 29 '15 at 06:05
  • 2
    Answer to the second part: http://stackoverflow.com/questions/693788/c-void-arguments – Spikatrix Mar 29 '15 at 06:15
  • FWIW: I use the `void func(void)` with an explicit `(void)` in both function declarations and function definitions. It is necessary in function declarations; omitting the `void` means that you've not specified anything about the number and types of the parameters except that the function is not a varargs function, so you must include `(void)` to tell the compiler to reject calls with arguments. Because it is necessary in the declaration, I use it in the definition too, primarily for consistency and secondarily because the compiler options I use (GCC with `-Wstrict-prototypes`) require it. – Jonathan Leffler Mar 29 '15 at 06:49
  • 1
    This is asking two different questions, you should really edit this to ask one question and start a new question for the other question. (Although the one about `void` in the parameter list already has an answer [here](http://stackoverflow.com/questions/693788/c-void-arguments), so don't actually ask that again) – M.M Mar 29 '15 at 07:05

4 Answers4

8

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

  1. 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:

  1. 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:

  1. 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).

  • 1
    Regarding " it is not an error to call void func(); with arguments," : it causes undefined behaviour, so it is an error. However the compiler is not required to diagnose the problem, which is why it is a bad idea to omit `void`. – M.M Mar 29 '15 at 07:04
7

According to the C Standard (6.7.6.3 Function declarators (including prototypes)

  1. The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.

  1. 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.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 3
    Note that a function declared `void f();` does indeed mean that the number and types of the parameters are unknown, but the number is fixed — the function cannot be a varargs function using ellipsis `...` in the argument list. – Jonathan Leffler Mar 29 '15 at 06:40
  • 1
    And the relevant part of the standard for the 'cannot be a varargs function' is §6.5.2.6 Function calls, ¶6: _If the expression that denotes the called function has a type that does not include a prototype, … If the function is defined with a type that includes a prototype, and either the prototype ends with an ellipsis (`, ...`) or …, the behavior is undefined._ – Jonathan Leffler Mar 29 '15 at 07:02
  • @Vlad "As for the return statement...", can you clarify, exemplify, this paragraph? What is the confusion? I'm a noob. – Santropedro Jul 09 '19 at 02:58
  • @Santropedro A function with the return type void can be executed until the closing brace } is encountered. – Vlad from Moscow Jul 09 '19 at 07:58
4

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.

Ayushi Jha
  • 4,003
  • 3
  • 26
  • 43
4

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.

carloabelli
  • 4,289
  • 3
  • 43
  • 70
  • "`return` can sometimes be omitted, but it is always better to specify exactly what value is intended to be returned." — no, this is an error. What you get is not a random value, it is undefined behavior. It could crash. – Potatoswatter Mar 29 '15 at 06:15
  • @Potatoswatter I was specifically referencing the `main` function for which it can indeed be omitted – carloabelli Mar 29 '15 at 06:17
  • @cabellicar123 , Can the return statement be omitted for C89,C99 and C11 for `main` ? – Spikatrix Mar 29 '15 at 06:20
  • @cabellicar123 ,I tried the code with GCC. It emits this warning when I compile using `-std=c89`: `In function 'main': warning: control reaches end of non-void function [-Wreturn-type] }` and does not give out any warning when I use C99 or C11. So I guess that the `return` statement is required in C89. – Spikatrix Mar 29 '15 at 06:32
  • 1
    @CoolGuy: In C99 and C11, to match the decision made by C++98, it is possible to leave the `return` off the end of `main()`. In ISO/IEC 9899:2011, §5.1.2.2.3 **Program termination** _If the return type of the `main` function is a type compatible with `int`, a return from the initial call to the `main` function is equivalent to calling the `exit` function with the value returned by the `main` function as its argument; reaching the `}` that terminates the `main` function returns a value of 0._ C89/90 did not include the 'reaching the `}`' clause. – Jonathan Leffler Mar 29 '15 at 06:33
  • @cabellicar123 At the time I commented, you weren't, and since `main` can't return `void` (in any version of standard C), it's not relevant to the question. – Potatoswatter Mar 29 '15 at 06:34
  • @Potatoswatter The question was *It is mandatory to use "return" at the end of each function or not?* I believe it is relevant as the OP makes no indication of a designated return type. `void` is only in his example – carloabelli Mar 29 '15 at 06:35
  • @JonathanLeffler Strange that the code still compiles with the `-std=c89` flag. I would assume an error, but it must be compiler specific. – carloabelli Mar 29 '15 at 06:43
  • No (it is not strange that it compiles without a diagnostic). It is undefined behaviour, and no diagnostic is required for undefined behaviour. – Jonathan Leffler Mar 29 '15 at 06:44