Free visual Python lesson

Python Functions — Explained Visually

A function packages code under a name. Give it inputs, get a result. Press Play and watch Python jump into the function and back.

Python code

1def square(n):
2 result = n * n
3 return result
4
5ans = square(4)
6print(ans)

Variables / Memory

Memory is empty.

Execution

STEP 1 / 8Step 1 of 8. Program starts.

Program starts.

Output

Flow chart

RanNowTrueFalseChecked, not taken◇ decision · ▱ input/output
StartStartdef square(n)def square(n)ans = square(4)ans = square(4)print(ans)print(ans)EndEnd▶ square(n)▶ square(n)result = n * nresult = n * nreturn resultreturn resultreturnreturn

Defining and calling a function

def add(a, b):
    return a + b

result = add(3, 4)
print(result)  # 7

def only defines the function. Nothing runs until you call add(3, 4). Then a becomes 3, b becomes 4, and return sends 7 back.

print vs return

print shows a value on screen. return hands the value back so the rest of your program can use it.

Common beginner mistakes

  • Forgetting the brackets when calling: add instead of add(3, 4).
  • Forgetting return, so the function gives back None.
  • Using a variable made inside the function from outside it (scope).

Practice exercises

Try each one yourself first, then open it in the Visualizer to watch every line run.

Exercise 1

Square a number

Write a function square(n) that returns n * n, then print square(6).

def square(n):
    # return the answer
    pass

print(square(6))
Hint

Replace pass with return n * n.

Show solution and explanation
def square(n):
    return n * n

print(square(6))  # 36

Calling square(6) jumps into the function with n = 6. return sends 36 back to the line that called it, where print shows it.

Watch the solution step by step

Exercise 2

Greeting with a default

Write greet(name, greeting="Hello") and call it with and without the second value.

def greet(name, greeting="Hello"):
    pass

greet("Sara")
greet("Omar", "Salam")
Hint

Print greeting and name inside the function.

Show solution and explanation
def greet(name, greeting="Hello"):
    print(greeting, name)

greet("Sara")
greet("Omar", "Salam")

When no greeting is passed, the default "Hello" is used. Each call gets its own fresh variables.

Watch the solution step by step

Exercise 3

Is it prime?

Write is_prime(n) that returns True or False, and test it on a few numbers.

def is_prime(n):
    # check divisors from 2 to n - 1
    pass

print(is_prime(7), is_prime(9))
Hint

Return False as soon as a divisor is found; return True after the loop.

Show solution and explanation
def is_prime(n):
    if n < 2:
        return False
    for d in range(2, n):
        if n % d == 0:
            return False
    return True

print(is_prime(7), is_prime(9))  # True False

return ends the function immediately. For 9 the loop stops at d = 3; for 7 it checks every divisor and reaches return True.

Watch the solution step by step

Try your own code

Paste any code into the free visualizer, or sign up free for all visual lessons with quizzes and progress tracking.