   Quick Question
  I didn't know this was a valid syntax. Why is this working :int foo(int i){  if(!i) return; //ok  else return foo(i/2); // ?}Can someone explain this logically. I would have thought that the second returnstatement would have caused an error.   
int foo(int i){  if(!i) return; // not ok - foo must return a value of type 'int'  else return foo(i/2); // this is called 'recursion' - where a function calls itself (usually due to some condition, otherwise then it will create a form of infinite loop.}Basically the function will just keep calling itself until 'i' == 0, then you are supposed to return some value of type int (presumably i). Though that function is pretty useless because it will always return 0, but im guessing it wasnt intended to have a purpose.Im not sure but i think the statement:else return foo(i/2);could be just:else foo(i/2);but I cant seem to wrap my head around the idea of that return actually having an effect.. weird..Anyway yeah, kthxbye   ;  CodeCriminal is mostly right. He is wrong about the second return statement not being necessary. To explain why I'll create 3 functions that with different names (i'll give a default return value of 10 to make things interesting):int Foo1(int i)  if(!i) return 10;  else return Foo2(i/2);}int Foo2(int i)  if(!i) return 10;  else return Foo3(i/2);}int Foo3(int i)  if(!i) return 10;  else return FooX(i/2);}int main(){  std::cout << Foo1(2) << std::endl;}and step through the logic of those functions:1. Foo1(2) calls Foo2(1)2. Foo2(1) calls Foo3(0)3. Foo3 returns 104. Foo2 returns Foo3(3) aka 105. Foo1 returns Foo2(2) aka Foo3(1) aka 106. cout prints Foo1(3) aka Foo2(2) aka Foo1(1) aka 10If the 2nd return statement didn't exist lines 4 and 5 wouldn't work. Also, unless you run out of stack (I doubt it) your function will always return the same value. So it isn't a very interesting recursive function. A more interesting recursive function is:int fibonacci(int i){    if(i <= 1) return 1;    return fibonacci(i-1) + fibonacci(i-2);}See if you can trace how the function behaves!   ;  Oh wait, I'm sorry. I made a type. This should be the function :void foo(int i){  if(!i) return; //ok  else return foo(i/2); // ?}The function should be of type void, not int.   ;Quote:Original post by ConcentrateOh wait, I'm sorry. I made a type. This should be the function :void foo(int i){  if(!i) return; //ok  else return foo(i/2); // ?}The function should be of type void, not int.The answer is the same: recursion. It's just an even more useless function than I thought earlier. If foo printed something it would be less useless. Try this:void foo(int i){  if(!i) {    std::cout << i << std::endl; return; }  else {    int x = foo(i/2);    std::cout << x << std::endl; return;  }}I believe it will print the numbers from 0 to x if you call Foo(x);   ;  I know what a recursion function is, does, and how it works. I am not askinganything about it. I am sorry for not being clear. What I mean was that in thisrandom function that I made up :void foo(int i){ if(i == 0) return; else return foo(i/10);}that has no purpose except as a demonstration, why is the second return a validsyntax. Specifically, here " "return" foo(i/10)". Without the return it would bejust a recursive function call. But that return threw me off. Since foo is a function of type void. Why is return foo(i/10) valid ? Is the return statementthere optional, because foo is of type void? Sorry if I'm still not clear.;  I'm no expert but foo has a return type of void, so why should it be a problem?   ;Quote:Original post by ConcentrateI am sorry for not being clear. What I mean was that in thisrandom function that I made up :void foo(int i){ if(i == 0) return; else return foo(i/10);}that has no purpose except as a demonstration, why is the second return a validsyntax.I'll answer your question with another question: why should the language specification specifically disallow returning void?   ;Quote:Original post by nobodynewsQuote:Original post by ConcentrateI am sorry for not being clear. What I mean was that in thisrandom function that I made up :void foo(int i){ if(i == 0) return; else return foo(i/10);}that has no purpose except as a demonstration, why is the second return a validsyntax.I'll answer your question with another question: why should the language specification specifically disallow returning void?It shouldn't. But the function is of type void so it shouldn't return anything.Its been a long day. I just need some sleep.