Simple Functions, Continued

Tom Kelliher, CS23

Feb. 14, 1996

Function Communication

Function communication mechanisms:

Global variables

Advantages:

Disadvantages:

Rule of thumb: ok to have a few global variables; should be used from many functions

Return Value

What more is there to say?

Parameters

Synonym: arguments

Formal parameters:

These are used to provide input values for the function, return values, or both. They have block scope, local lifetime, and are lvalues. They behave exactly as initialized local variables.

Actual parameters:

Expressions (constants and variables are a subset) in a function call that correspond to the formal parameters.

Correspondence between formal and actual parameters:

Example:

float addFloats(float, float);
int i = 6;
float x;

x = addFloats(i + 6, 12.0);      // wrong
x = addFloats(i + 2.3, 12.0);    // ok
x = addFloats(float(i), 12.0);   // ok

Default passing method: call by value

Call By Value Parameters

Call Semantics:

  1. Actual parameter expressions are evaluated in arbitrary order
  2. Parameter values stored for use by function being called
  3. Function is called
  4. Formal parameters initialized from stored values

Actual parameters are rvalues

Example:

void f(void);
void g(int, int);

void f(void)
{  int i = 1;

   g(i, 6);
   cout << i << endl;   // what is printed?
}

void g(int a, int b)
{  a = 12;
   b = 20;
}

Conclusion: called function can't communicate through value parameters

Call By Reference Parameters

Call Semantics:

  1. Actual parameter lvalues are evaluated in arbitrary order
  2. Function is called
  3. Formal parameters are aliased to lvalues of actual parameters

Actual parameters must be lvalues

Example:

void f(void);
void g(int&, int&);   // reference parameter indicator

void f(void)
{  int i = 1;

   g(i, 6);   // constant 6 is illegal here --- not an lvalue
              // i + 4 would likewise be illegal
   cout << i << endl;   // what is printed?
}

void g(int& a, int& b)
{  a = 12;
   b = 20;
}

Conclusion: called function can communicate through reference parameters

Example 1

Problem

We want to develop a program which reads a set of real numbers from a file, computes their average, and outputs the average.

Requirements Specification

Develop a program that does the following:

Analysis

Inputs: Firstly, a filename. Secondly, the data (real numbers) within a file. The data will be one number per line.

Outputs: The arithmetic average of the data, or an error message if there is no data.

Formula: Sum of the data divided by the number of data items.

Design

Initial pseudocode:

call openFile
call readData
call closeFile
call printResults

Example 2

Problem

We want to develop an interactive, menu-driven program that gives the user two choices:

Requirements Specification

Develop a program that does the following:

Analysis

Inputs: User choice. If the choice is 1, an integer. If the choice is 2, an integer for the beginning of the range and another for the end of the range.

Outputs: A message stating whether or not the integer is prime, if the choice is 1. A list of prime integers in the specified range, if the choice is 2.

Formula: Check to see if the given integer n can be divided by any integer between two and the integer obtained by truncating sqrt(n) to an integer.

Design

Initial pseudocode:

while the user wants to continue
   begin
      call printMenu
      set choice to enteredChoice
      call processChoice

      ask the user if they want to continue
   end
end_while



Thomas P. Kelliher
Tue Feb 13 11:41:55 EST 1996
Tom Kelliher