OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 5:48 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Do I need to use "void" parameter?
PostPosted: Sun Apr 30, 2017 1:50 pm 
Offline
Member
Member

Joined: Tue Sep 23, 2014 6:12 pm
Posts: 144
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?


Top
 Profile  
 
 Post subject: Re: Do I need to use "void" parameter?
PostPosted: Sun Apr 30, 2017 2:02 pm 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
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?


Top
 Profile  
 
 Post subject: Re: Do I need to use "void" parameter?
PostPosted: Sun Apr 30, 2017 2:39 pm 
Offline
Member
Member
User avatar

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


Top
 Profile  
 
 Post subject: Re: Do I need to use "void" parameter?
PostPosted: Sun Apr 30, 2017 3:07 pm 
Offline
Member
Member

Joined: Tue Sep 23, 2014 6:12 pm
Posts: 144
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' "


Top
 Profile  
 
 Post subject: Re: Do I need to use "void" parameter?
PostPosted: Sun Apr 30, 2017 3:59 pm 
Offline
Member
Member
User avatar

Joined: Sun Jul 14, 2013 6:01 pm
Posts: 442
you dont have to use it

_________________
Operating system for SUBLEQ cpu architecture:
http://users.atw.hu/gerigeri/DawnOS/download.html


Top
 Profile  
 
 Post subject: Re: Do I need to use "void" parameter?
PostPosted: Sun Apr 30, 2017 6:16 pm 
Offline
Member
Member
User avatar

Joined: Wed Aug 17, 2016 4:55 am
Posts: 251
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.


Top
 Profile  
 
 Post subject: Re: Do I need to use "void" parameter?
PostPosted: Sun Apr 30, 2017 9:29 pm 
Offline
Member
Member
User avatar

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

_________________
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]


Top
 Profile  
 
 Post subject: Re: Do I need to use "void" parameter?
PostPosted: Tue May 02, 2017 1:33 pm 
Offline
Member
Member
User avatar

Joined: Fri Jan 27, 2017 12:15 pm
Posts: 149
Location: Belgium
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.

_________________
AQUA OS: https://obiwac.wordpress.com/aqua-os/


Top
 Profile  
 
 Post subject: Re: Do I need to use "void" parameter?
PostPosted: Tue May 02, 2017 1:51 pm 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
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.


Top
 Profile  
 
 Post subject: Re: Do I need to use "void" parameter?
PostPosted: Tue May 02, 2017 2:21 pm 
Offline
Member
Member
User avatar

Joined: Fri Jan 27, 2017 12:15 pm
Posts: 149
Location: Belgium
Oh ok.

_________________
AQUA OS: https://obiwac.wordpress.com/aqua-os/


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 34 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group