Search code examples
cfunctionpow

Return powf result to main function C Programming


I'm trying to make a separate function that takes parameters x and y from the main function use use it in the powf, assign it to a variable 'result' and output it to the main function. Any help is appreciated

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

int  main() {

  float x = 5.5;
  float y = 3;
  float total = 1;
  int i;


  for( i=0; i <= y; i++ ) {
    total = total * x;
  }

  printf("total = %.3f\n",total);
  printf("Result of powf function is: %.3f\n", result);

  return 0;

}

float mathPow(float x, float y){

  result = powf(x,y);

  return result;

}

Solution

  • I have modified your code a bit

    float mathPow(float x, float y){
      float result = powf(x,y);
      return result;
    }
    int  main() {
    
      float x = 5.5;
      float y = 3;
      float total = 1;
      int i;
      for( i=0; i <= y; i++ ) {
        total = total * x;
      }
      printf("total = %.3f\n",total);
      printf("Result of powf function is: %.3f\n", mathPow(x,y));
      return 0;
    }
    

    as you can see you did not called the function mathPow() in your main().

    In order to execute the User Define functions you need to call them from your main()