I'm writing a rather simple program following Euclidean algorithm (we have to write it using recursion). It works really well when executed in C++ Tutor but when I compile the program in the terminal, it already gives me:
warning: control may reach end of non-void function [-Wreturn-type]
When I try to execute it in the terminal, it throws:
runtime error: execution reached the end of a value-returning function without returning a value
(But I am returning a value?)
Why does it work with c++ tutor but not in the Linux terminal (using clang compiler)?
I tried to use a bunch of extra variables in the function to make the process clearer to me, but I still don't get why it thinks that there would be a case where I would not return a value.
#include <iostream>
using namespace std;
int ggt(int a, int b){
int rest{0};
int zaehler{0};
int divisor{0};
if(a>=b){
zaehler=a;
divisor=b;
if(a%b==0){
return b;
}
else{
rest=a%b;
divisor=rest;
zaehler=b;
ggt(zaehler, divisor);
}
}
else{
zaehler=b;
divisor=a;
if(b%a==0){
return a;
}
else{
rest=b%a;
divisor=rest;
zaehler=a;
::durchlaeufe--;
ggt(zaehler, divisor);
}
}
}
int main(){
int a{40}, b{12};
cout << "Bitte Zaehler eingeben: ";
cin >> a;
cout << "\n";
cout << "Bitte Nenner eingeben: ";
cin >> b;
cout << "\n";
if(ggt(a, b)==0){
cout << "ERROR\n";
}
else {
cout << "Der groesste gemeinsame Teiler ist: " << ggt(a, b) << "\n";
}
return 0;
}
In this example, with a=40 and b=12, the result should be 4. And that's exactly what C++ tutor says...