Thread: C++ Functions
View Single Post
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