Chapter 5: Functions

Chapter Goals

Functions as Black Boxes

Functions as Black Boxes

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 (Syntax 5.1 : Function Definition)

Writing Functions (futval.cpp)

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;
}

Function Comments

Function Comments

/**
   Computes the value of an investment with compound interest.
   @param intial_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

Parameters (Syntax 5.3 : Function Declaration (or Prototype))

Syntax 5.3 : Function Declaration (or Prototype)

return_type function_name(parameter1, ..., parametern);
Example:
double abs(double x);
Purpose: Declare a function so that it can be called before it is defined.

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 Variables)

Variable Scope and Global Variables (global.cpp)

Stepwise Refinement

From Pseudocode to Code

From Pseudocode to Code

From Pseudocode to Code

From Pseudocode to Code (intname.cpp)

From Pseudocode to Code

Walkthroughs

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

Walkthroughs

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