Why put void in params?

前端 未结 6 1024
别跟我提以往
别跟我提以往 2020-12-08 19:33

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

Why not just leave it blank?

void createLevel(void);

void createLevel();
相关标签:
6条回答
  • 2020-12-08 20:03

    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.

    0 讨论(0)
  • 2020-12-08 20:18

    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.

    0 讨论(0)
  • 2020-12-08 20:19

    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:~$
    
    0 讨论(0)
  • 2020-12-08 20:23

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

    0 讨论(0)
  • 2020-12-08 20:28

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

    0 讨论(0)
  • 2020-12-08 20:29

    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.

    0 讨论(0)
提交回复
热议问题