A Touch of Java

Part 3: functions

PGSS Computer Science Core Slides

Wherein, with statements in hand, the young programmers venture to put them together to build functions.

The function

To the outside world a function is ideally a black-box solution to a problem class.

  input n
      |    +-----------+
      +--->|           | /
           |           |/|
           | Primality | |
           |           |\|----+
           |           | \    v
           +-----------+     output 0 or 1

Other functions using Primality need not know how it works. This allows for abstraction of functionality - necessary for large software systems.

Parameters

The function accepts parameters defining inputs to the problem class. The return value is the output.

  n (parameter)
      |    +-----------+
      +--->|           | /
           |           |/|
           | Primality | |
           |           |\|----+
           |           | \    v
           +-----------+    0 or 1 (return value)
  x and y (parameters)
      |    +-----------+
      +--->|           | /
           |           |/|
           | Addition  | |
           |           |\|----+
           |           | \    v
           +-----------+    sum of x and y (return value)

Function definition

A function definition describes the function's parameters and return value and how to compute the return value.

  public static int Primality(int n) {
	...
  }

public and static are required, meaningless words. The first int is the type of the return value. Primality is the name of the function. The parameter type and name is int n. And the stamentes computing the return value go between the braces.

To use a function, we can call it in an expression.

  if(Primality(query) == 1) {
	...
  }

Function definition (cont'd.)

Let's see a complete definition of Primality using Prime-Test-All.

  public static int Primality(int n) {
	int i;

	i = 2;
	while(i * i <= n) {
	  if(n % i == 0) {
		return 0;
	  }
	  i = i + 1;
	}
	return 1;
  }

The function body has two parts. The first part has the variable declarations. The second part holds assignment, conditional, and iterative statements. The return statement immediately exits the function with the specified return value.

Altering parameters

Normally, if a function changes a parameter it is not changed in the calling function.

  public static void SetToZero(int n) {
	n = 0;
  }

  public static void Test() {
	int num;

	num = 2;
	SetToZero(num);
	...

(The void return type means that the function does not return anything.)

In Test(), num is still two after the call to SetToZero().

Altering parameters (cont'd.)

With arrays, however, altering array elements does alter elements in the calling function.

  public static void SetToZero(int[] a) {
	int i;

	i = 0;
	while(i < a.length) {
	  a[i] = 0;
	  i = i + 1;
	}
  }

  public static void Test() {
	int[] arr = new int[30];

	SetToZero(arr);
	...

(The construct a.length gives the number of elements of a.)

After the call to SetToZero() in Test(), arr will have all zeroes:

       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  arr: |0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Another example function

  public static double ArrayTotal(double[] arr) {
	double sum;
	int i;

	sum = 0.0;
	i = 0;
	while(i < arr.length) {
	  sum = sum + arr[i];
	  i = i + 1;
	}
	return sum;
  }

Yet another example

  public static int[] ArraySum(int[] a, int[] b) {
	// we assume a.length == b.length
	int[] value = new int[a.length];
	int i;

	i = 0;
	while(i < a.length) {
	  value[i] = a[i] + b[i];
	  i = i + 1;
	}
	return value;
  }