Chapter 6: Function calls

Calling functions

face Josiah Wang

Think of a coffee machine. You give it some input (coffee beans, water) and press a button. The output is some freshly brewed coffee. You do not need to know how it works inside. You only need to know that the coffee machine makes you fresh coffee if you provide it with some coffee beans and water.

A coffee machine example

Similarly, to call (use) a function, you will only need to know:

  1. Its name
  2. What the function does (at a high level), for example “compute the absolute value”
  3. What inputs (arguments) the function can take
  4. What output does the function return

A function call

Here is a quick quiz to test your understanding.

The quiz uses the Python built-in function abs() which takes a number and returns the absolute value of the given number.

1 2 3

Question 1

What is the input (argument) in the function call abs(-42)?

-42

Question 2

What is the output (return value) of the function call abs(-42)? Type Error if it causes an error.

42

Question 3

What does the function call abs("hello") return? Type Error if it causes an error.

Error
Explanation:

Python will give you a TypeError: bad operand type for abs(): 'str'. The error message should be self-explanatory - you cannot pass a str as an input argument to the function call abs(). The function just does not know what to do with it!