✕ Close

Week 16 · 16.4 · Thu, Nov 19 · The Bigger Picture: ACT-style reading

Write It Once, Call It Often: From EDSAC's Subroutines to Java's Methods

1 This section explains how early programmers learned to write a piece of a program once and use it many times, and how Java does the same thing with methods.

A Library on Paper Tape

2 On May 6, 1949, a computer called EDSAC ran its first logged program at the University of Cambridge's Mathematical Laboratory. That program worked out the squares of the numbers from 0 to 99. Programs for EDSAC were punched as holes in paper tape, and many of them needed the same jobs done again and again, such as working out a sine or doing arithmetic with complex numbers. Writing those steps out fresh in every program took time, and every fresh copy was one more place for a mistake to hide.

Figure 1. A painting of EDSAC at the University of Cambridge's Mathematical Laboratory, which ran its first logged program on May 6, 1949. Its logic was built from vacuum tubes, and small round screens could show what was in its memory.
Figure 2. A painting of five-hole punched paper tape, the kind EDSAC read its programs from. Each row of holes across the tape stands for one character, and the line of small sprocket holes pulls the tape through the reader.

3 The Cambridge team's answer was a library. During 1948 and 1949 they began building a library of subroutines, which are named pieces of a program that each do one job and can be used by any program that needs that job. By 1951 the library held 88 subroutines, kept as punched paper tape in a filing cabinet. That year, Maurice Wilkes, David Wheeler and Stanley Gill published The Preparation of Programs for an Electronic Digital Computer, which is described as the first book on computer programming. It included a catalog that told a programmer how to use each subroutine in the library.

Figure 3. By 1951 the Cambridge library held 88 subroutines, kept as punched paper tape in a filing cabinet.

Jumping In and Coming Back

4 A subroutine is useful only if the program can leave its own steps, run the subroutine, and then pick up exactly where it left off. David Wheeler, a Cambridge student who earned his PhD there in 1951, worked out how to do that on EDSAC. His method is called the closed subroutine: the subroutine is kept in one place, and the program jumps into it whenever the job is needed. The jump marked the place it came from so that the subroutine could return there, and it became known as the "Wheeler jump." In 2003 the Computer History Museum named Wheeler a Fellow for his invention of the closed subroutine.

5 With a closed subroutine, a program that needs a job done five times does not hold five copies of the steps. It holds one copy and five jumps into it. Each time the subroutine finishes, control goes back to the place right after the jump that called it, and the program goes on from there. Figure 4 shows one copy with three jumps into it.

Figure 4. A closed subroutine: the program keeps one copy and jumps into it three times, and each time control comes back to the step after the jump.

Java's Methods Work the Same Way

6 In Java, a named piece of code like this is called a method. A method call is the line that sends the program into a method. Oracle's Java tutorial says that a method returns to the code that invoked it when it completes all the statements in the method. A method declared void does not send a value back; it does its job, such as printing, and then control returns to the caller.

7 A call can also hand values to the method. The tutorial explains that a parameter is one of the variables named in a method's declaration, and an argument is an actual value passed in when the method is invoked. Each argument must match its parameter in type and order. In the program below, team is the parameter, and "Saints" and "Pelicans" are arguments.

1public class Main
2{
3    public static void cheer(String team)
4    {
5        System.out.println("Go " + team);
6    }
7
8    public static void main(String[] args)
9    {
10        cheer("Saints");
11        cheer("Pelicans");
12        System.out.println("Done");
13    }
14}
Go Saints
Go Pelicans
Done

The parts of this program

  • class Main (lines 1–14) is the container. Java keeps every method inside a class, so every program has at least one. Here the class holds the methods, and the program is named after it.
  • cheer (lines 3–6) is a method: a named set of steps inside the class. Its lines run only when a call sends the program into it.
  • main (lines 8–13) is the method Java runs first. The program starts at main's first line and ends when main finishes.

The words in the header

  • public: other code is allowed to use this method. Every method in CS I is public.
  • static: the method is called by its name alone, with no object. (Objects come in AP CSA.)
  • void (where the return type goes): this method gives nothing back. It does its steps, often printing, and then the call is over. A void method has no return value, so a call to it cannot be printed or stored.
  • cheer (line 3) is the method's name. A call uses the name: cheer(...).
  • (String team): the parameter. team is a box that holds text. Each call fills it with the value in the call's ( ), the argument.
  • public static void main(String[] args): the same words in front of every main. void: main gives nothing back. String[] args: main's parameter, a list of Strings a program can be handed when it starts; CS I never uses it.

8 The program starts at main. Line 10 calls cheer, so "Saints" goes into team, line 5 prints Go Saints, and control comes back to main. Line 11 calls cheer again, this time with "Pelicans," and then line 12 prints Done.

Figure 5. A Java method call works the same way: cheer("Saints") sends the argument "Saints" into the parameter team, cheer prints Go Saints, and control comes back to the next line of main.

9 Today's question asks how a programmer could write a few lines only once and still have them run five times. The lines go into one method, and the program calls that method five times; each call jumps in, runs the lines, and comes back to the line after the call, just as Wheeler's closed subroutines did on EDSAC. On today's bench, each line is clicked in the order it runs, into each method and back.

Figure 6. Each argument's value goes into the method's parameter, and inside the method the parameter is read as that value.

Summary

10 EDSAC's programmers at Cambridge kept a library of subroutines, 88 of them by 1951, so that common jobs were written once and used by many programs. David Wheeler's closed subroutine let a program jump into one copy of a subroutine and come back to where it left off. A Java method works the same way: a method call sends control into the method, the arguments land in its parameters, and when the method finishes, control returns to the line after the call.

Sources: University of Cambridge Computer Laboratory, A brief informal history of the Computer Laboratory (EDSAC timeline) and David Wheeler obituary; IEEE Computer Society, David J. Wheeler (Computer Pioneers); The Preparation of Programs for an Electronic Digital Computer (Wikipedia); Oracle, The Java Tutorials: Returning a Value from a Method and Passing Information to a Method or a Constructor; course repository, snippets run with the course JDK.

ACT-style reading · 9 questions

Read the passage on the left. Answer each question by clicking a choice: the right answer turns green, and a wrong one tells you why. Questions point to paragraph numbers, as on the ACT.

1. Main idea

The main purpose of the passage is to:

2. Detail

According to paragraph 2, what did EDSAC's first logged program do?

3. Detail

According to paragraph 3, how was EDSAC's library of subroutines kept by 1951?

4. Words in context

As it is used in paragraph 3 ("It included a catalog that told a programmer how to use each subroutine"), the word catalog most nearly means:

5. Detail

According to paragraph 4, what did the "Wheeler jump" do that made a closed subroutine work?

6. Inference

Based on paragraph 5, a program uses a closed subroutine for one job three times. How many copies of the subroutine's steps does the program hold?

7. Detail

According to paragraph 7, in the call cheer("Saints"), what is "Saints"?

8. Detail

According to paragraph 8, what happens right after line 5 prints Go Saints?

9. Synthesis

Which sentence from the passage best answers how a programmer could write a few lines only once and still have them run five times?