⚡ Functions

Learn how to organise code into reusable blocks that you can call whenever you need them.

What you'll learn:

  • How to define and call functions
  • Using parameters to pass data into functions
  • Return values - getting results back from functions
  • Local vs global variables - understanding scope

Key Concepts

Defining Functions

Use def to create a function. Give it a name and add parentheses.

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

Calling Functions

Use the function name with parentheses to run it.

greet() # Runs the function

Parameters

Variables inside the parentheses that receive data.

def greet(name):
    print(f"Hello {name}!")

Arguments

The actual values you pass when calling the function.

greet("Alice") # "Alice" is the argument

Return Values

Use return to send a result back.

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

Using Return Values

Store or use the returned value.

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

Local vs Global Variables

Understanding where variables can be accessed is crucial when working with functions.

Local Variables

Created inside a function

  • Only exist inside the function
  • Cannot be accessed from outside
  • Destroyed when the function ends
  • Parameters are local variables too
def calculate():
    total = 100   # Local variable
    return total

print(total)      # ERROR! total doesn't exist here

Global Variables

Created outside all functions

  • Can be accessed from anywhere
  • Exist for the entire program
  • Use global keyword to modify inside functions
  • Generally avoid when possible
score = 0          # Global variable

def add_point():
    global score   # Need 'global' to modify it
    score = score + 1

add_point()
print(score)       # 1
Best Practice: Prefer passing values as parameters and using return values instead of global variables. This makes code easier to understand and debug!

Learning Path

Follow these activities in order.

Key Vocabulary

function

A named block of code that performs a specific task. Can be reused multiple times.

parameter

A variable in the function definition that receives data. def greet(name):

argument

The actual value passed to a function when calling it. greet("Alice")

return

Sends a value back from the function to where it was called.

local variable

A variable created inside a function. Only exists within that function.

global variable

A variable created outside all functions. Can be accessed anywhere in the program.

scope

The area of a program where a variable can be accessed.

call

Running a function by using its name followed by parentheses. greet()

Quick Reference

functions_reference.py
# Defining a simple function
def say_hello():
    print("Hello!")

# Function with parameter
def greet(name):
    print(f"Hello, {name}!")

# Function with multiple parameters
def add(a, b):
    return a + b

# Function with return value
def calculate_area(width, height):
    area = width * height
    return area

# Calling functions
say_hello()                 # Hello!
greet("Alice")              # Hello, Alice!
result = add(5, 3)         # result = 8
size = calculate_area(4, 6) # size = 24

# Global vs Local
counter = 0  # Global

def increment():
    global counter
    counter = counter + 1  # Modifies global