15 April 2013

Reusing which i already have made (functions - Part 1)

Listen me this is a very important concept and if i were you ,i will never skip this stuff.Every body knows what is a function.If you dont know then read some book.Here i will give finishing to your understanding of function you can pack the frequently used code inside function and call that function whenever you need that code for eg
#include //defination of factorial function int factorial(int x){ int i,k=1; for(i=1;i<=x;++i) //computing factorial of x k = i*k; return k; }  int main() { int x=5,temp; temp=factorial(x); printf("%d",temp);// will output 120 on screen x=6 temp=factorial(x); printf("%d",temp); //will output 720 x=7 temp=factorial(x); printf("%d",temp); // will output 5040 }
//called function int func(int x,float y) // receiving input x,input y { printf("%d%f",x,y);//do some stuff there return k; //returning ouput k to the calling function } //calling function int main() { int x=5,int y; y=func(5); // valid giving input 5 and catching output func(5) // valid giving input 5 and not catching output printf("%d",func(6)) //valid calling function from printf will give output on screen func();//invalid statement we must pass 2 parameters func(1.3,2) // valid func(2,1.3) //valid func() }