Communication between Functions

When you write programs with multiple functions, they must have some way to communicate with each other. They need to be able to pass information back and forth. By information, I mean data - variables, constants, and expressions. There are several different mechanisms in C++ to do this:

  1. global data
  2. value parameters
  3. reference parameters
  4. return statement

Given specific situations, some mechanisms may be more appropriate than others; some mechanisms may be inappropriate.

This set of exercises will explore these various mechanisms. Each exercise involves running two different versions of a simple payroll program. In each case, the payroll program consists of 2 functions: main() and CalcPay().

Function Tasks

Main()

1. provides the necessary data for calculating the pay

2. prints the calculated pay.

CalcPay()

does the pay calculation


The following diagram illustrates the data that must be communicated between the two functions.

Exercise 1 - Global Data vs Local Data

Exercise 2 - Value Parameters vs Reference Parameters

Exercise 3 - Void Functions vs Value-returning Functions