#include...
void dummyfunction (void){
cout << "This is a function!" << endl;
}
int main()
{
dummyfunction ();
return 0;
}
Okay.. now here''s my question. The example was used to express how you could use the void type to just return a message, without returning a value and how to receive no parameters.
I understand my confusion must either be that I''m just a stoopid newbie, or that the example just sucks.. but, if you wanted to return a message.. the logical thing to do would be to just use cout in the main, rather than create a whole function. So I guess I''m asking.. where else and how can "void" be used for effeciency?
I''ve got a feeling I''m missing something about functions here, that''s throwing me off completely and that I''m going to sound like a complete idiot. So.. just tell me if I''m missing the point and please try to answer my question above. Thanx.
// Chimaera
Functions
Okay.. through the Cplusplus.com tutorial, I''m working my way through C++.. and I know a sufficient amount. After taking awhile off studying to do other things, I went back and found that I had some reviewing to do on functions. I read back over the Functions chapter to refresh my memory. At the very end, there was an example of the void type. The example went somethin'' like this..
well lets say you needed to open files and read their contents into an array. And you have 5 files you need to do this for. You can either
A) open, read, and close 5 files in main (messy)
or
B) write a void function that will open and process one file
then just call that function 5 times from main
that is an example
"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
A) open, read, and close 5 files in main (messy)
or
B) write a void function that will open and process one file
then just call that function 5 times from main
that is an example
"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
It''s not a dumb question at all. A lot of times examples are given in a simplistic way, which sometimes makes it seem like something is used for no reason. Say you had a block of code 20 lines long, and you want to use that code 4 or 5 times in your program. Rather than write 80 or 100 lines of codes, you write just the one fucntion, and call it in various places. Sometimes that code may need parameters or return a value, sometimes not, it just depends on what your trying to do with your program.
Oh, examples are frequently useless, as the author forgets to explain enough about it. My favourite examples were always the ones about OOP. Five line demonstrations just didn''t cut it, because there''s no demonstrated benefit in not coding it procedurally. It''s only when you try to do something useful with it ...
As for the example above, doing stuff with files, that''s a very bad example of the use of a void function. You should be returning error codes - unless you throw exceptions, but not all errors are exceptional and should be handled.
Oh yeah, you all know that printf is not void, right? Quick, what does it return?
--
Get a stripper on your desktop!
As for the example above, doing stuff with files, that''s a very bad example of the use of a void function. You should be returning error codes - unless you throw exceptions, but not all errors are exceptional and should be handled.
Oh yeah, you all know that printf is not void, right? Quick, what does it return?
--
Get a stripper on your desktop!
I don''t think anyone''s quite answered your question here, so i''ll give it a go :
Yep, you''re right... that example does suck (in the sense it''s a waste of a function). The other''s have answered this bit ok though, but I think I need to make it clear that just because you have a block of code you want to use several times, doesn''t mean it should be of type ''void''; it could be of any type (the most appropriate). You may still want to check the return value etc. Sorry, I just got the feeling you aren''t fully comfortable with functions, so I needed to explain that bit...
Use void where you don''t care about the return type. I wouldn''t exactly say it''s any more "efficient" than checking for a return value myself (I always use int not void, for functions that simply return success / failure or otherwise).
int .
Insomnia
quote:
but, if you wanted to return a message.. the logical thing to do would be to just use cout in the main, rather than create a whole function.
Yep, you''re right... that example does suck (in the sense it''s a waste of a function). The other''s have answered this bit ok though, but I think I need to make it clear that just because you have a block of code you want to use several times, doesn''t mean it should be of type ''void''; it could be of any type (the most appropriate). You may still want to check the return value etc. Sorry, I just got the feeling you aren''t fully comfortable with functions, so I needed to explain that bit...
quote:
So I guess I''m asking.. where else and how can "void" be used for effeciency?
Use void where you don''t care about the return type. I wouldn''t exactly say it''s any more "efficient" than checking for a return value myself (I always use int not void, for functions that simply return success / failure or otherwise).
quote:
Oh yeah, you all know that printf is not void, right? Quick, what does it return?
int .
Insomnia
Insomnia
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement