Hi every1
I was wondering, when im reading the tutorials, i come accross th function "void" alot, can anyone explain to me what this function is and does?
Hi every1
I was wondering, when im reading the tutorials, i come accross th function "void" alot, can anyone explain to me what this function is and does?
it's a specifier for a function that doesn't return a value.
And that's why it needs to use return;, because it can't return anything...it's a specifier for a function that doesn't return a value.
Last edited by maxorator; 10-05-2006 at 10:35 AM.
"The Internet treats censorship as damage and routes around it." - John Gilmore
would it need to if it isn't doing any sort of recursion? I mean you can have a void function that doesn't use return;
recursion or not, id imagine it would need it. the computer wouldnt 'know' that the function is recursive.
i thought every function needed a return statement no matter what, except for 'int main' where it is implied.
Um, how does recursion factor into this? The language itself makes the return statement optional for functions that return void.
Last edited by laserlight; 10-05-2006 at 10:56 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
*sigh*
void is not a function. void is a type specifier, but a special one. void is the non-type. A pointer to void is one that points to an address without further type qualification. A function returning void returns nothing. In C, a function with only void in the parameter list takes no parameters. A variable cannot be of type void, because it would always be empty, which wouldn't make sense.
By the way, a function returning void does not need a return statement, except in some decades old, very weird compilers. Unless you want an early return.
laserlight, you forgot to modify result in the recursive branch. You need
result *= n;
after the recursive call.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
It makes me worry about your learning process.Hi every1
I was wondering, when im reading the tutorials, i come accross th function "void" alot, can anyone explain to me what this function is and does?
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
Sorry about that, I was not thinking properly (never ever tried writing recursive functions that return void, heh). After catching my mistake when I actually went to test it as something looked wrong to me, here's my fix:
Code:void factorial(unsigned int& result, unsigned int n) { if (n == 0) { result = 1; } else { factorial(result, n - 1); result *= n; } }
Last edited by laserlight; 10-05-2006 at 11:09 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
So you worry about his learning process because he hasn't learned everything yet?Originally Posted by siavoshkc
"The Internet treats censorship as damage and routes around it." - John Gilmore
No, I think by reading a tutorial (Valid One) correctly, he/she should not think void is function. If so, something is wrong.So you worry about his learning process because he hasn't learned everything yet?
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
Old C programmers never die, they simply get cast into a void
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.