Hi
I have a fuction starting like: void print ()..., what is the void keyword there for?
Hi
I have a fuction starting like: void print ()..., what is the void keyword there for?
It tells the compiler that your function doesn't return anything.
If it were "int print()", then your function would be returning an int.
I find that it's best to return a value to indicate success, so that the code that called the function knows how to proceed. This is the function template I use:
This can make debugging easier by allowing you to quickly determine which functions cause the error. Thus, development is sped up.Code:BOOL DoSomething() { BOOL bSuccess=false; //once the function has successfully completed its task: bSuccess=true; return bSuccess; }
[email protected]
Microsoft Visual Studio .NET 2003 Enterprise Architect
Windows XP Pro
Code Tags
Programming FAQ
Tutorials
yeah, for some reason I cringe when i see void anything... not just void main... i don't like the void keyword unless it's in the parameters:Code:bool somefunction(void);
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
Lol, what are you, a C programmer ;-)
You can also use try..catch blocks if your function doesnt return anything:
Although my syntax might not be right since I don't have a compiler with meCode:try { somefunction(); } catch (const Error&) { //... } //... void somefunction() { throw (Error); }
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
Error would have to be a valid type obviously (it'll most likely be a typedef) but it looks all right.Originally posted by JaWiB
Although my syntax might not be right since I don't have a compiler with me
SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
I say what I say, I mean what I mean.
IDE: emacs + make + gcc and proud of it.
You blokes are funny. If I have something, like a print method, I would cringe doing something like:Originally posted by major_small
yeah, for some reason I cringe when i see void anything... not just void main... i don't like the void keyword unless it's in the parameters:Code:bool somefunction(void);
I don't like the idea of a function returning the same value every time it's called. I mean if it is never going to be checked, then it has no purpose, it has no "why". But of course that's just me.Code:bool blah::print() const { cout << data << endl; return true; }
Yeah it seems kind of stupid. However, that is not what occurs in my functions. I return bSuccess=true when the function is successful. I return bSuccess=false when the function is not successful. I always check the return values from my functions. Thus, it is very useful.I don't like the idea of a function returning the same value every time it's called.
Being pedantic, and sticking to the standard, is a good character trait. It (helps) to ensure perfection, and reduces development time. Having a good programming ethic will pay off when you are developing something on a huge scale, because you will avoid stupid petty small errors.You blokes are funny.
I'm not saying that BOOL DoSomething() is going to destroy your computer, but sticking to the standard, and writing 'void' where there are no parameters, will encourage you to write correct code ALL the time.
[email protected]
Microsoft Visual Studio .NET 2003 Enterprise Architect
Windows XP Pro
Code Tags
Programming FAQ
Tutorials
In this example, why not return the value from cout itself, that way the caller can verify that cout actually worked.Code:bool blah::print() const { cout << data << endl; return true; }
For example:
Code:bool print(char *data, ofstream &os) { return os << data << endl; }
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]