# Thread: gcd function

1. ## gcd function

As an exercise I had to write a recursive function that computes the greater common denominator between two numbers. Before the use of a few brain cells that made me write one good answer:
Code:
```int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}```
I wrote the same function as follows:
Code:
```int gcd(int a, int b)
{
int temp;

if (b != 0)
{
temp = b;
b = a % b;
a = temp;
gcd(a, b);
}
else
return a;
}```
Although the program worked correctly I can't understand one thing.
What happens to the functions that are expected to return a value (int) but they won't return any?
They do not seem to wrap up after gcd(a, b=0) returns the correct result to the program.

Thank you for your time to read my post !!!

2. They both always return something.

edit - Ah, I see you aren't returning where you should:
Code:
```	if (b != 0)
{
temp = b;
b = a % b;
a = temp;
return gcd(a, b);
}```

Quzah.

3. Yes but let's say in the way of the return value some functions won't return anything. For example gcd(24,16) will compute the values for a and b and then will call gcd(16,8) but will not arrive at the return point.
My question is, if the machine meet up with a function (during a recursive computation) that is expected to return something (an int in that case) but doesn't return anything, is ok with that?

4. Originally Posted by ardavirus
What happens to the functions that are expected to return a value (int) but they won't return any?
If a function that returns any type other than void returns in any manner other than by an explicit return statement, the behaviour is undefined.

That means anything is allowed to happen. The compiler is not required to diagnose an error. The program may crash when run. In practice, a common result is returning some junk value to the caller.

5. That means that the second function is wrong cause is subject to undefined behavior ?

Is it safe to generalize and say that recursive functions that won't wrap up is a bad practice ?

6. As Wernher von Braun said: "One test result is worth one thousand expert opinions."

A non void function always returns something, even if you don't have a return statement, but what it will return in that case is anybody's guess.
The fact that your function is recursive does not make a difference in this case. The recursion will eventually stop and the execution will eventually resume back at the caller of the recursive function, but the values propagated will probably be wrong.

Try the following code (to make von Braun happy):
Code:
```#include <stdio.h>

int f(int x){
if( x == -354 )
return 13;
}

int main(int argc, char **argv){
int ret_val;

ret_val = f(argc);
printf("f() returned: %d\n",ret_val);

return 0;
}```
BTW, if you use "-Wall" most compilers should warn you about the missing "return".
gcc 4.2 for example, says: "warning: control reaches end of non-void function" for the program above.

7. Originally Posted by ardavirus
Yes but let's say in the way of the return value some functions won't return anything. For example gcd(24,16) will compute the values for a and b and then will call gcd(16,8) but will not arrive at the return point.
Then your compiler should be complaining at you, telling you that you wrote your function wrong.

Quzah.

8. @anthonyd
Thank you very much for your analytical answer my friend. After this I can say that I own the question :-)

9. Originally Posted by ardavirus
That means that the second function is wrong cause is subject to undefined behavior ?
Yes.
Originally Posted by ardavirus
Is it safe to generalize and say that recursive functions that won't wrap up is a bad practice ?
I would say so, yes. Infinitely recursive functions will eventually consume more resources than the computer has available.

10. @grumpy
Thank you very much for the direct answers, you were very helpful !!!

@quzah
Thank you very much as well, the correction you put in my "erroneous" function cleared the fog much more.

This forum provides the best help a self-teaching novice like me can get, I am infinitely in debt.

11. @grumpy
Thank you very much for the direct answers, you were very helpful !!!

@quzah
Thank you very much as well, the correction you put in my "erroneous" function cleared the fog much more.

This forum provides the best help a self-teaching novice like me can get, I am infinitely in debt.

Popular pages Recent additions