Concepts / Arrays of Integers

Arrays of Integers

A subroutine parameter lets the same subroutine perform its task on different inputs, with the caller choosing the input on each call.

  • Programming

Why a Subroutine Needs Inputs

Imagine a subroutine that prints a line of output. Every time the program calls it, the same line appears. That is useful, but limited. Most useful subroutines need to know something about the situation they are working in: which number to display, which list to process, or which label to attach to a result. A subroutine parameter is the mechanism that supplies that information. The caller hands a value to the subroutine, and the subroutine uses that value while it does its job.

Keep this question in mind as you read: if the caller can choose the input on every call, how can one piece of code produce different results? The answer follows from the way a parameter carries information into the subroutine.

Tracing One Call to a Print Routine

Consider a concrete need. A subroutine's job is to display every value in an integer array in a neat format: the values separated by commas and wrapped in a pair of square brackets. Before writing it, trace what has to happen. The caller makes the call and supplies an array. The subroutine receives that array through its parameter. It then prints an opening bracket, prints each value with the separators handled correctly, and prints a closing bracket. Control then returns to the caller.

CallerprintArrayConsole output1. call with the int[ ] argument2. print [, each value, ]3. return control to the caller
In what order do the caller, the subroutine call with its argument, the subroutine's work, and the return back to the caller happen?

The sequence matters. The argument must be supplied at the moment of the call, because the subroutine can only work on the array it was given. The printing happens inside the subroutine's own work, and only after that work finishes does control go back to the caller.

Passing a Whole int[ ] as One Parameter

A parameter does not have to hold a single number or a single word. Its type can be an array type. When that happens, the caller does not pass the array's elements one at a time. Instead, the entire array of values travels into the subroutine as a single parameter. The subroutine then has access to all of the values in that array through its one parameter.

Definition

int[ ] is the notation for an array of integers used as a parameter type. A parameter declared as int[ ] receives one whole array from the caller, even though that array holds many values.

The subroutine has to know which array to display, and the way to tell it is to give it a parameter of type int[ ]. The caller supplies the array when it makes the call, and the subroutine works on that array. The positions of the values run from 0 up to the array's length minus one, and a loop can visit each position in turn.

values[0]4values[1]8values[2]15
How do the positions 0 through length-1 map onto the elements of an int[] that the subroutine loops over?

Writing the Print Routine

The following routine is a generated illustration of the task described above. It takes one int[ ] parameter, prints an opening bracket, prints each value with a comma before every value except the first, and prints a closing bracket. The commas are handled by a single check inside the loop, which is the careful separator handling the task requires: no comma before the first value, and a comma before each later value.

java
Output
[4, 8, 15]
Notes on the code
  • The parameter int[] values receives the whole array from the caller.
  • The loop runs from position 0 to values.length - 1.
  • The if check prints a comma only before values after the first one.

Predicting the Output Before Revealing It

What do you think happens?

Given the printArray routine above, what does the caller see on screen if the array is {4, 8, 15}?

  • 4 8 15

  • [4, 8, 15]

  • [4,8,15]

  • [, 4, 8, 15]

Reveal answer
Answer:

[4, 8, 15]

The opening bracket prints first, then 4 with no leading comma, then a comma and space before 8 and before 15, and finally the closing bracket. The commas come only between values.

Same Routine, Different Inputs

The printArray routine does not change between calls. What changes is the input the caller passes. A subroutine parameter lets the same subroutine perform its task on different inputs, with the caller choosing the input on each call. Two calls with different arrays therefore produce two different lines of output from one piece of code.

Input {4, 8, 15}Output [4, 8, 15]Input {10, 20}Output [10, 20]printArrayprintArray
How does one subroutine produce different results when the caller passes different arrays on separate calls?

Notice that the second call does not require a second routine. The caller changed only the argument. This is the reuse that parameters make possible: one subroutine, written once, serves every array the caller hands it.

Where the Print Routine Fits in a Program

A subroutine's task is always a subtask of some larger program. A program that does a complicated job is usually divided into smaller pieces, and each piece is handled by a subroutine that does one clearly described thing. The printArray routine is one such piece: a program that, say, computes and reports results can hand the reporting work to this subroutine.

Deciding how to split a large job into those pieces is a separate design skill from writing any single piece. This article focuses on writing the pieces. How the pieces fit together into a whole program is a question that comes back later in the course.

Mistakes Beginners Make with Array Parameters

  • Common mistake

    Printing a comma before the first value

    What it looks like
    Output such as [, 4, 8, 15] instead of [4, 8, 15]
    Why it fails

    The separator rule is no comma before the first value and a comma before each later value.

    Do this instead

    Guard the separator with a check such as if (i > 0) so the comma prints only before later values.

  • Common mistake

    Forgetting that the parameter holds the whole array

    What it looks like

    Writing the subroutine to expect one number when the caller passes an int[ ]

    Why it fails

    A parameter whose type is int[ ] receives an entire array as a single parameter, not one value.

    Do this instead

    Declare the parameter as int[ ] and use values.length to know how many positions to visit.

  • Common mistake

    Expecting one routine to know which array to use without being told

    What it looks like

    Calling printArray() with no argument and expecting it to find the right data

    Why it fails

    The subroutine works on the input the caller supplies on each call.

    Do this instead

    Pass the array as the argument at the call site.

  • Common mistake

    Trying to write a subroutine for a whole program at once instead of one assigned task

Practice: Tracing a Second Call

EASY

Using the printArray routine from this article, what is printed by the statement printArray(new int[] {10, 20}); and then by the statement printArray(new int[] {7})? Write both outputs exactly, including brackets and commas.

Hints
  • Work through the loop one position at a time and note where the comma check prevents a leading comma.
  • A one-value array has no commas at all between values.
MEDIUM

Write a small subroutine that takes an int[ ] parameter and prints only the first and last values, separated by a dash and wrapped in square brackets, for example [4-15]. Explain how your subroutine would behave differently for a one-value array.

Hints
  • Use the array's length to find the last position.
  • Think about what the first and last position are when the array has only one value.

Key Takeaways

  • A subroutine parameter carries information from the caller into the subroutine, so the same subroutine can perform its task on different inputs.
  • A parameter of type int[ ] receives an entire integer array as one single parameter rather than as separate values.
  • A print routine for an int[ ] must place commas only between values, with brackets around the whole list.
  • A subroutine is always a subtask of a larger program, and splitting a program into subtasks is a separate design skill from writing each piece.