Skip to content

Defining and calling functions

Define a function with parameters and return, call it with arguments, and tell what it returns from what it prints, including the None of a function without return.

It opens on

This program defines area(w, h) as return w * h and then runs print(area(3, 4)). Will it print 7, 12 or 34?

Why it matters

Programs are built from named pieces that can be run again with different values, and the commonest early bug with functions is a result that is printed when the caller needed it returned, which shows up as a mysterious None.

Good to know first

  • Values, types and expressions: w * h multiplies two numbers
  • Variables and assignment: a name is bound to a value
  • Input, output and type conversion: print shows a value on the screen

Step by step

  1. Define it, call it

    Trace a call of a function with parameters and return, binding the arguments to the parameters and giving the value the call becomes.

    Line 4 calls area(3, 4). The values 3 and 4 are the arguments, and they are bound to the parameters by position, so w is 3 and h is 4. The body returns 3 * 4 = 12, the call itself becomes 12, and x is bound to 12.

    the first row of the table

    A common mix-up

    The def line runs the function.

    In fact: It only defines it; the body runs at each call, and the first line to run in this program is line 4.

  2. Return is not print

    Say what a function that prints, and a function without return, gives back to its caller, and when a return ends a function.

    So line 4 puts 12 on the screen, and r is bound to None, and line 5 prints that None. The program shows 12 and then None.

    the table of the screen and r

    A common mix-up

    A function that prints its result gives that result to the caller.

    In fact: The caller gets None, and the value was only shown on the screen.

  3. One function calls another

    Trace a function that calls another function, and call a function with its arguments in the right order.

    The call cost(3, 4, 5) binds w to 3, h to 4 and price to 5. Its body needs area(w, h), which is area(3, 4), a second call; that call returns 12, and then cost returns 12 * 5, which is 60, so the program prints 60.

    A common mix-up

    The arguments are matched to the parameters by their names or by their size.

    In fact: They are matched only by position, so swapping them changes what each parameter holds.

Try it

With def double(n): return n * 2, what does print(double(7) + 1) show?Answer

15

Hint: The call becomes 14 first

With def f(x): y = x + 1, what does print(f(2)) show?Answer

None

Hint: There is no return

For def sub(a, b): return a - b, what does print(sub(4, 10)) show?Answer

-6

Hint: a is 4 and b is 10

What does print(area(5, 2) + 1) show?Answer

11, because the call area(5, 2) becomes 10, and then 10 + 1 is 11.

What must be added to twice so that print(twice(4)) shows 8?Answer

The word return in front of n * 2, so that the value is handed back.

Defining and calling functions

30 minutes every month. No card.