mercredi 1 juillet 2015

Why is the value of "C" changing in the following code?

the output I'm getting for the given code is "0" even though I initialized the value of c as "1"...can somebody explain it... Why is the value of "C" changing in the following code??

#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;
}

Aucun commentaire:

Enregistrer un commentaire