代码之家  ›  专栏  ›  技术社区  ›  Adi

将powf结果返回到主函数C编程[关闭]

  •  -4
  • Adi  · 技术社区  · 7 年前

    我试图制作一个单独的函数,从主函数中获取参数x和y,在powf中使用它,将其分配给变量“result”,并将其输出到主函数。感谢您的帮助

    #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;
    
    }
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Digvijaysinh Gohil    7 年前

    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;
    }
    

    如您所见,您没有调用该函数 mathPow() 在你的 main() .

    为了执行 用户定义函数 你需要从你的办公室给他们打电话 主()

        2
  •  0
  •   Adi    7 年前

    这就是整个程序的工作原理

    #include <stdio.h>
    #include <math.h>
    
    
    float mathPow(float x, float y);
    
    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;
    
    }
    
    float mathPow(float x, float y){
    
     float result = powf(x,y);
    
      return result;
    
    }