function help answering questions

I'm confused by the following three problems. The only thing that throws me off is how similar the questions are, I feel like they are asking the same thing. Can anyone help me determine if I'm doing what is asked of me. Thanks in advance for any help.

3. Write a function called DisplayArea that displays the area of a room when the width and length are passed as arguments. Are width and length passed by value or reference?
void displayArea(double width, double length) {
cout << "Enter width" << endl;
cin >> width;
cout << "Enter length" << endl;
cin >> length;
cout << "The area of the room is: " << length * height << endl;
}
Passed by value
4. Write a function called CalculateArea that calculates the area of a room when the width and length and area are passed as arguments. Are width and length and area passed by value or reference? Show a sample call.
calculateArea(width, length);

void calculateArea(double width, double length) {
int area = 0.;

cout << "Enter width" << endl;
cin >> width;
cout << "Enter length" << endl;
cin >> length;
area = length * width;
}
Passed by reference
5. Write a function called Area that calculates the area of a room and returns area when the width and length are passed as arguments. Show a sample call.
area(width, length);

void area(double width, double length) {
int area = 0.;

cout << "Enter width" << endl;
cin >> width;
cout << "Enter length" << endl;
cin >> length;
area = length * width;
cout << "The area of the figure is: " << area << endl;
}
They are similar, but not the same.

#3 - displayArea - the function takes in length and width variables and prints the area it calculates. You have a 'height' in the output here that I think you mean to be 'width'.
#4 - calculateArea - The function takes in length, width and area variables and calculates the area. Nothing is returned from this function, implying to me that you want the function to update the area variable in the calling function. The code above only has two parameters, not three.
#5 - Area - the function takes in length and width variables and returns the area it calculates. If a function needs to return a value, the return type can't be void.

I read these as the length and width having been entered by the user in the calling function, so they don't have to be entered again. But it's not absolutely clear.
Let's start with question# 4 - CalculateArea, like the name suggests, is supposed to calculate the area, given a room's dimensions. It's not supposed to do anything else, like getting input, printing the area etc.
Therefore, CalculateArea might look like this:

1
2
3
4
5
6
int CalculateArea(int width, int length) {
	
	int area = (width * length);
	return area;

}


Now question# 3 - DisplayArea, like the name suggests, is supposed to display the area, given a room's dimensions. It's not supposed to do anything else, like calculating the area, etc.
Therefore, DisplayArea might look like this:

1
2
3
4
5
6
7
void DisplayArea(int width, int length) {

	int area = CalculateArea(width, length);

	std::cout << "The area is: " << area << std::endl;

}


As for question# 5, this seems to be a duplicate of what's being asked on question# 4 with CalculateArea. I don't see a real difference.
Don't forget to answer the second part of each question - providing an example of how each function might be called, and whether their parameters are passed by value or reference.
Last edited on
Topic archived. No new replies allowed.