Also, to line 36 and 40, why is it "return 2" and "return"? I don't know if I asked my question right, but in my program I tried to do area = length * width? I assume we're adding Return length * width; width because it's a step-by-step procedure? |
You are returning the output of the function, Of course, you can do this inside the function
1 2
|
area = length * width;
return area;
|
But that adds an unnecessary line.
The formula of perimeter is 2*(l+b) :) not l+b
return 2*(length+width)
2 multiplied by the sum of length and width.
Line 40, it calculates length * width (lets say 4 * 3 , which is 12) and returns 12.
I appreciate the response, I get it now. I just have two questions about your program. Is that just the rule, whenever there is a function before the "int main()", we must definite it again in the beginning after the int main() again? |
I would like to ask you to read about declarations and definitions.
This is a declaration.
double calcArea(double, double);
This is a definition.
1 2 3
|
double calcArea(double length, double width) {
return length * width;
}
|
The declaration introduces the name into scope. It doesn;t say anything about what the function does, only gives the name, output datatype and parameter datatype.
Definition does the above AND gives the process, that is the contents between {..}
You can use two options
1 2
|
//all definitions here
int main() { }
|
Or you can use this option(used in this program)
1 2 3
|
//all declarations here
int main() { }
//all definitions here
|
Well, not exactly, basically a declaration or definition should be above where it is called from. I would like you to read it from a textbook to understand better.
Remember, when you call a function in main, it searches for the function ABOVE main. So, in option 1, it finds the definition directly and proceeds.
In option 2, it searches the function ABOVE main, finds the declaration, then goes to the definition. This is a very simplified way to understand, so again I recommend a book, such as Chapter 8 of Programming : Principles and Practices using c++ to understand this better.