I have a very basic understanding of how to code functions. I understand that you must prototype the functions before using them. I am attempting to write a simple program utilizes four functions.
1) getLength - This function should ask the user to enter a rectangle's length and then return it has a double.
2) getWidth - This function should ask the user to enter a rectangle's width and then return it has a double.
3) getArea - should accept the length and width as arguments and return the rectangle's area. (area=length*width)
4) displayData - should accept the length, width, and area as argument and display them in an appropriate message on the screen.
I have attached what coding I have done so far. If someone would so kind as to code the length and area, I believe I can code the rest from their lead.
interesting way of programming. As I am mostly new this style is a little hard to follow without more detail comments. possible to use what I have coded format?
A definition would take this form:
< Return-Type > < Identifier > ( < Parameter-List > )
{
< Body >
}
A real-world example:
1 2 3 4 5 6 7 8 9
double getLength( )
{
// Get the user's input:
double Input( 0.0 );
std::cin >> Input;
// Return the user's input:
return( Input );
}
Here's what's going on:
1) Define what's called the function header. This consists of the function's return-type, name, and parameter list.
2) After, define the function's body. This is where the function's inner-workings reside.
3) Return from the function. This is achieved with the return statement. A function must have a return statement, except for a function that returns void.
I am very grateful for the help. I coded what I believe is a good basic program. I am having issues when I try to complie it.
line 29 states "no match for 'operator<<' in 'std::cout << displayData()' "
line 32 and 40 states "expected primary-expression before "double"" and " expected `;' before "double""
line 49 states "a function-definition is not allowed here before '{' token" and "expected `;' before "double""
Any help in resoving these issues will be greatly appreciated.
1) The reason you're getting the cout error for that particular function is because it returns nothing. It has a void return type, which you cannot cout.
2) All of your function definitions should be outside (i.e. below) main().
3) The function definitions for getArea and displayData don't match their prototypes. The prototypes have no parameters and the definitions do have parameters.
Subzero, your example may work but, as this looks like a school project, it's important to meet the specification:
I am attempting to write a simple program utilizes four functions.
Also, the spec mentions that the functions need to return values, so pass-by-reference isn't a good option.
Here's how I'd do length. The rest you should be able to work out from it.
Function definitions need to go after the closing brace in main.
displayData doesn't return anything. So of course, cout doesn't know what you want done with the nothing you're returning. displayData also takes parameters. This is not reflected in the function prototype before main, and it should be.
When you call getArea() and displayData() you don't provide any arguments to them.
When you call getLength() and getWidth() you don't assign the return value to anything. You need to do so, so you can then provide those values to getArea().
I copied and pasted iHutch105's example (i hope you don't mind) and attempted to build my program around it. I have linker error message that does not make sense to me. PLease let me know where my mistake is at and a possible way to fix it.
error message:
[Linker error] undefined reference to `getArea()'
[Linker error] undefined reference to `displayData()'
area = getArea(length, wide);
displayData(length, wide, area);
The prototypes and definitions should look identical for the return type, function name, and parameter list. Then your use of the function should supply the proper number of parameters for it to work properly.