Skip to content
ExamSpine

Lesson 4.5

Several parameters; order matters

Part 4: FunctionsReading, about 5 min2 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.

Several parameters; order matters

By the end of this you will be able to write a function that takes several values, call it with the values in the right order, and spot a call that has them the wrong way round.

You have already met a function with two parameters of different types, text and a number. Swapping those two values in a call gave an error, so the slip was caught. When two parameters have the same type, a swap gives no error at all. The program runs and gives a wrong answer.

Values are matched by position

A shop works out how much change to give back.

def change_due(paid, price):
    return paid - price

print(change_due(20, 13))
print(change_due(13, 20))
public static void main(String[] args) {
    System.out.println(changeDue(20, 13));
    System.out.println(changeDue(13, 20));
}

static int changeDue(int paid, int price) {
    return paid - price;
}

The first value in a call goes to the first parameter, the second value to the second parameter, and so on. Only the position counts.

Values in the callpaid holdsprice holdsReturned
20, 1320137
13, 201320-7

So this prints 7, then -7. Both parameters hold whole numbers, so nothing stops the second call: the computer cannot know which number you meant as the price.

Names in the call do not help

Matching by position holds even when the variables in the call have the same names as the parameters.

def change_due(paid, price):
    return paid - price

paid = 20
price = 13
print(change_due(price, paid))
public static void main(String[] args) {
    int paid = 20;
    int price = 13;
    System.out.println(
        changeDue(price, paid));
}

static int changeDue(int paid, int price) {
    return paid - price;
}

This prints -7. The call hands over two values, 13 and then 20, and they go into the parameters in that order: 13 into paid and 20 into price. The names of the variables in the call mean nothing to the function, because a call hands over values, not names. To get the order right, read the first line of the function's definition and write the values in the same order.

Some swaps hide

Here is a function where a swap makes no difference.

def total_cost(price, quantity):
    return price * quantity
static int totalCost(int price, int quantity) {
    return price * quantity;
}

With the values 3 and 4 in either order, it returns 12, because 3 * 4 and 4 * 3 are the same. Here a swapped call does no harm. But it also means a test can pass even when a function mixes up its parameters. When you test a function with several parameters, use values where swapping them would change the answer. For the change function, 20 and 13 give 7 one way and -7 the other, so a test with them shows at once whether the order is right.

Two common slips

  • Swapping two values of the same type. No error appears, and the answer is just wrong. Check each call against the first line of the definition, and test with values where a swap would show.
  • In Java, writing the type once for several parameters. Each parameter needs its own type, as in int paid, int price. Writing int paid, price does not compile. In Python, a parameter needs no type, so this slip cannot happen there.

Now try it yourself on the questions below.

Exercise 4.5.14.5.1 · Write itInteger power

Write a function that returns base raised to the power exponent, using a loop, not a built-in power function.

  1. base and exponent are whole numbers, chosen so that the answer is from -1000000000 to 1000000000, and exponent is at most 100.
  2. exponent is always 0 or positive.
  3. Anything raised to the power 0 is 1.
  4. Do not use ** (Python) or Math.pow (Java).

Only the number your function returns is checked.

def int_power(base: int, exponent: int) -> int

You cannot use these in this question:

  • pow()
  • .__pow__()
  • import math
  • import cmath

This question is about writing the loop yourself, so built-in power functions are not allowed.

Examples

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

  • int_power(2, 3) should return 8: 2 cubed is 8 (rule 1)
  • int_power(5, 0) should return 1: anything to the power 0 is 1 (rule 3)
  • int_power(3, 2) should return 9: 3 squared is 9 (rule 1)

public static int intPower(int base, int exponent)

Checking whether Java answers can run here.

You cannot use these in this question:

  • Math.pow()

This question is about writing the loop yourself, so built-in power functions are not allowed.

Examples

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

  • intPower(2, 3) should return 8: 2 cubed is 8 (rule 1)
  • intPower(5, 0) should return 1: anything to the power 0 is 1 (rule 3)
  • intPower(3, 2) should return 9: 3 squared is 9 (rule 1)
solution.pySolution.java

Exercise 4.5.24.5.2 · Write itRepeat a character

Write a function that returns a string made by repeating a character a given number of times.

  1. ch is a single character (a string of length 1).
  2. count is a whole number, always 1 or more.
  3. Return a string of exactly count copies of ch.
  4. Do not use string repetition (* on a string in Python, or .repeat in Java).

Only the string your function returns is checked.

def repeat_char(ch: str, count: int) -> str

You cannot use these in this question:

  • .__mul__()
  • .__rmul__()

This question is about using a loop, so string repetition shortcuts are not allowed.

Examples

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

  • repeat_char("*", 5) should return "*****": five stars (rule 3)
  • repeat_char("a", 1) should return "a": one copy (rule 3)
  • repeat_char("#", 3) should return "###": three hashes (rule 3)

public static String repeatChar(String ch, int count)

Checking whether Java answers can run here.

You cannot use these in this question:

  • .repeat()

This question is about using a loop, so string repetition shortcuts are not allowed.

Examples

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

  • repeatChar("*", 5) should return "*****": five stars (rule 3)
  • repeatChar("a", 1) should return "a": one copy (rule 3)
  • repeatChar("#", 3) should return "###": three hashes (rule 3)
solution.pySolution.java