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?
Printable View
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...Quote:
it's a specifier for a function that doesn't return a value.
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.
*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.
It makes me worry about your learning process.Quote:
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?
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;
}
}
So you worry about his learning process because he hasn't learned everything yet?Quote:
Originally Posted by siavoshkc
No, I think by reading a tutorial (Valid One) correctly, he/she should not think void is function. If so, something is wrong.Quote:
So you worry about his learning process because he hasn't learned everything yet?
Old C programmers never die, they simply get cast into a void ;)