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
globalkeyword 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
Learning Path
Follow these activities in order.
1. Predict: Function Output
Predict what functions will output when called.
2. Study: Worked Example
See how to write functions step by step.
3. Investigate: Trace Function Calls
Follow values through function calls and returns.
4. Modify: Arrange the Code
Put code blocks in order to create functions.
5. Test: Knowledge Quiz
Check your understanding of functions.
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
# 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