OSDev.org
https://forum.osdev.org/

Do I need to use "void" parameter?
https://forum.osdev.org/viewtopic.php?f=13&t=31716
Page 1 of 1

Author:  mac [ Sun Apr 30, 2017 1:50 pm ]
Post subject:  Do I need to use "void" parameter?

I'm reading a chapter of my book, but in the examples it lists
Code:
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?

Author:  iansjack [ Sun Apr 30, 2017 2:02 pm ]
Post subject:  Re: Do I need to use "void" parameter?

Try compiling this snippet of code:
Code:
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?

Author:  matt11235 [ Sun Apr 30, 2017 2:39 pm ]
Post subject:  Re: Do I need to use "void" parameter?

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.

Author:  mac [ Sun Apr 30, 2017 3:07 pm ]
Post subject:  Re: Do I need to use "void" parameter?

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' "

Author:  Geri [ Sun Apr 30, 2017 3:59 pm ]
Post subject:  Re: Do I need to use "void" parameter?

you dont have to use it

Author:  Sik [ Sun Apr 30, 2017 6:16 pm ]
Post subject:  Re: Do I need to use "void" parameter?

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.

Author:  Love4Boobies [ Sun Apr 30, 2017 9:29 pm ]
Post subject:  Re: Do I need to use "void" parameter?

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

Author:  obiwac [ Tue May 02, 2017 1:33 pm ]
Post subject:  Re: Do I need to use "void" parameter?

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.

Author:  iansjack [ Tue May 02, 2017 1:51 pm ]
Post subject:  Re: Do I need to use "void" parameter?

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.

Author:  obiwac [ Tue May 02, 2017 2:21 pm ]
Post subject:  Re: Do I need to use "void" parameter?

Oh ok.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/