Chapter 8 Margin Notes
General Note: Up to this point, all of the code you've written has appeared in the main() function. In this chapter, you learn that it makes sense to place code in other functions that you write.
Page 314. A 'flat implementation' means a single function.
Page 315, non-void and void functions. I think this is pretty poorly explained. Let me summarize in two sentences. Some functions 'do something' and return a value to the calling program. These are what the author calls 'non-void' functions. Other functions 'do something' but do not return a value. These are called 'void' functions.
Page 315, recursion. A recursive function is a function that calls itself. If this confuses you, hold on until the end of the chapter.
Page 316, the Function Header. The Function header is just the first line of a function. It consists of the data type of the value to be returned by the function (int, char, String, etc), the name of the function, and a parameter listing. Like this
long cube (int x)
The name of this function is cube, its return value is a long integer, and it accepts a single integer parameter named 'x'.
Page 318. Check out the rules for naming a function.
Page 318, Debugging Tip. Return values from a function are accomplished using the return statement.
Page 320, Caution. It's not necessary to declare a parameter, the way you do a local function. In essence, the parameter is 'declared' in the function header.
Page 320, returning a value. Note how a value or an expression is returned using the return statement.
Page 321, Debugging Tip. Check out the debugging tip.
Page 322, Calling a Non-Void Function. Are called just like standard functions. Bear in mind that if your function returns a value, you must provide for the assignment of the return value to a variable or its use in an expression.
Page 323, Arguments versus parameters. No big deal---parameters are defined in function headers, and arguments are the values passed to the function.
Page 324, Debugging Tip. The author is trying to say that arguments are passed to a function positionally--that is, the name of the argument has nothing to do with the name of the parameter---only the order does.
Page 325, Void Functions. Let's not make a mountain out of a molehill here. Void functions do not return a value to the calling procedure via a return statement---however, there is a way to return multiple values, which we'll look at shortly. A Void Function header specifies a void return type, like this...
void displayHeader()
By the way, don't be fooled by the author's lack of parameters for this function--a void function may also have one or more parameters.
Page 328, Programming Note. Trivia--it is possible to execute the return statement within a void function. Notice that is has no value, however.
Page 328, Value Parameters. When a calling procedure passes arguments to a function whose parameters are defined by value, anything the function does 'to' these variables is not propagated back to the calling procedure. In other words, if the value of one or more of the passed parameters is changed, the change is not reflected in the argument(s) value in the calling procedure. Think of value parameters as passing copies of values to the function. You need not do anything 'special' to define a function with Value Parameters---just name them normally.
Page 330, Reference Parameters. Reference parameters are designated, in the function header, by preceding the name of the parameter with an ampersand, like this...
long cube (int &x)
When a calling procedure passes arguments to a function whose parameters are defined by reference, anything the function does 'to' these variables is propagated back to the calling procedure. In other words, if the value of one or more of the passed parameters is changed, the change is reflected in the argument(s) value in the calling procedure.
Page 330, the author's explanation of Reference Parameters. I'm not sure I like his statement that parameter values are passed back to the arguments. When you pass an argument to a function whose parameters are defined by reference, you pass the 'address' of the argument, not the actual value. As a result, anything the calling function does to that argument is done directly against the argument (via its address). There's no real 'passing' back of anything.
Page 334, User defined functions within your program. User defined functions can appear anywhere within your program, but by convention, they appear after the main() function of your program. The problem with this is that calls to these functions are then 'not recognized' by the main() function. You can get around this by placing the user defined functions 'before' the main() function, but as I indicated, this is not conventional. You can solve the problem by defining function prototypes at the 'top' of your program code---this alerts the compiler that a custom function is in your program. Function prototypes are really just the header line of the function---like this...
long cube (int x);
Page 338, Debugging Tip. Don't forget the semicolon for the function prototype.
Page 338, Default Parameters. Having just learned about parameters, this next section might be confusing for you. However, it's not too complicated. Basically, it's possible to call a function which accepts multiple parameters, and pass it none, some or all of them. For instance, here's a volume() function which calculates volume.
int volume (int length, int width, int height) {
return length * width * height;
}
You can decide to assign a default value of 5 for width and 2 for height (perhaps you are in a shipping department where all of your boxes a 5 inches wide and 2 inches high, but vary by length). You can define the defaults either in the header
int volume (int length = 3, int width = 4, int
height = 5) {
return length * width * height;
}
or in the function prototype (but not both)
int volume (int length = 3 , int width = 4, int height = 5);
You could then call the volume() function with zero, 1, 2 or all of the arguments, like this
cout << "volume is " <<
volume()
or
cout << "volume is " <<
volume(l)
or
cout << "volume is " << volume(l,w)
or
cout << "volume is " <<
volume(l,w,h)
Page 340, Function Overloading.
This topic may confuse you after dealing with default parameters, but
it's possible in C++ to create two (or more) functions with the same name, but
whose signature (number and type of parameters) is different. When a procedure
calls the function, C++ knows which one to execute by matching the parameter
list. As the author points out, Function Overloading is related to Polymorphism,
which is one of the cornerstones of object-oriented programming.
Page 354, Magnifying Glass. A local variable is a variable that is defined within a specific block of code, such as a function. As such, it can only be 'seen' by code within that block. What the author fails to say here is that you can define a variable outside of any function--in which it then has global level visibility, and can be seen by code anywhere within your program. In general, it's best to define your variables as locally as possible.
Page 363, Recursion. Recursion is a process whereby an operation (function) calls itself until a terminating condition is reached. Some types of computer programs can best (or only) be solved using recursion. For the math majors in the class, factorials would be a good example. By the way, I hate the author's use of the term 'clone'. Take a look at the code on page 365--the balance() function has a call to balance() in it! Most importantly, there's also a way to end the function.
Page 368, Caution. Check this out--it's crucial.