mardi 3 mars 2015

recursion returning unexpected output


I think the output should be 4 7 Instead I am getting 6 6 4 7 since p1 = p2 i.e. 6 = 6 the cout statement should not be performed. Why am I seeing the 6 6 ?



using namespace std;

void test(int p1, int p2);

void main()
{
test(2, 8);
return ;
}


void test(int p1, int p2)
{
if (p1 != p2)
{
p1 = p1 + 2;
p2 = p2 - 1;
test(p1, p2);
cout << p1;
cout << p2;
}
}



Aucun commentaire:

Enregistrer un commentaire