//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;
}
}
내 재귀 함수 호출에서 "return"키워드를 사용하지 않으면 프로그램에서 잘못된 값을 반환하지만 함수를 단계별로 살펴보면 다음과 같이 나타납니다. 로컬의 값이 올바르게 갱신됩니다. 그 함수들이 (void 타입이 아닌 한) 값을 반환해야한다는 것을 알고 있습니다. 아마도이 규칙은 재귀 함수 호출에도 적용될 수 있을까요?기본 C++ 재귀 프로그램 질문
누군가가 정교하게 설명해 주시겠습니까?
코드를 올바르게 포맷하십시오. 그것을 표시하고 Ctrl + K를 누르십시오. – mmmmmmmm
@ 닉 D : 감사합니다 :-) – mmmmmmmm