如何使用函数计算BMI?

How to calculate BMI using function?

计算体重指数。 体重指数将您的体重与身高进行比较,计算方法是将您的体重(千克)除以您的身高(米)的平方。它可以让您了解相对于您的身高,您是体重不足、健康体重、超重还是肥胖。

体重指数类别:

  1. 体重不足 = <18.5
  2. 正常体重 = 18.5–24.9
  3. 超重 = 25–29.9
  4. 肥胖 = BMI 为 30 或更高

如果您体重过轻或超重或肥胖,请根据您的身高和年龄确定理想体重。

使用 Devine 公式估算理想体重(公斤):

男性:IBW = 50 kg + 2.3 kg for each inch over 5 feet.

女性:IBW = 45.5 kg + 2.3 kg for each inch over 5 feet.

#include <stdio.h>
#include <stdlib.h>

double calculateBMI(double weight, double height);

int main(void)
{
    printf("Calculate your BMI\n"); //Calculation of body mass index (BMI)

    double w, h, ret;
    double BMI = w / (h*h);
    ret = calculateBMI(BMI);

    printf("Enter your weight in kilograms:\n", w); //Input your weight in kilograms here
    scanf("%lf", &w);
    printf("Enter your height in metres:\n", h); //Input your height in metres here
    scanf("%lf", &h);
    printf("Your BMI is %lf\n", ret)

    printf("BMI categories:\n");
    if (BMI < 18.5)
    {
        printf("Your BMI is %lf and you are currently underweight.\n");
    }
    else if (BMI >= 18.5 && BMI <= 24.9)
    {
        printf("Your BMI is %lf and you are normal weight.\n");
    }
    else if (BMI >= 25 && BMI <= 29.9);
    {
        printf("Your BMI is %lf and you are currently overweight.\n");
    }
    else (BMI >= 30);
    {
        printf("Your BMI is %lf and you are obese.\n");
    }


    return 0;
}

double calculateBMI(double weight, double height)
{
    double result;
    result = weight / (height*height);

    return result;
}

求助我还是不知道怎么实现这些功能。请帮助我。

你的函数:

double calculateBMI(double w, double h)
{
    BMI = weight / (height*height);
    return calculateBMI;
}

此处调用函数并返回函数名称"calculateBMI"。

Return 您 want.This 的正确值不是递归函数。

您应该使用方法中使用的参数来计算 BMI。 您的变量 BMI、身高和体重最初并未在函数中声明。相反,您应该将 BMI 声明为 double,并使用身高和体重作为函数参数。

此外,您需要 return 从函数中获取 BMI 值。您错误地 returning 了 calculateBMI,它不是函数内部的有效标识符。

工作代码为:-

double calculateBMI(double weight, double height)
{
    double BMI = weight / (height*height);
    return BMI;
}

此外,您还没有在 main() 中调用方法 calculateBMI()。

...
printf("Enter your weight in kilograms:\n"); //Input your weight in kilograms here
scanf("%lf", &weight);
printf("Enter your height in metres:\n"); //Input your height in metres here
scanf("%lf", &height);
// add below line in your code to call the function.
BMI = calculateBMI(height,weight);
printf("BMI categories:\n");
...

我还建议你阅读更多关于 C 中的函数的知识。你需要更多的函数基础知识(也要努力练习)。

EDIT ---> Based on OP's comment, the final code would be :

#include <stdio.h>
#include <stdlib.h>

double calculateBMI(double weight, double height);

int main(void)
{
printf("Calculate your BMI\n"); //Calculation of body mass index (BMI)

double w, h, BMI;
printf("Enter your weight in kilograms:\n", w); //Input your weight in kilograms here
scanf("%lf", &w);
printf("Enter your height in metres:\n", h); //Input your height in metres here
scanf("%lf", &h);
BMI = calculateBMI(w,h);
printf("Your BMI is %lf\n", BMI)

printf("BMI categories:\n");
if (BMI < 18.5)
{
    printf("Your BMI is %lf and you are currently underweight.\n");
}
else if (BMI >= 18.5 && BMI <= 24.9)
{
    printf("Your BMI is %lf and you are normal weight.\n");
}
else if (BMI >= 25 && BMI <= 29.9);
{
    printf("Your BMI is %lf and you are currently overweight.\n");
}
else (BMI >= 30);
{
    printf("Your BMI is %lf and you are obese.\n");
}


return 0;
}

double calculateBMI(double weight, double height)
{
    double result;
    result = weight / (height*height);
    return result;
}