CSCI A201/A597

Lecture Notes 26

Spring 2000


Arrays and procedures combined review.

We've posted a set of sample practical practice problems.

Today in class we'll go over procedures with arrays as parameters or return values. In class today we need to clarify a few things that have been presented before, so most of the things you will see in the notes today can also be found in the previous sets of lecture notes.

Let's define a method that adds two whole numbers.

If it returns the result it is also called a function. Let's decide on the nature of the two whole numbers that will be received by the function as inputs (let they be int's) and let's call the function add.

It will be easy to define it, but where do we put it? Place it in a class called Library, like we've been doing so far.

We now have this program:

public class A13 {
    public static void main(String[] args) {
	int result; 
	result = 1 + 2; 
	result = Library.add(1, 2); 
	result = 1 + (Library.add(1, 1)); 
	result = Library.add(1, Library.add(1, 1)); 
	result = Library.add(Library.add(-1, 2), Library.add(1, 1)); 
    }
} 

class Library {
    public static int add(int a, int b) {
	int result;
	result = a + b;
	return result; 
    } 
}
Notice that all assignments are setting result in main to 3.

You can verify this if you place a print statement after each assignment, printing result.

If a method can return an int, it can be invoked anywhere in an expression where its result (an int) would fit.

From now on we change the name of our library class and call it simply Stuff.

Can a function return an array?

The answer is yes:

public class A13 {
    public static void main(String[] args) {
	int[] v = Stuff.createArray(10); 
	System.out.println(v[3]); 
    } 
} 

class Stuff {
    public static int[] createArray(int size) {
	int[] n = new int[size]; 
	for (int i = 0; i < size; i++) {
	    n[i] = i; 
	} 
	return n; 
    }
}
Can we pass an array as a parameter?

The answer is yes, we do it as main does it:

public class A13 {
    public static void main(String[] args) {
	int[] v = Stuff.createArray(10); 
        Stuff.show(v); 
    } 
} 

class Stuff {
    public static int[] createArray(int size) {
	int[] n = new int[size]; 
	for (int i = 0; i < size; i++) {
	    n[i] = i; 
	} 
	return n; 
    }
    public static void show(int[] a) {
	for (int i = 0; i < a.length; i++) {
	    System.out.print(a[i] + " "); 
	} 
	System.out.println(); 
    } 
}
Is that the original array we are looking at?
public class A13 {
    public static void main(String[] args) {
	int[] v = Stuff.createArray(10); 
        Stuff.show(v); 
	Stuff.inflate(v); 
        Stuff.show(v); 
    } 
} 

class Stuff {
    public static int[] createArray(int size) {
	int[] n = new int[size]; 
	for (int i = 0; i < size; i++) {
	    n[i] = i; 
	} 
	return n; 
    }
    public static void show(int[] a) {
	for (int i = 0; i < a.length; i++) {
	    System.out.print(a[i] + " "); 
	} 
	System.out.println(); 
    } 
    public static void inflate(int[] v) {
	for (int i = 0; i < v.length; i++) {
	    v[i] += 1; 
	} 
    } 
}
Running this we obtain:
frilled.cs.indiana.edu%javac A13.java
frilled.cs.indiana.edu%java A13
0 1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 10 
frilled.cs.indiana.edu%
so the answer is yes.

Does it work the same with numbers?

public class A13 {
    public static void main(String[] args) {
	System.out.println("____________________________Passing arrays..."); 
	int[] v = Stuff.createArray(10); 
        Stuff.show(v); 
	Stuff.inflate(v); 
        Stuff.show(v); 
	System.out.println("____________________________Passing integers..."); 
	int n = 3; 
        Stuff.show(n); 
	Stuff.inflate(n); 
        Stuff.show(n); 
	System.out.println("____________________________Array names are actually addresses!"); 
    } 
} 

class Stuff {
    public static void show(int a) {
	System.out.println(a);
    } 
    public static void inflate(int v) {
	v = v + 1; 
    } 
    public static int[] createArray(int size) {
	int[] n = new int[size]; 
	for (int i = 0; i < size; i++) {
	    n[i] = i; 
	} 
	return n; 
    }
    public static void show(int[] a) {
	for (int i = 0; i < a.length; i++) {
	    System.out.print(a[i] + " "); 
	} 
	System.out.println(); 
    } 
    public static void inflate(int[] v) {
	for (int i = 0; i < v.length; i++) {
	    v[i] += 1; 
	} 
    } 
}
Let's run it:
frilled.cs.indiana.edu%javac A13.java
frilled.cs.indiana.edu%java A13
____________________________Passing arrays...
0 1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 10 
____________________________Passing integers...
3
3
____________________________Array names are actually addresses!
frilled.cs.indiana.edu%
So the answer is no, they don't act the same.

Both the array and the primitive type are passed by value (a copy of what they are is actually being passed to the procedure) but the array is used as a base address (to which the index acts as an offset) and this extra level of indirection allows us to access the original elements of the array.

This example also shows that more than one function (method) can be defined with the same name: what matters is the name of the function plus the number and types of the arguments.

We closed by stating a problem.

It's the sample problem 8 in the lab notes of last week (pract prep probs).

Sample Problem 8.

Write a function
int[] append(int[] a, int[] b)
that appends one array after another. For example, if a is
1 4 9 16
and b is
9 7 4 9 11
then append returns the array
1 4 9 16 9 7 4 9 11
We'll look at this next time, in class or during the practical review.


Last updated: Apr 18, 2000 by Adrian German