Functions


#include <stdio.h>
#include <math.h>

double funct(double x){
        return(x*x);
 }

int main(){
   
     int i;
     double x,y;    

        printf(" x \t f(x) \t square root of f(x) \n");
        printf("------------------------------------------\n");

for ( i = 1 ; i <= 10; i=i+1) {
        x = (double) i;
        y = funct(x);
        printf("%5.2f \t %6.2f\t %6.2f\n",x, y,sqrt(y));
}

}


Power


/* positive powers of integers */

#include <stdio.h>

int power(int base, int exp) {

    int res;

    if (exp == 0)
      res = 1;             /* Zeroth power is equal to 1 */
    else
      res = base * power(base,exp-1); /* Recursive definition of power */
    return(res);
}

int main() {

    int m, n;

    printf("Enter two positive integers > ");
    scanf("%d %d",&n,&m);
    printf("%d power of %d is equal to %d.\n",m, n, power(n,m));
}


/* arbitrary powers of real numbers */

#include <stdio.h>

double power(double base, int exp) {

    double res;

    if (exp < 0)
      res = 1/power(base,-exp); /* Negative power is 1/positive power */
    else 
       if (exp == 0)
          res = 1;             /* Zeroth power is equal to 1 */
    else
      res = base * power(base,exp-1); /* Recursive definition of power */

    return(res);
}

int main() {

    int m;
    double n;

    printf("Enter the base and the exponent > ");
    scanf("%lf %d",&n,&m);
    printf("%d power of %lf is equal to %lf.\n",m, n, power(n,m));
}