✦ Python Foundations Functions module

Concept 15 · Defining Functions

Name reusable code.
Call it when you need it.

Use def, a function name, parentheses, a colon, and indented code to define a reusable action.

01 Learn02 Try03 Predict

A function stores instructions until you call it.

Use def, a function name, parentheses, a colon, and indented code to define a reusable action.

def greet():
    print("Hello from Python!")

greet()
greet()

Defining greet does not run its body. Each greet() call runs the indented print instruction.

Change one value or function call, then explain how the result changes.

↳

Keep this in mind: Read the function definition and its calls before predicting the result.

Try it
def greet():
    print("Hello from Python!")

# Change the message, then call greet more than once
greet()

Run the example, then change one value and observe the result.

Python Playground

Loading Python runtime…

Edit the example and run it in your browser.

defining_functions.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

Read the function definition and each call before choosing an answer.

def say_hi():
    print("Hi!")

print("Before")
say_hi()
print("After")
Choose the output
Sloby, the Python AI Tutor mascot

Your learning mentor

Ask Sloby about this concept

Describe what feels unclear and Sloby will guide your reasoning.

Hi! I'm Sloby. Let's explore this Python concept together.

What will this program print, and what is the difference between defining and calling show_message?

def show_message():
    print("Function body")

print("Defined")
show_message()
Python AI Tutor · Learn by understanding, not just by running code.