Writing Your Own Functions

Python 3.14 · ✓ verified by execution on 2026-07-22

As your programs grow, you’ll find yourself writing the same steps again and again. A function lets you name a block of code once and then run it whenever you like, just by calling its name. You define one with the def keyword:

python
def say_hello():
    print('Hello World')

say_hello()
Output
Hello World

Passing Arguments

Functions become far more useful when you feed them data. The values you pass in are called arguments; inside the function they become parameters you can use by name.

python
def greet(name):
    print(f'Hello {name}')

greet('Alice')
Output
Hello Alice

Returning Values

Printing shows a value on screen; returning hands it back to the caller so the rest of your program can use it. The return statement does exactly that.

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

result = add(5, 3)
print(result)
Output
8

Local Variables

The execution of a function introduces a new symbol table used for the local variables.

python
def scope_test():
    x = 10
    print(x)

scope_test()
Output
10

Common Misconceptions

A function executes its code as soon as it is defined. Defining a function only creates it. The code inside does not run until the function is called.

Variables created inside a function can be accessed outside of it. Variables defined inside a function are local to that function and disappear when the function finishes.

A function must always have a return statement. If a function doesn’t have a return statement, it implicitly returns None.

Edge Cases

Under the Hood

Watch how function calls, arguments, and local variables are managed in memory:

python · visualize
def square(num):
    result = num * num
    return result

val = square(4)

Check yourself

What keyword is used to define a function in Python?

Reveal answer

def — The def keyword is used to introduce a function definition.

If a function does not have a return statement, what does it return?

Reveal answer

None — If a function doesn't have a return statement, it implicitly returns None.

Where are variables that are assigned inside a function stored?

Reveal answer

In a local symbol table — Variables assigned inside a function are local to that function's execution.

Challenges

Challenge 1 +50 XP

Write a function called `multiply` that takes two parameters, multiplies them together, and returns the result. Then call it with 4 and 5 and print the result.

python
  • Test 1 — expects "20\n"
Need a hint? (−25% XP)

Use the def keyword, and remember to return a * b.

Show solution (0 XP)
def multiply(a, b):
    return a * b

print(multiply(4, 5))

Challenge 2 +100 XP

Create a function `say_goodbye` that takes a `name` parameter and prints 'Goodbye ' followed by the name. Call it with 'Bob'.

python
  • Test 1 — expects "Goodbye Bob\n"
Need a hint? (−25% XP)

Use an f-string to easily insert the name parameter into the printed string.

Show solution (0 XP)
def say_goodbye(name):
    print(f'Goodbye {name}')

say_goodbye('Bob')