Unit 3 • Lesson 2

Defining and Calling Functions

Overview

Learn how to create functions with the def keyword and call them from anywhere in your code. You'll see how indentation, naming, and parentheses define a function's body and trigger its execution, and practice writing your first custom functions from scratch.

Beginner 15–20 min

What You Will Learn in This Lesson

By the end of this lesson, you will know:

  • How to define functions: Use the def keyword to create your own functions.
  • Function syntax: Understand the structure of function definitions including the colon and indentation.
  • How to call functions: Execute functions by writing their name followed by parentheses.
  • Function naming: Learn best practices for naming your functions clearly and descriptively.

Why This Matters

Defining your own functions is one of the first steps toward writing professional Python code. Functions let you organize your code into reusable pieces, making your programs easier to read, test, and maintain. Once you can define and call functions, you can start building more complex programs.

Step 1: Defining a Function

To create a function in Python, you use the def keyword followed by the function name and parentheses. The function body (the code that runs when you call it) goes on the next line, indented.

Basic Function Definition
def greet():
    print("Hello!")
    print("Welcome to Python!")
1

Start with def

The keyword def tells Python you're defining a function. It's short for "define" and is required to create a function.

2

Choose a Function Name

After def, write the function name. Use lowercase letters and underscores, like greet or calculate_total. The name should describe what the function does.

3

Add Parentheses

After the function name, add parentheses (). These are required even if your function doesn't take any inputs (we'll learn about inputs in the next lesson).

4

Add a Colon

End the definition line with a colon :. This tells Python that the function body is coming next.

5

Write the Function Body

On the next line, indent and write the code that should run when the function is called. All code inside the function must be indented consistently (typically 4 spaces).

Important: Defining a function doesn't run it - it just tells Python what the function does. To actually execute the code inside a function, you must call it (write its name with parentheses). Think of defining a function like writing a recipe - the recipe doesn't cook the food, you have to follow it!

Mini Practice #1: Define Your First Function

Try It Yourself

Try defining a simple function. Notice that nothing happens until you call it:

Press Run to see output

What happened? When Python first encounters def greet():, it doesn't run the code inside the function - it just remembers that a function named greet exists. That's why "Function defined, but not called yet" prints first. Then, when Python sees greet() (the function call), it jumps to the function definition and runs all the indented code inside it. That's when "Hello!" and "Welcome!" are printed. Finally, Python continues with the rest of the program and prints "Function called!". This shows the difference between defining a function (telling Python what it does) and calling a function (actually running it).

Step 2: Calling a Function

To execute a function, you write its name followed by parentheses. This is called "calling" or "invoking" the function. When you call a function, Python jumps to the function definition, runs all the code inside it, and then returns to where you called it.

Calling a Function
def greet():
    print("Hello!")

greet()  # This calls the function
greet()  # You can call it multiple times
greet()  # Each call runs the function again

How Function Calls Work

When Python sees greet(), it looks for a function named greet that you've defined earlier in your code. It then executes all the code inside that function's body. After the function finishes, Python continues with the next line after the function call. You can call a function as many times as you want - each call runs the function independently.

Step 3: Function Naming Rules

Function names follow the same rules as variable names in Python. They should be descriptive and follow Python naming conventions:

Good Function Names
# Use lowercase letters and underscores
def calculate_total():
    pass

def get_user_name():
    pass

def print_welcome_message():
    pass
Avoid These Naming Patterns
# Don't start with numbers
def 2calculate():  # ERROR!

# Don't use spaces
def calculate total():  # ERROR!

# Don't use Python keywords
def if():  # ERROR! 'if' is a keyword
def print():  # ERROR! 'print' is a built-in function

Best Practice: Function names should clearly describe what the function does. Use verbs like calculate, get, print, or check to make it obvious. For example, calculate_total is better than ct or func1 because it tells you exactly what the function does.

Mini Practice #2: Multiple Function Calls

Try It Yourself

Notice how you can call a function multiple times, and each call runs independently:

Press Run to see output

What happened? Each time Python sees print_info(), it runs the entire function body. That's why you see the same three lines printed three times. This demonstrates one of the key benefits of functions: you write the code once (in the function definition), but you can use it many times (by calling it multiple times). Without functions, you'd have to write those three print statements three times, which is repetitive and harder to maintain.

Step 4: Functions Can Be Called Anywhere

Once you define a function, you can call it from anywhere in your code - before or after other code, inside loops, inside other functions (we'll learn about this later), or even multiple times. The only rule is that the function must be defined before you try to call it.

Calling Functions in Different Places
def greet():
    print("Hello!")

greet()  # Call it here

for i in range(3):
    greet()  # Call it inside a loop

greet()  # Call it again here

Function Definition Order

Python reads your code from top to bottom. When it encounters a function definition, it remembers it but doesn't run it. When it encounters a function call, it looks for the function definition that came before it. This means you must define a function before you call it - if you try to call a function before it's defined, Python will give you an error.

Step 5: Empty Functions

Sometimes you want to define a function but haven't written the code yet. Python requires that function bodies have at least one line, so you can use the pass keyword as a placeholder. This tells Python "this function does nothing for now, but it's valid syntax."

Using pass as a Placeholder
def calculate_total():
    pass  # Function does nothing yet

# You can call it, but nothing happens
calculate_total()

When to Use pass: Use pass when you're planning out your program structure and want to define functions before writing their code. It's also useful when you need a function that does nothing (though this is rare). Later, you can replace pass with the actual function code.

End-of-Lesson Exercises

Exercise 1: Create and Call a Function

Create a function named introduce that prints your name and a hobby. Then call the function twice.

Use def introduce(): followed by print statements, then call introduce() twice.

Write your code above and click "Check Answer" to verify it's correct.

Exercise 2: Function with Multiple Statements

Create a function named display_info that prints three different pieces of information (like your name, age, and favorite color). Call it once.

Your function should have three print statements inside it.

Write your code above and click "Check Answer" to verify it's correct.