Sunday, March 10, 2013

Functions in C examples

 A function provides a convenient way to encapsulate some computation, which can then be used without worrying about its implementation. With properly designed functions, it is possible to ignore how a job is done; knowing what is done is sufficient. C makes the sue of functions easy, convenient and efficient; you will often see a short function defined and called only once, just because it clarifies some piece of code.


 A function definition Syntax:   
 return-type function-name(parameter declarations, if any)  
 {  
   declarations  
   statements  
 }  
 Example:   
 /*  
 power.c: This program will give you brief idea about functions.   
 Author: Afiz S   
 Date: 11/08/10  
 */  
 #include  
 int power(int a, int b); //prototype of the function.   
 main()  
 {  
 int a,b;  
 printf("Enter your 2 number base and exponent\n");  
 scanf("%d%d",&a,&b);  
 printf("%dpower %d is = %d\n",a,b,power(a,b)); // calling function.   
 }  
 int power(int a, int b) // function defination   
 {  
 int i,base=a;  
 for(i=1;i  
  {  
  //printf("%d\n",a);  
  a=a*base;  
  }  
  return a;  
 }  

No comments:

Post a Comment