Create function power and a main program to exercise it - C Function

C examples for Function:Utility Function

Introduction

A function definition has this form:

return-type function-name(parameter declarations, if any)  
{  
  declarations  
  statements  
}  

Demo Code


#include <stdio.h>  
  
int power(int m, int n);  
  
int main()  //from   w ww  .  j  a  v a  2 s.  c  o m
 {  
     int i;  
  
     for (i = 0; i < 10; ++i)  
         printf("%d %d %d\n", i, power(2,i), power(-3,i));  
     return 0;  
 }  
  
 /* power:  raise base to n-th power; n >= 0 */  
 int power(int base, int n)  
 {  
     int i,  p;  
  
     p = 1;  
     for (i = 1; i <= n; ++i)  
         p = p * base;  

     return p;  
 }

Related Tutorials