I absolutely suck at functions. I know that you need to declare a function, code for it outside main, and then call it in main. I just don't know the syntax well. Plus i'm getting an error for "redefinition of formal parameter bmi."
// This first line is good. You are passing 3 parameters in by value... 'weight', 'height', and 'BMI'
double calculateBMI(double &weight, double &height, double &BMI)
{
double numerator = weight * 703; // <- declares a new var named 'numerator'
double denominator = height * height; // <- declares a new var named 'denominator'
double BMI = numerator / denominator; // <- declares a new var named 'BMI'
// ... but wait... we already have a var named BMI (it's the parameter passed to the
// function. So this will generate an error
#include <iostream>
usingnamespace std;
void calculateBMI(double *weight, double *height, double *BMI);
int main()
{
double weight, height, BMI;
cout << "This program will calculate a person's Body mass index(BMI)" << endl;
cout << "Enter the weight of the person " << endl;
cin >> weight;
cout << "Enter the height of the person " << endl;
cin >> height;
// send the addresses of weight, height, BMI to the calculating function:
calculateBMI(&weight, &height, &BMI);
cout << "The BMI of the person is:- " << BMI;
return 0;
}
void calculateBMI(double *weight, double *height, double *BMI)
//having received the addresses for weight, height, BMI: use them for calculation
{
//Declare local variables locally - avoids confusion:
double numerator, denominator;
numerator = *weight * 703;
denominator = *height * *height;
// use "address" of BMI to return the value to main()
//since you are changing BMI through its address
//no need for this function to return anything
*BMI = numerator / denominator;
}
You set up the function calculateBMI to expect addresses in which to look for the values it will operate on:
*weight. . . etc.
You send the addresses via the use of &weight. . . etc.
@PCrumle48 why did you change from references to pointers? This will work
1 2 3 4 5 6 7 8 9
// This first line is good. You are passing 3 parameters in by value... 'weight', 'height', and 'BMI'
double calculateBMI(double &weight, double &height, double &BMI)
{
double numerator = weight * 703; // <- declares a new var named 'numerator'
double denominator = height * height; // <- declares a new var named 'denominator'
double BMI = numerator / denominator; // <- declares a new var named 'BMI'
// ... but wait... we already have a var named BMI (it's the parameter passed to the
// function. So this will generate an error
You should use your original and not the pointer OP.
One more thing to mention, you should check that denominator isn't 0. Though they are doubles so won't be as easy as if(denominator) By the way OP you should avoid using global variables and prefer local.
You are right, of course - I have been out of the C/C++ mode for too long, I guess, living in a programming world where pass-by-reference isn't standard use.
One observation hasn't been made about this:
In the original, weight, height and BMI are declared globally, so this:
1 2 3 4 5 6 7
void calculateBMI(double &apple, double &orange, double &pear)
{
double numerator = weight * 703;
double denominator = height * height;
BMI = numerator / denominator;
cout << "The BMI of the person is:- " << BMI;
}
though it wouldn't make sense to do so, will still calculate BMI correctly based on the globally visible values!
so would this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void calculateBMI(double &apple, double &orange, double &pear)
{
double numerator = apple * 703;
// weight retrieved through reference &apple
double denominator = orange * orange;
//Height through reference &orange
pear = numerator / denominator;
// BMI written through reference &pear
cout << "The BMI of the person is:- " << pear;
cout << "The BMI of the person is:- " << BMI;
// BMI retrieved by its global name
// when you return to main(), only BMI remains,
// &apple, &orange and &pear are out of scope in main()
}
Maybe the place to check for zero denominator is where weight is entered, so the user has an opportunity to correct it.