为什么“C”的值在以下代码中发生变化?

时间:2015-07-01 18:28:06

标签: c++ c function recursion

  

即使我,我得到的给定代码的输出为“0”   将c的值初始化为“1”......有人可以解释一下......   为什么“C”的值在以下代码中发生变化?

#include <iostream>

using namespace std;

int c=1; // global initialized 'c' as 1..

long long f(long long n){

    if(n==6){

        return 2;
    }

    else{   

        c=c+1;

        f(n-2);

    }

}
int main()
{
    long long n,ans,p;
    cin>>n;

    ans=f((2*n)-2);

    cout<<c; //printing out the value of 'c'
    return 0;
}

1 个答案:

答案 0 :(得分:0)

因为c的值在以下代码中被更改:

else{       
        c=c+1;    
        f(n-2);    
    }