Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 13th, 2005, 8:30 PM   #1
Clotters
Programmer
 
Join Date: May 2005
Posts: 60
Rep Power: 4 Clotters is on a distinguished road
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?
Clotters is offline   Reply With Quote
Old Jul 13th, 2005, 9:27 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 13th, 2005, 9:40 PM   #3
Clotters
Programmer
 
Join Date: May 2005
Posts: 60
Rep Power: 4 Clotters is on a distinguished road
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?
Clotters is offline   Reply With Quote
Old Jul 13th, 2005, 10:02 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 13th, 2005, 10:57 PM   #5
Clotters
Programmer
 
Join Date: May 2005
Posts: 60
Rep Power: 4 Clotters is on a distinguished road
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.
Clotters is offline   Reply With Quote
Old Jul 13th, 2005, 11:04 PM   #6
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
er... I'm not sure why you have a prototype for roomArea, which you never define.
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials.
--WilliamSChips on Slashdot
uman is offline   Reply With Quote
Old Jul 13th, 2005, 11:42 PM   #7
Clotters
Programmer
 
Join Date: May 2005
Posts: 60
Rep Power: 4 Clotters is on a distinguished road
roomArea is meant to be Area. It's a typo mistake.
Clotters is offline   Reply With Quote
Old Jul 14th, 2005, 6:05 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:41 PM.

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