Do I need to use "void" parameter?

Programming, for all ages and all languages.
Post Reply
mac
Member
Member
Posts: 144
Joined: Tue Sep 23, 2014 6:12 pm

Do I need to use "void" parameter?

Post 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?
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

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

Post 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?
User avatar
matt11235
Member
Member
Posts: 286
Joined: Tue Aug 02, 2016 1:52 pm
Location: East Riding of Yorkshire, UK

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

Post 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.
com.sun.java.swing.plaf.nimbus.InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonWindowNotFocusedState
Compiler Development Forum
mac
Member
Member
Posts: 144
Joined: Tue Sep 23, 2014 6:12 pm

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

Post 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' "
User avatar
Geri
Member
Member
Posts: 442
Joined: Sun Jul 14, 2013 6:01 pm

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

Post by Geri »

you dont have to use it
Operating system for SUBLEQ cpu architecture:
http://users.atw.hu/gerigeri/DawnOS/download.html
User avatar
Sik
Member
Member
Posts: 251
Joined: Wed Aug 17, 2016 4:55 am

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

Post 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.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

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

Post 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.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
obiwac
Member
Member
Posts: 149
Joined: Fri Jan 27, 2017 12:15 pm
Libera.chat IRC: obiwac
Location: Belgium

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

Post 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.
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

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

Post 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.
User avatar
obiwac
Member
Member
Posts: 149
Joined: Fri Jan 27, 2017 12:15 pm
Libera.chat IRC: obiwac
Location: Belgium

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

Post by obiwac »

Oh ok.
Post Reply