Skip to content
BENIBIX Learn

Functions in Python

Level 4 · Functions

How to write a function with def, call it, give it arguments and get a return value back. The built-in functions Python already has.

Course contentsLesson 12 of 18

After this lesson you can

  • Write a function with def and call it
  • Tell a parameter from an argument
  • Use return to hand a result back

13 mincovers 6 glossary entries

Worth reading first: For and While Loops

How to write a function and use it

A function is a named block of code that does one job. You write it once and use it as often as you like. In Python, def starts it: def area(width, height):, then the block. A program of 1,000 lines might hold 50 functions, each one short enough to read in a minute.

Running a function is called a function call. You write its name, then brackets: area(3, 4). Python jumps into the function, runs its block, then comes back to the line that called it. Defining a function does nothing on its own. Nothing happens until something calls it.

The names in the brackets of def are the parameters, here width and height. They are empty slots. The values you put in the brackets of a call are the arguments, here 3 and 4. When the call runs, width gets 3 and height gets 4. Parameters are the slots, and arguments fill them.

A return value is the result a function hands back. The line return width * height sends 12 back to the caller, which can store it: a = area(3, 4). A function ends the moment it reaches return. A function with no return hands back None.

Printing and returning are not the same. print shows a value on the screen, and then it is gone. return gives the value back to the code, so it can be stored, added or passed on. A function that only prints its answer cannot be used inside a bigger sum.

Python comes with about 70 built-in functions you can use with no import. You have met some already: print, len, int, str, type and range. Others include max, min, sum, sorted and round. Check the list before you write your own.

A common mistake is leaving off the brackets. area on its own names the function but does not run it. Python happily prints something like <function area> instead of an answer. If a value looks strange, check for the missing ().

python
def area(width, height):      # width and height are parameters
    return width * height

a = area(3, 4)                # 3 and 4 are arguments
print(a)                      # 12
print(area(5, 2) + 1)         # 11

print(max(4, 9, 2), len("hello"))   # built-in functions
# 9 5
def writes the function once. Each call fills the parameters with new arguments and gets a result back.
The values go into the slots, and the block runs. Then the result comes back to the line that called it.

Worked example

Lucy stops copying the same sum

Lucy's script works out the price with tax in 6 places, each with the same line of maths copied in.

She writes def with_tax(price): return price * 1.2, and replaces all 6 copies with a call. When the rate changes, she edits 1 line.

The words for this lesson6

Check yourself

What does a function hand back if it has no return line?

Finished this lesson?

Marking it complete also marks its 6 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.