//This program finds the GCD of two numbers using a recursive function call via the
//Euclidean algorithm
#include <iostream>
#include <cmath>
using namespace std;
int GCD (int A, int B);
int main()
{
int A = 45, B = 55;
cout << "The GCD is " << GCD(A,B) << endl;
//test
return 0;
}
int GCD (int A, int B)
{
A = abs(A);
B = abs(B);
if (A > B)
{
A = A - B;
return GCD (A, B); //Recursive function call - works fine
//GCD (A, B); -- This function call seems to return an incorrect value
else if (A < B)
{
B = B - A;
return GCD (A, B);//Recursive function call
//GCD (A, B); -- This function call seems to return an incorrect value
}
else if (A = B)
{
return A;
}
}
Here's my question: I've noticed that if I dont use the "return" keyword in my recursive function call, the program returns an incorrect value, but if I step through the function, the values in local correctly update. I know that functions (unless they are of the type void) must return a value. Perhaps this rule applies in recursive function calls as well?
Could someone please elaborate/help me understand?