Python Functions

Let’s look at functions

  1. Notice

    def creates a callable function. Its body executes when called, and return passes a result back to the caller.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
def double(number):
    return number * 2
result = double(3)
print(result)
Live state

Top-level scope

double→Object 1
result→Object 3
Object 1 · functionfunction doubleObject 3 · int6
This step

def creates a callable function. Its body executes when called, and return passes a result back to the caller.

A call carries a value in and a result out

Arguments enter the function, its body runs in a local scope, and return carries a result back to the caller.

Image brief

Alt text
Calling double with three binds the parameter number to three, computes number times two, and returns six to result.
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python Functions". Teaching focus: Arguments enter the function, its body runs in a local scope, and return carries a result back to the caller. Depict exactly the relationships and labels in this reference diagram: double(3) | v PARAMETER: number = 3 BODY: number * 2 RETURN: 6 | v result = 6 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: Calling double with three binds the parameter number to three, computes number times two, and returns six to result.

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
def double(number):
    return number * 2
result = double(3)
print(result)
Live state

Top-level scope

double→Object 1
result→Object 3
Object 1 · functionfunction doubleObject 3 · int6
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
6

What do you expect this program to print?

What does this example display?

def double(number):
    return number * 2
result = double(3)
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

6

Follow the printed values in order. The output is: 6

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

def creates a callable function. Its body executes when called, and return passes a result back to the caller.

def creates a callable function. Its body executes when called, and return passes a result back to the caller.

More examples and reference

As you write more code, you’ll notice that some parts of your code repeat. Instead of writing the same thing again and again, we can group that code into something called a function.

Functions let you give a name to a block of code. Later, you can use that name to run the code whenever you want — as many times as you want!

Why Do We Need Functions?

  • To avoid repeating the same code again and again.
  • To make our code clean and easy to understand.
  • To organize code into smaller, manageable pieces.

Basic Syntax of a Function

This is how you define a simple function in Python:

def function_name():
    # block of code
    print("Hello!")

Explanation:

  • def is a keyword used to define a function.
  • function_name is the name you choose for your function (no spaces, only letters, numbers, and underscores).
  • () is where we can pass information (called parameters) — more on that below.
  • The code inside the function is written with indentation (usually 4 spaces).

How to Call (Use) a Function

Once the function is defined, you can use it like this:

def say_hello():
    print("Hello!")

say_hello()
Hello!

Explanation: The line say_hello() tells Python to run the code inside the function.

Functions with Parameters (Inputs)

You can also send information to a function by using parameters:

def greet(name):
    print("Hello, " + name + "!")

greet("Arjun")
greet("Bob")
Hello, Arjun!
Hello, Bob!

Explanation: The word you pass inside greet() goes into the name variable inside the function.

Functions That Return a Value

Sometimes you want a function to calculate and give back a value. For that, we use the return keyword:

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

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

Explanation: The function add() calculates 3 + 4 and returns 7, which we print.

Different Types of Functions

  • No input, no return: Just does something like print
  • With input, no return: Takes input and prints result
  • With input and return: Takes input and gives back result

Examples of All Types

# No input, no return
def show_message():
    print("Welcome!")

# Input, no return
def greet_user(name):
    print("Hi, " + name)

# Input and return
def multiply(x, y):
    return x * y

# Using the functions
show_message()
greet_user("Zara")
print(multiply(3, 5))
Welcome!
Hi, Zara
15

Conclusion

Functions are like mini-programs inside your main program. You can call them whenever you need them. This makes your code easier to read, write, and fix.

What to remember

Connect what you noticed in the example with the idea below.

Remember

def creates a callable function. Its body executes when called, and return passes a result back to the caller.

Watch out for this idea

Defining a function immediately executes its body.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python Functions with Parameters →