What is the purpose of the void parameter in functions in the C language?

Asked

Viewed 2,762 times

14

The parameter void it’s just semantic or he does something I don’t know?

int main() {
    return 0;
}
int main(void) {
    return 0;
}
  • 1
  • 2

    @diegofm actually this does not have in question linked.

  • I had just read this question @diegofm and not the same thing

2 answers

13


In this case are definitions of functions. It defines that the function can only be called with any argument being passed. Without the void can call with passage of arguments.

Look no working in the ideone. And in the repl it.. Also put on the Github for future reference.

If they were statements, then it would be different. See below:

int funcao(void);
int funcao() {
    return 0;
}

int funcao2();
int funcao2(int x, int y) { //válido
    return 0;
}

I put in the Github for future reference.

In the declaration of the prototype void indicates that no parameter can be used in defining the function implementation.

Contrary to what many imagine and how it works in other languages the use of signing of function funcao() means only that no parameter is explicitly defined, giving the freedom to do as you wish.

The recommended C is always to use the void in the prototype declaration, if present. In C++ is not required, the language assumes that there will be no parameters.

Question that already speaks of specification.

  • According to the C99 standard, the definition of functions is not recommended because it is obsolete: ISO/IEC 9899 - 6.11.7 - Function Definitions: The use of Function Definitions with Separate Parameter Identifier and declaration lists (not prototype-format Parameter type and Identifier declarators) is an obsolescent Feature.

  • @Lacobus leaves the article link on the C99 standard

3

In C, when the signature of a function has only the parameter void means that such function does not receive any parameters when called.

There’s a big difference between int foobar(); and int foobar( void );. In the first case, the function was only declared and does not have a prototype, in the second case, the function was declared together with a prototype.

According to the C99 standard, the definition of functions is not recommended because it is obsolete, let’s see:

6.11.6 Function declarators

The use of Function declarators with Empty parentheses (not prototype-format Parameter type declarators) is an obsolescent Feature.

6.11.7 Future language Directions, Function declarators

The use of Function Definitions with Separate Parameter Dentifier and declaration lists (not prototype-format Parameter type and Identifier declarators) is an obsolescent Feature.

No need to use void when declaring/implementing a function that does not receive any parameters, however, it is a good practice that aims to optimize the readability and understanding of the code, facilitating the differentiation of when the function is being called and when it is being declared.

Declaration only:

int foobar(); /* Evite Declarar Funções Sem Protótipo! */

Statement and prototype:

int foobar( void ); /* Boa Prática! */

Definition/Implementation:

int foobar( void ){
    return 0;
}

Calling for:

n = foobar();

I hope I’ve helped!

  • I suspected from the beginning

Browser other questions tagged

You are not signed in. Login or sign up in order to post.