Skip to main content
gamedev.net
🔒 Locked

Returning void

Started by Ironica Jul 16, 2003 at 9:07 AM 14 replies 1.2k views
Original Post
Ironica
Ironica
Hmm... I just tryed this, and it seems to compile fine. Would it actually work as I'd expect?
void func(){
 return;
}
  
How about if I had something like:
void func(int v){
 if(v == 1)
  return;
 cout << v;
}
  
would that work fine? (I can't test it out right now.) I mean like print the number, unless it's equal to 1. If so, that's weird! I thought you couldn't return from a void function!
Crispy
Crispy
That''s a perfectly valid piece of code. You can''t return a value from a function returning void.
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Yanroy
Yanroy
It works fine under MSVC 6. I do it all the time when I''m coding (largely because I''m a bad boy and I like to use void main() instead of int main()). I''ve never had a single problem with it. And it functions just like you''d expect with returning an actual value, except, obviously, nothing is returned Maybe this isn''t ANSI compliant...

--------------------


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
Ironica
Ironica
quote:
Original post by Crispy
You can''t return a value from a function returning void.

Right. You can return from a void function, but you can''t return a value. Makes sense =)
Sneftel
Sneftel
sure ya can. In fact, you can do this:


template<typename T> T func()
{
return T();
}

Now, that''s not especially interesting, until you consider this:

func<void>();

Check it out! Not only are you returning from a void function, you are actually constructing and returning a variable of type void! Megacool!
JuNC
JuNC
Although that is a toy example, is void an actual value (i.e. the single value of type void) in C++ like, say, unit would be in ML? (just interested to know what the standard says about that)
Sneftel
Sneftel
quote:
Original post by JuNC
Although that is a toy example, is void an actual value (i.e. the single value of type void) in C++ like, say, unit would be in ML? (just interested to know what the standard says about that)

Nope, it isn''t (except where it exists as void*). This feature is just to make templates a little more generic (I believe there''s a couple of STL helper classes that make use of it).
JuNC
JuNC
Actually, I just tested it with VC++6 & gcc 2.95.2

VC barfs, ''function returning void''

gcc no problem

Is gcc correct?
Sneftel
Sneftel
Yep, gcc is correct. This is another one of those VC6 incompatibilities. I assume it''s fixed in .NET.
craphead
craphead
gcc came first. 3.x sux stick with the old one. you cant use fsstream properly with 3.x.
50/50 Robotics and Software
JuNC
JuNC
/me rolls eyes - typical!

Oh well, I can't imagine I'll ever find it a problem

EDIT: I was replying to Sneftel, gcc 3.x rocks

[edited by - JuNC on July 16, 2003 11:15:24 AM]
Sneftel
Sneftel
quote:
Original post by craphead
gcc came first. 3.x sux stick with the old one. you cant use fsstream properly with 3.x.

You''re not serious, are you? gcc 3.x has vastly better optimization. Perhaps your problems stem from the fact that there IS no "fsstream"?
Vich
Vich
GCC 3.x fixed a problem over 2.9x (dunno what precisely) that was very crucial and took me weeks to find it out:

When I started a server application, and made a thread for every person that connected, I wanted to use ''string'' or a std list. Both crashed in version 2.9x and worked perfectly in 3.x

"My basic needs in life are food, love and a C++ compiler"
[Project AlterNova] [Novanet]
[www.LifeIsDigital.net - My open source projects and articles.
Ironica
Ironica
quote:
Original post by Sneftel
Yep, gcc is correct. This is another one of those VC6 incompatibilities. I assume it''s fixed in .NET.


I was using VC++.NET, so yes.
TSwitch
TSwitch
quote:
Original post by Crispy
That's a perfectly valid piece of code. You can't return a value from a function returning void.
I did, once. But now there's a blue swirly vortex on the wall behind me and every now and then a person wearing some sort of alien space suit pokes his head through, says something about a parallel dimension and then that's when I fire off a couple shotgun shells in his direction. I got a piece of one, once, but someone dragged him back into the swirly dealie before I could get a knife to cut off his head and mount it on the wall.



[edited by - TSwitch on July 16, 2003 5:50:05 PM]
Member of the Unban nes8bit or the White Rhino in my Basement Gets Sold to the Highest Bidder Association (UNWRBGSHBA - Not accepting new members.)Member of the I'm Glad Mithrandir Finally Found an Association that Accepts People with his Past History Association (IGMFFAAPPHA)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.