Page 1 of 1

Do I need to use "void" parameter?

Posted: Sun Apr 30, 2017 1:50 pm
by mac
I'm reading a chapter of my book, but in the examples it lists

Code: Select all

int variable(void)
instead of omitting the parameter when there is none.

Should this be redundant? Should I follow it anyway since it's written in the book?

Re: Do I need to use "void" parameter?

Posted: Sun Apr 30, 2017 2:02 pm
by iansjack
Try compiling this snippet of code:

Code: Select all

void test()
{
};

void test2(void)
{
};

int main(void)
{
        test(3);
        test2(3);
}
What error(s) do you get? Can you see why it might be better to use the "void" declaration?

Re: Do I need to use "void" parameter?

Posted: Sun Apr 30, 2017 2:39 pm
by matt11235
If you don't specify any parameters, it will allow you to call the method with anything as a parameter.
By putting void, you're telling it not to do that.

Re: Do I need to use "void" parameter?

Posted: Sun Apr 30, 2017 3:07 pm
by mac
iansjack wrote:Try compiling this snippet of code:
...
What error(s) do you get? Can you see why it might be better to use the "void" declaration?
Yes, I definitely see that.

Output:
"error: too many arguments to function 'test2' "

Re: Do I need to use "void" parameter?

Posted: Sun Apr 30, 2017 3:59 pm
by Geri
you dont have to use it

Re: Do I need to use "void" parameter?

Posted: Sun Apr 30, 2017 6:16 pm
by Sik
I can definitely see where the confusion comes from.

For function declarations in C, leaving nothing between parenthesis tells the compiler to not bother checking, meaning it'll take anything (bad idea!), so you need to pass void to explicitly tell the compiler that it takes nothing instead. I don't remember if newer versions of the language change this behavior to be like C++'s (see below).

In C++, leaving the parenthesis empty means exactly not taking any arguments (as if you had passed void).

In other words: welcome to some legacy quirk from old language implementations that stuck around for newer ones.

EDIT: tl;dr include it to be safe.

Re: Do I need to use "void" parameter?

Posted: Sun Apr 30, 2017 9:29 pm
by Love4Boobies
That "void" isn't a parameter. At any rate, what you're looking at are old-style declarations. Before C89 got standardized, C used to only work like this:

Code: Select all

void foo();

void foo(x, y)
int x;
char y;
{
    // bla bla bla
}
Although they're still around so as not to break legacy code, you don't want to use them because your arguments will have no type checking.

Re: Do I need to use "void" parameter?

Posted: Tue May 02, 2017 1:33 pm
by obiwac
Sik wrote:I can definitely see where the confusion comes from.

For function declarations in C, leaving nothing between parenthesis tells the compiler to not bother checking, meaning it'll take anything (bad idea!), so you need to pass void to explicitly tell the compiler that it takes nothing instead. I don't remember if newer versions of the language change this behavior to be like C++'s (see below).

In C++, leaving the parenthesis empty means exactly not taking any arguments (as if you had passed void).

In other words: welcome to some legacy quirk from old language implementations that stuck around for newer ones.

EDIT: tl;dr include it to be safe.
Why is it a necessarily bad idea? It works either way.

Re: Do I need to use "void" parameter?

Posted: Tue May 02, 2017 1:51 pm
by iansjack
obiwac wrote:Why is it a necessarily bad idea? It works either way.
Because the two appear to mean the same, but actually have very different meanings.

Re: Do I need to use "void" parameter?

Posted: Tue May 02, 2017 2:21 pm
by obiwac
Oh ok.