# Simple Functions, Continued

Tom Kelliher, CS23

Feb. 14, 1996

# Function Communication

Function communication mechanisms:

• Global variables
• Return value
• Parameters (reference or pointer)

## Global variables

• Simplicity, convenience --- no messy parameter lists

• Pollution of global name space
• Reduction of modularity

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:

• Must match in number
• One-to-one correspondence between individual parameters
• Types of corresponding parameters must match

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:

• Prompt the user to enter a filename. Attempt to open the file for reading. Continue prompting until a file is successfully opened.
• Read the data in the file, accumulating appropriate totals so that an average can be calculated.
• When finished reading data, close the file.
• Calculate and output the average.

## 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 closeFile
call printResults
```

# Example 2

## Problem

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

• determine if a positive integer is prime
• compute all primes in a given range.

## Requirements Specification

Develop a program that does the following:

• Displays a menu of choices (1 to determine if an integer is prime and 2 to find all primes in a specified range).
• Prompts the user to enter a choice. If the user enters anything other than 1 or 2, prompt the user again to enter a correct choice.
• In case the user chooses to find if an integer is prime, ask them to enter a positive integer. Then, determine if the integer is prime and report the result.
• If the user chooses to find all primes in a given range, prompt him or her to enter a positive integer for the beginning of the range, and another positive integer for its end. Make sure that the beginning and end of the specified range are both positive, and that the beginning of the range is less than or equal to its end. If not, warn the user and prompt them to enter correct values.
• Consider all integers in the range and determine if they are prime. Print any integer(s) found to be prime.
• After a user choice is completed, ask the user if they want to continue. If the answer is yes, display the menu again and proceed as explained above. Otherwise, terminate execution.

## 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