Section 1.7 Functions


#include <stdio.h>
int power(int m, int n);
/* test power function */
    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;
Run this


This program is a simple demonstration of functions. A function power is declared to take two integer arguments and return an int value. In the program we send a number, base and a number, n to power, and the program returns the value of base raised to power n.

Comments by Disqus