Showing posts with label stack. Show all posts
Showing posts with label stack. Show all posts

20130411

How to learn stack and flow of function call and control in cpp

/*How to learn stack. Stack is memory in which Last in first out is followed.
When function call is encountered the address of the next statement is stored or pushed in the stack. When the control returns back from the definition to calling function the address is poped out which is nothing but the statement next to the function call and executed.*/
#include<stdio.h>
#include<iostream>
using namespace std;
void function1();
void function2();

int main()
{
printf("1\n");
printf("2\n");
function1();
printf("3\n");
printf("4\n");

return 0;
}
void function1()
{
printf("1.1\n");
printf("1.2\n");
function2();
printf("1.3\n");
printf("1.4\n");
}
void function2()
{
printf("2.1\n");
printf("2.2\n");
}

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
}