Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   C++ Functions (http://www.programmingforums.org/showthread.php?t=4897)

Clotters Jul 13th, 2005 8:30 PM

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?

DaWei Jul 13th, 2005 9:27 PM

Main in itself doesn't do anything to calculate the area. It CALLS A FUNCTION to do that. The function is Area. It does the work. It accepts a length and a width and returns the product of those (the area). One could calculate it directly in main, but the lesson is showing you that it's better to do it with a function. If you need to do it 73 times, you call the function 73 times instead of having to replicate its entire body of code 73 times. That isn't too bad for the area function, a single multiplication, but consider if it were a very complex calculation.

The prototype at the top is not for you; it's for the compiler. Now it can tell if "main" called it correctly, because its pattern exists before the call. One doesn't have to prototype it. One could put the actual definition above main and the definition would let the compiler know that "main" called it properly. Most likely, though, for organizational reasons, you would like to have "main" at the top. Normally, with lots of functions to let the compiler know about, one would put the prototypes in a header file so they didn't visually clutter up the source file.

Now about those code tags.........THEY ARE SO DAMNED MUCH EASIER THAN PROGRAMMING!!!! put [/code] below your code block and [code] above your code block. I list them in reverse order here to prevent them from working in this example.

Clotters Jul 13th, 2005 9:40 PM

Sorry, I'll fx it up for anyone else trying to view it.

:

//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;
)


Thanks for the help by the way. Basically by making a prototype, you make promise the compiler you'll define the function later on, but for readability and clarity of script, it isn't a good idea to define the function at this point in the script.

Am I right?

DaWei Jul 13th, 2005 10:02 PM

That's correct. Understand that a portion of this kind of stuff is personal preference. C++ requires SOMETHING to say what the function is going to look like, however (that's why you have things like "include <iostream" -- it tells the compiler what the functions provided in that library look like so it can check your calls for validity). C, if you didn't declare or define the function in advance, just made some assumptions and let it go with a warning. This:
:

areaOfRoom = Area(lengthOfRoom, edithofRoom)
instructs the processor to jump off to the function, Area, and execute it (it saves a record of where to come back to when the function executes a "return" instruction). Area expects a length and a width, which the caller (main) passed as "lengthOfRoom" and "widthofRoom"). Area makes the multiplication and puts the answer in a return variable and returns back to "main". "Main" puts that value in "areaOfRoom" for further use (outputting it, in this instance). "Main" is delegating the work. If there were an existing function for calculating area in the run-time libraries, one wouldn't even have to write it, just call it and get the answer back. That's the purpose of functions -- write once, call many times.

Clotters Jul 13th, 2005 10:57 PM

While we're on the subject, where would be the best place to write the definitions of the functions? My logic tells me that to write it in the script, like I do now, isn't the best idea.

uman Jul 13th, 2005 11:04 PM

er... I'm not sure why you have a prototype for roomArea, which you never define.

Clotters Jul 13th, 2005 11:42 PM

roomArea is meant to be Area. It's a typo mistake.

DaWei Jul 14th, 2005 6:05 AM

Again, that's a personal (or client's specified) organizational preference. In a serious project one has many functions as well as (hopefully) object definitions. One function per page (or more if it won't fit), if they aren't tiny, is a common requirement. A fair part of that material will be formal, non-code documentation. Obviously there will be the need, in such a case, for declarations (prototypes) to be repeated. It is for that reason that such material is put in a header file. The same work can then be "included" at the top of each source file where it is required. Class/structure definitions can also be put in header files. Generally speaking, code should not be put in header files as multiple inclusions will result in multiple definitions. Header guards prevent a file from being included more than once in the same source file; they do not prevent its inclusion in several source files. Templates are often an exception. These things will be part of your learning process. You may get bum steers along the way. Check more than one source and use discretion. Learn how a build process works. Preprocessing is one thing, compiling is another, linking/locating is yet another.


All times are GMT -5. The time now is 6:23 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC