Skip to content
ExamSpine

Lesson 4.4

Parameter versus argument; returning versus printing

Part 4: FunctionsReading, about 5 min3 exercises in this lesson

Java answers can't run on Spine yet. You can still read Java, trace it, and put an answer's lines in order.

Parameter versus argument; returning versus printing

By the end of this you will be able to tell a parameter from an argument in any piece of code, and decide whether a function should return its answer or print it.

Parameter or argument

Two words describe the values that go into a function, and they are easy to mix up.

def describe(who, score):
    return who + " scored " + str(score)

print(describe("Ana", 7))
bonus = 4
print(describe("Ben", bonus + 5))
public static void main(String[] args) {
    System.out.println(describe("Ana", 7));
    int bonus = 4;
    System.out.println(
        describe("Ben", bonus + 5));
}

static String describe(String who, int score) {
    return who + " scored " + score;
}

who and score are the parameters: the names written in the brackets of the definition. "Ana" and 7 are the arguments of the first call: the values written in the brackets of a call. This prints Ana scored 7, then Ben scored 9.

An argument does not have to be a plain value. In the second call, the argument bonus + 5 is worked out first, to 9, and only that value is handed to score. The function never sees bonus or the +.

ParameterArgument
Where it is writtenIn the brackets of the definitionIn the brackets of a call
What it isA name that holds a value while the function runsA value, or something worked out to a value before the call
How many there areOne for each value the function takesOne for each parameter, in every call

A way to remember it: the parameter is the empty box the function has ready, and the argument is what a call puts in it.

Returning or printing

A function can give its answer in two ways. It can print it, which shows it on the screen and nothing more. Or it can return it, which hands the value to the caller, who then decides what to do with it. Here is a function that returns a length of time in minutes.

def total_minutes(hours, mins):
    return hours * 60 + mins

first = total_minutes(1, 30)
second = total_minutes(0, 95)
if first > second:
    print("The first film is longer")
else:
    print("The second film is longer")
public static void main(String[] args) {
    int first = totalMinutes(1, 30);
    int second = totalMinutes(0, 95);
    if (first > second) {
        System.out.println(
            "The first film is longer");
    } else {
        System.out.println(
            "The second film is longer");
    }
}

static int totalMinutes(int hours, int mins) {
    return hours * 60 + mins;
}

The first call returns 90 and the second returns 95. The caller compares them, finds that 90 > 95 is false, and prints The second film is longer. The same returned values could just as well be added together, stored, or printed.

Now imagine a version that prints the minutes instead of returning them. It would show 90 and 95 on the screen, but the caller would have nothing to compare: in Python, each call would give back None, and in Java, a void method's call cannot be stored in first at all.

So ask what the caller needs:

  • If the caller will do more with the answer (compare it, add to it, store it, pass it to another function), return it.
  • If showing it is the whole job, as with the first functions you wrote, print it.

A returned value can always be printed afterwards. A printed value can never be got back.

A common slip

Returning and printing are easy to confuse, because a returned value is often printed straight away, so both end with a value on the screen. The difference shows when the caller tries to use it. If a value appears on the screen but your variable holds None (Python), or the Java compiler says that void cannot be converted or that a return statement is missing, the function printed when it should have returned.

Now try it yourself on the questions below.

Exercise 4.4.14.4.1 · Answer itParam vs arg

Here is a function and a call to it.

def area(width, height):
    return width * height

result = area(5, 3)
static int area(int width, int height) {
    return width * height;
}

// in main:
int result = area(5, 3);

(a) State which names are parameters and which values are arguments. [1]

(b) State what would happen if the function printed width * height instead of returning it, and result was used later. [1]

[2]

Exercise 4.4.2 · Trace itTrace: return value

Complete the trace table for this Python code. [2]

Line 1: def add_one(x):
Line 2, indented 1 level: return x + 1
Line 3:
Line 4: a = 3
Line 5: b = add_one(a)
Line 6: c = add_one(b)
Line 7: print(c)

Fill in one row each time a line changes a, b or c, or prints something. The table already has the right number of rows. In each row, write only what that line changes; you can leave the other cells empty or copy their values down. Write values the way Python shows them: 3 and 3.0 are different, and text goes in quotes. In the output column, write exactly what appears on screen, with no quotes around it.

Rowabcoutput
1
2
3
4

Exercise 4.4.2 · Trace itTrace: return value (Java)

Complete the trace table for this Java code. [2]

Line 1: public static void main(String[] args) {
Line 2, indented 1 level: int a = 3;
Line 3, indented 1 level: int b = addOne(a);
Line 4, indented 1 level: int c = addOne(b);
Line 5, indented 1 level: System.out.println(c);
Line 6: }
Line 7:
Line 8: static int addOne(int x) {
Line 9, indented 1 level: return x + 1;
Line 10: }

Fill in one row each time a line changes a, b or c, or prints something. The table already has the right number of rows. In each row, write only what that line changes; you can leave the other cells empty or copy their values down. Write values the way Java shows them: 3 and 3.0 are different, and text goes in quotes. In the output column, write exactly what appears on screen, with no quotes around it.

Rowabcoutput
1
2
3
4

Exercise 4.4.34.4.3 · Write itClamp a value

Write a function that clamps a value into a range: if it is below the low end, return the low end; if it is above the high end, return the high end; otherwise return the value unchanged.

  1. value, low and high are whole numbers.
  2. low is always less than or equal to high.
  3. If value is less than low, return low.
  4. If value is greater than high, return high.
  5. Otherwise, return value unchanged.

Only the number your function returns is checked.

def clamp(value: int, low: int, high: int) -> int

Examples

Your function is tested on 8 cases: the 3 examples below, and 5 hidden ones that check the rules above.

  • clamp(5, 1, 10) should return 5: value is in range, returned unchanged (rule 5)
  • clamp(-3, 0, 100) should return 0: value is below low, return low (rule 3)
  • clamp(150, 0, 100) should return 100: value is above high, return high (rule 4)

public static int clamp(int value, int low, int high)

Checking whether Java answers can run here.

Examples

Your function is tested on 8 cases: the 3 examples below, and 5 hidden ones that check the rules above.

  • clamp(5, 1, 10) should return 5: value is in range, returned unchanged (rule 5)
  • clamp(-3, 0, 100) should return 0: value is below low, return low (rule 3)
  • clamp(150, 0, 100) should return 100: value is above high, return high (rule 4)
solution.pySolution.java