大约几分钟前,我刚刚发现了递归函数。我一直在玩它们,现在我从以下函数中获得了不同的输出:
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;
}
为此,我得到输出:
In fm = 4
In fm = 8
In fm = 16
In fm = 32
function(a) = 32
In f2 n = 4
In f2 n = 8
In f2 n = 16
In f2 n = 32
function2(a) = 4
他们不应该都产生结果32
吗?