I just found out about recursive functions about a few minutes back. I was playing around with them and now I am getting different outputs from the following functions:
int function(int m) {
m = 2*m;
std::cout<<"In f m = "<<m<<std::endl;
if(m > 20)
{
return m;
}
function(m);
};
int function2(int n) {
n = 2*n;
std::cout<<"In f2 n = "<<n<<std::endl;
if(n < 20)
{
function2(n);
}
return n;
};
int main() {
int a = 2;
std::cout <<"function(a) = "<<function(a)<<std::endl;
std::cout <<"function2(a) = "<<function2(a);
return 1;
}
To this I get the output:
In f m = 4
In f m = 8
In f m = 16
In f m = 32
function(a) = 32
In f2 n = 4
In f2 n = 8
In f2 n = 16
In f2 n = 32
function2(a) = 4
Shouldn't they both yield result of 32?