Example on page 177
[ printtime.cpp ]
(needs ccc_time -- use Q to compile)
Functions declared with a non-void type (like "int") return a value.
They are used in expressions. But
void functions
return no value. They are used like statements.
- void_function_definition::= "void" name"(" parameters ")" "{" body "}"
void newline() { cout << "\n"; }
- void_return::= "return" ";".
Do not include an expression. If there is no return in a procedure then
the compiler adds one at the end of the body of the procedure.
- void_function_call::=name "(" actual_parameters ")" ";".
- statement::= assignment | void_function_call | ... .
Example from page 179-180
[ raisesal.cpp ]
Note -- a reference parameter is replaced only by a variable when the function is called.
If you want a function to change a actual parameter's value then it
must be called by reference.
Forgetting the "&" in a reference parameter leads to nasty little logic errors.
The compiler will not help you. Only knowing the syntax and the semantics
can help you.
- reference_parameter::= type "&" parameter.
The actual parameter for a reference must be a variable of the right type.
Later.
Interesting but not tested in this class...
Visicalc
was a breakthrough -- Computation for accountants.
Example on pages 184-185
[ global.cpp ]
Just say no! Or points will be lost and bugs created.
Many libraries define useful global constants as well as functions and procedures.
Example -- RAND_MAX for rand(). You can and should define global const for
mathematical constants like π.