|
C++ Functions
I'm reading SAMS Teach Yourself C++ in 21 Days and going through one chapter a day. I'm only on chapter 5 at the moment and am encountering few problems.
The chapter introduces functions and I'm just a bit lost. I wondered if any of you could help.
I'll write one example written so you can see clearly when I am confused - but I'll change a few parts just in case their are copyright issues. Sorry, I don't know how to put those white code screen on so I'm going to have to do it this way which may eat up whitespace.
//Lsiting 5.1 - demonstrates the use of function prototypes
#include <iostream>
short int roomArea(short int roomLength, short int roomWidth); // prototype
int main()
{
using std::cout;
using std::cin;
int lengthOfRoom;
int widthOfRoom;
int areaOfRoom;
cout << "How wide is your room? ";
cin >> widthOfRoom;
cout << "\nHow long is your room? ";
cin >> lengthOfRoom;
areaOfRoom = Area(lengthOfRoom, edithofRoom)
cout << "\nYour room is ";
cout << areaOfRoom;
cout << " square feet \n";
return0;
}
int Area(int len, int wid)
{
return len * wid;
)
Output should be
How wide is your room? 10
How long is your room? 20
Your room is 200 square feet.
Now what I don't get about this example is how any of the int Area function is relevant at all. Couldn't I take everything outside of int main() away and still get the same results? Also, when you write a prototype for the function, wouldn't it be better to write the stipulations of what the variables do, so later on you don't have to do that like the author did in the last three lines?
Can anyone explain to me what I the author is trying to get across?
|