All subtasks in a program can be implemented as functions, either as functions that return a value or as void
functions.
A formal parameter is a kind of place holder that is filled in with a function argument when the function is called. There are two methods of performing this substitution, call-by-value and call-by-reference.
In the call-by-value substitution mechanism, the value of an argument is substituted for its corresponding formal parameter. In the call-by-reference substitution mechanism, the argument should be a variable and the entire variable is substituted for the corresponding argument.
The way to indicate a call-by-reference parameter in a function definition is to attach the ampersand sign, &, to the type of the formal parameter.
An argument corresponding to a call-by-value parameter cannot be changed by a function call. An argument corresponding to a call-by-reference parameter can be changed by a function call. If you want a function to change the value of a variable, then you must use a call-by-reference parameter.
A good way to write a function declaration comment is to use a precondition and a postcondition. The precondition states what is assumed to be true when the function is called. The postcondition describes the effect of the function call; that is, the postcondition tells what will be true after the function is executed in a situation in which the precondition holds.
Every function should be tested in a program in which every other function in that program has already been fully tested and debugged.
A driver program is a program that does nothing but test a function.
A simplified version of a function is called a stub. A stub is used in place of a function definition that has not yet been tested (or possibly not even written) so that the rest of the program can be tested.
A debugger, strategic placement of cout
statements, and the assert
macro are tools that can help you debug a program.