Modern C++: The Good Parts/At last, functions

Functions are so important to programming that teaching control structures has been rather tedious without them, so with great relief let's see how to make our own.

Explanation
Woohoo, the code just got much easier to read, once you understand functions.

As the comments kind of explain, each function has a return type before its name. A function's return type will be the type of any calls to that function. A function call is an expression which causes a function to be run, such as. When the control flows over a  statement, the containing function exits. The function's caller then receives the return value as the value of the function call and continues from that point.

can stand in for  in a. The point is that control doesn't "fall through" from one case to another. C++ does allow that to happen, but it can be very confusing and it's rarely useful.

By now, you've probably noticed that  has a return type of int. It's the only non-void function that you don't need to  from; by default, it returns 0 when control reaches the bottom. 0 indicates that the program was successful, or ran without problems; any other value tells whoever ran the program that something went wrong. These values are called "exit codes", but you won't need to specify one anytime soon.

Functions also have parameters, the variables declared between their parentheses. Any function call must provide a value for each of the function's parameters; these values are called arguments.

Finally, a function must appear in the source before any call to it. However, C++ offers an important loophole to this that we'll cover later.

Exercises
under construction

Vocabulary

 * return type: the type of value that any call to a specific function will be resolved to.
 * function call: an expression which causes a function to be run.
 * caller: the function which contains a specific function call.
 * return value: the value that a specific function call will be resolved to.
 * void: a return type meaning that nothing is returned.
 * parameter: a variable in a function which is assigned by each call to that function.
 * argument: the value of a parameter for a specific call to a function.