20130411

How to learn recursive function call in cpp/C++/C

/*How to learn recursive function call in cpp/C++/C
 Cpp Program to learn recursive function */
#include<iostream>
using namespace std;
int sum(int m, int k);
int main()
{
int i,m,k;
m=3;
k=2;
i=sum(m,k);
cout<<"sum="<<i<<endl;
//return 0;
}
int sum(int rm, int rk)
{
cout<<rm<<" "<<rk<<endl;
if(rm==0) return(rk);//termination condition
else return(1+sum(rk, rm-1));//recursive condition//if not +1 then sum=0
}

/*
3 2
2 2
2 1
1 1
1 0
0 0
sum=5 //in stack first address of sum(3,2) was stored
*/
Previous                             Home                               Next

1 comment:

  1. for details one can refer to
    learncppdotcom
    and
    linuxforudotcom

    ReplyDelete