函数内的 print 给出正确的值,但返回值为零?

问题描述 投票:0回答:1

我一直在尝试返回“hypo”,并且可以获得在函数内打印的正确值,但是当我尝试在函数外部使用hypo的返回值时,它返回零。有什么理由吗?

#include<stdio.h>

double pythagorean(double num1,double num2);

int main(){
//----------------------------------------------------------------- Problem 1
  double a;
  double b;
  double hypo;
  
  printf("Enter the value for a: ");  //values for A and B
  scanf("%lf",&a);
  printf("Enter the value for b: ");
  scanf("%lf",&b);
    
  pythagorean(a,b);                    // function one
  
  
  printf("If a = %.3lf and b = %.3lf, then that means the hypotenuse is %.3lf.\n",a,b,hypo);
    
    return 0;
}


double pythagorean(double num1,double num2)
{
  
  double hypo = sqrt((num1)*(num1)+(num2)*(num2));
  printf("%lf\n",hypo); 
    
    return hypo;
}
c return
1个回答
0
投票

改变

pythagorean(a,b); 

hypo = pythagorean(a,b);  // store the return value in 'hypo'

仅仅因为变量名称相同,因为它们驻留在不同的作用域中,所以它们不共享相同的内存,和/或自动更新。

© www.soinside.com 2019 - 2024. All rights reserved.