Program Defined Value Returning Function

I need to remove both calculation functions from the code below and assign both to a program-defined value-returning function named getHypotenuse.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
//declare variables
double sideA = 0.0;
double sideB = 0.0;
double sumSqrs = 0.0;
double hypotenuse = 0.0;

//get lengths of two sides
cout << "Side a length: ";
cin >> sideA;
cout << "Side b length: ";
cin >> sideB;

//calculate the length of the hypotenuse
sumSqrs = pow(sideA, 2) + pow(sideB, 2);
hypotenuse = sqrt(sumSqrs);

//display the length of the hypotenuse
cout << "Hyptonuse length: "
<< hypotenuse << endl;

system("pause");
return 0;
} //end of main function

Include a function prototype at the top:
double getHypotenuse(double, double);//tells the compiler the return type, and parameter types of getHypotenuse()
Put a function definition at the bottom:
1
2
3
4
5
double getHypotenuse(double A, double B)
{
    //put the formula here
    return hypotenuse; //returns hypotenuse to main
}

And call the function from main.
What am I doing wrong? Thanks!

#include <iostream>
#include <cmath>
using namespace std;

//function prototype
double getHypotenuse(double sideA, double sideB);

int main()
{
//declare variables
double sideA = 0.0;
double sideB = 0.0;
double sumSqrs = 0.0;
double hypotenuse = 0.0;

//get lengths of two sides
cout << "Side a length: ";
cin >> sideA;
cout << "Side b length: ";
cin >> sideB;

//calculate the length of the hypotenuse
hypotenuse = getHypotenuse;

//display the length of the hypotenuse
cout << "Hyptonuse length: "
<< hypotenuse << endl;

system("pause");
return 0;
} //end of main function

//*****function definitions*****
double getHypotenuse(double sideA, double sideB)
{
sumSqrs = pow(sideA, 2) + pow(sideB, 2);
hypotenuse = sqrt(sumSqrs);
return hypotenuse;
}
You need to specify arguments to pass to the getHypotenuse function:
hypotenuse = getHypotenuse(sideA, sideB)
Also, if you're only squaring a number you should just say sideA*sideA rather than pow(sideA, 2).
Thanks again! This code works now, do you see anything else wrong with the format?

#include <iostream>
#include <cmath>
using namespace std;

//function prototype
double getHypotenuse(double sideA, double sideB);

int main()
{
//declare variables
double sideA = 0.0;
double sideB = 0.0;
double sumSqrs = 0.0;
double hypotenuse = 0.0;

//get lengths of two sides
cout << "Side a length: ";
cin >> sideA;
cout << "Side b length: ";
cin >> sideB;

//calculate the length of the hypotenuse
hypotenuse = getHypotenuse(sideA, sideB);

//display the length of the hypotenuse
cout << "Hyptonuse length: "
<< hypotenuse << endl;

system("pause");
return 0;
} //end of main function

//*****function definitions*****
double getHypotenuse(double sideA, double sideB)
{
double sumSqrs = 0.0;
double hypotenuse = 0.0;
sumSqrs = sideA * sideA + sideB * sideB;
hypotenuse = sqrt(sumSqrs);
return hypotenuse;
Topic archived. No new replies allowed.