38

What's the reason for putting void inside of the params?

Why not just leave it blank?

void createLevel(void);

void createLevel();
01F0
  • 1,228
  • 2
  • 19
  • 32
RoR
  • 15,934
  • 22
  • 71
  • 92

6 Answers6

49

The void in the parenthesis are from C. In C a function with empty parentheses could have any number of parameters. In C++ it doesn't make any difference.

halfdan
  • 33,545
  • 8
  • 78
  • 87
18

void in function argument lists is a relict of the past (C). In C++, you should leave the parentheses empty. Of course you can keep the void if it makes you happy.

In C, if you declare a function with empty parentheses, the meaning is that the number of parameters is unknown. void can be used to make it explicit that no parameters are expected.

Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122
13

In C++ there is no difference.

The following applies only to C:

Actually, according to this thread:

when you declare somewhere a function func(), this means you don't say anything about it's aguments. On the otherhand func(void) means NO ARGUMENTS

perfect_circle even posted a wonderful code example to illustrate the point:

skalkoto@darkstar:~$ cat code.c
#include <stdio.h>

int main()
{
        void func(void);
        func(3);
return 0;
}

void func(int a)
{
        printf("Nothing\n");
}
skalkoto@darkstar:~$ gcc code.c
code.c: In function `main':
code.c:6: error: too many arguments to function `func'
skalkoto@darkstar:~$ cat code1.c
#include <stdio.h>

int main()
{
        void func();
        func(3);
        return 0;
}

void func(int a)
{
        printf("Nothing\n");
}
skalkoto@darkstar:~$ gcc code1.c
skalkoto@darkstar:~$ ./a.out
Nothing
skalkoto@darkstar:~$
Rado
  • 8,634
  • 7
  • 31
  • 44
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
3

There's no difference, this is just down to personal preference, e.g. to show yourself that when designing the function you didn't forget to give params.

AVH
  • 11,349
  • 4
  • 34
  • 43
-1

It's a preferential thing. Some people prefer to make things explicit instead of implicit. There is no practical difference between the two.

mikehabibi
  • 137
  • 3
  • 2
    I wouldn't call an empty parameter list "implicit". It's not as if having no parameters is a special default behavior that kicks in when you, erm, list no parameters. Seems to me more like a preference for noisy over quiet ;-) – Steve Jessop Apr 07 '11 at 20:36
-1

Only put VOID void in params if you're old school (I do)

SQLMason
  • 3,275
  • 1
  • 30
  • 40