Chapter 5: Functions

Chapter Goals

Functions as Black Boxes

Writing Functions (Function Definition)

 Let us compute the value of a saving account with an initial balance of $1,000 after 10 years with the interest rate of p percent.

Writing Functions (futval.cpp)

Writing Functions (Syntax 5.1: Function Definition)

Syntax 5.1 : Function Definition

return_type function_name(parameter1, ..., parametern)
{
statements
}
Example:
double abs(double x)
{ if (x >= 0) return x;
else return -x;
}
Purpose: Define a functions and supply its implementation.

Writing Functions (Hard Wired Values)

double future_value(double initial_balance, double p, int n)
{ double b = initial_balance * pow(1 + p / 100, n);
return b;
}
double balance = future_value(1000, rate, 10);

Function Comments

Function Comments

 /**
Computes the value of an investment with compound interest.
@param initial_balance - the initial value of the investment
@param p the interest rate pre period in percent
@param n the number of periods the investment is held
@return the balance after n periods
*/
double future_value(double initial_balance, double p, int n)
{ double b = initial_balance * pow(1 + p / 100, n);
return b;
}

Return Values (Syntax 5.2: return Statement)

Syntax 5.3 : return statement

 return expression;
Example:
 return pow(1 + p / 100, n);
Purpose: Exit a function, returning the value of the expression as the function result.

Return Values

Return Values (approx.cpp)

Parameters

Side Effects

Procedures

Procedures (printime.cpp)

Reference Parameters

Reference Parameters (Syntax 5.4: Reference Parameters)

Syntax 5.4 : Reference Parameters

 type_name& parameter_name
Example:
 Employee& e
int& result
Purpose: Define a parameter that is bound to a variable in the function call, to allow the function to modify that variable.

Reference Parameters (raisesal.cpp)

Reference Parameters (Syntax 5.5: Constant Reference Parameters)

Syntax 5.5 : Constant Reference Parameters

 const type_name& parameter_name
Example:
 const Employee& e
Purpose: Define a parameter that is bound to a variable in the function call, to avoid the cost of copying the variable into the parameter variable.

Variable Scope and Global Variables (Variable Scope)

Variable Scope and Global Variables (global.cpp)

Stepwise Refinement

From Pseudocode to Code

Write a function that turns a number into a text string, for example 273 turns into "two hundred seventy four".

From Pseudocode to Code (intname.cpp)

Walkthroughs

int_name(n = 416)
c
r
416
""
int_name(n = 416)
c
r
416
""
 
digit_name(n = 4)
Returns "four"

int_name(n = 416)
c
r
 416 
 "" 
16
"four hundred"

int_name(n = 416)
c
r
 416 
 "" 
 16 
 "four hundred" 
0
"four hundred sixteen"

Preconditions

Preconditions (Syntax 5.6 : Assertions)

Syntax 5.6 : Assertions

 assert(expression);
Example:
 assert(x >= 0);
Purpose: If the expression is true, do nothing. If the expression is false, terminate the program, displaying the file name, line number, and expression.

Preconditions