【问题标题】:How to callback the value given to one function to be used in a different function如何回调赋予一个函数以在不同函数中使用的值
【发布时间】:2019-08-25 16:09:59
【问题描述】:

嘿,我正在使用 c 代码并编写了两个函数。我需要将赋予一个函数的值传递给另一个函数中包含的方程。

这是第一个从用户输入中获得值的函数:

double getAltitude()
{
double a;


printf("\nEnter altitude (m):");
scanf("%lf", &a);


if (a > 9000)
{
printf("Invalid input! Altitude must be between 0 and 9000m");
return getAltitude();
}
else if ( a < 0 )
{
printf("Invalid input! Altitude must be between 0 and 9000m");
return getAltitude();
}
return a; 
}

我需要使用用户输入的这个值来求解这个函数内部的一个方程

double density()
{
double density, a ;
density = (1.2 - 1.33*pow(10, -4)*a);
return density; 
}

应该调用来自第一个函数的用户输入来代替密度方程中的变量 (a)。

任何帮助将不胜感激

干杯

【问题讨论】:

  • 查找函数参数。

标签: c function callback


【解决方案1】:

在函数密度中加入变量double a,在getAltitude函数结束前调用密度函数。此外,如 cmets 中所述,请正确标记问题。

double getAltitude()
{
    double a;
    double x;

    printf("\nEnter altitude (m):");
    scanf("%lf", &a);


    if (a > 9000)
    {
        printf("Invalid input! Altitude must be between 0 and 9000m");
        return getAltitude();
    }
    else if ( a < 0 )
    {
        printf("Invalid input! Altitude must be between 0 and 9000m");
        return getAltitude();
    }
    x = density(a);
    return a; 
}


double density(double a){
     double density ;
     density = (1.2 - 1.33*pow(10, -4)*a);
     return density; 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多