Introduction to Functions and Basic Concepts
From the Functions curriculum
Introduction to Functions and Basic Concepts
TL;DR
Functions are like mini-machines that take an input, do something with it, and give you an output. You give them a name, define what they do, and then you can use them over and over. They help organize your code and avoid repeating yourself.
1. The Mental Model
Imagine a function as a special box. You put something in (the input), the box does its specific job, and then something comes out (the output). You don't always need to know how it works, just what it does.
2. The Core Material
What is a Function?

Photo by Efrem Efre on Pexels
A function is a block of code designed to perform a specific task. Think of it as a reusable piece of logic. Instead of writing the same steps multiple times, you write them once inside a function and then "call" (or "invoke") that function whenever you need it.
Here's how to think about its parts:
- Name: Every function needs a unique name so you can refer to it.
- Parameters (Inputs): These are the values you give to the function to work with. They're like placeholders for the actual data. Not all functions need parameters.
- Body: This is the code inside the function that does the actual work.
- Return Value (Output): This is the result the function gives back after it's done its job. Not all functions return a value; some just perform an action.
Why Use Functions?

Photo by Ann H on Pexels
You use functions for a few main reasons:
- Reusability: Write code once, use it many times.
- Organization: Break down complex problems into smaller, manageable pieces.
- Readability: Code is easier to understand when it's divided into logical functions.
- Maintainability: If you need to change how a specific task is done, you only change it in one place (the function).
Defining a Simple Function (Python Example)

Photo by Syirwan Ainu on Pexels
# Define a function called 'greet' that takes one parameter: 'name'
def greet(name):
message = "Hello, " + name + "!"
return message # The function gives back this message
# Now, let's use (call) the function:
greeting1 = greet("Alice")
print(greeting1) # Output: Hello, Alice!
greeting2 = greet("Bob")
print(greeting2) # Output: Hello, Bob!
In this example:
* greet is the function's name.
* name is the parameter. When you call it, like greet("Alice"), "Alice" is the argument passed to the name parameter.
* return message is what the function outputs.
graph TD
A["You need a task done (e.g., add two numbers)"] --> B{"Is there a function for this?"};
B -- "No" --> C["Define a new function:"];
C --> D["Choose a Name"];
C --> E["Decide on Parameters (Inputs)"];
C --> F["Write the Function Body (the 'how')"];
C --> G["Decide on Return Value (Output)"];
G --> H["Now you have a function!"];
B -- "Yes" --> I["Call the existing function"];
H --> I;
I --> J["Provide Arguments (Actual Inputs)"];
J --> K["Function runs"];
K --> L["Get the Return Value (Result)"];
Functions Without Parameters or Return Values

Photo by Ann H on Pexels
Functions don't always need inputs or outputs.
# Function without parameters or a return value
def show_welcome_message():
print("Welcome to our application!")
show_welcome_message() # Output: Welcome to our application!
# Function with parameters but no explicit return value (implicitly returns None)
def print_sum(a, b):
result = a + b
print("The sum is:", result)
print_sum(5, 3) # Output: The sum is: 8
3. Worked Example
Let's say you need to calculate the area of a rectangle multiple times in your program. Instead of repeating the length * width calculation, you can create a function.
# 1. Define the function
def calculate_rectangle_area(length, width):
"""
This function takes the length and width of a rectangle
and returns its area.
"""
area = length * width
return area
# 2. Use the function for different rectangles
# Rectangle 1: length = 10, width = 5
rect1_area = calculate_rectangle_area(10, 5)
print("Area of rectangle 1:", rect1_area) # Output: Area of rectangle 1: 50
# Rectangle 2: length = 7.5, width = 3
rect2_area = calculate_rectangle_area(7.5, 3)
print("Area of rectangle 2:", rect2_area) # Output: Area of rectangle 2: 22.5
# Rectangle 3: length = 20, width = 10 (reusing the function again!)
rect3_area = calculate_rectangle_area(20, 10)
print("Area of rectangle 3:", rect3_area) # Output: Area of rectangle 3: 200
In this example, the calculate_rectangle_area function handles the specific task of finding an area. You pass in different lengths and widths (arguments), and it always gives you back the correct area.
4. Key Takeaways
- Functions are named blocks of code that perform a specific task.
- They help make your code reusable, organized, and easier to understand.
- Functions can take inputs (parameters/arguments) and can produce outputs (return values).
- You "define" a function once and "call" or "invoke" it multiple times.
- Not all functions require parameters or return a value.
Common Mistakes to Avoid
- Forgetting to call the function: Defining a function doesn't make it run; you have to explicitly call it.
- Mismatching arguments and parameters: Passing the wrong number or type of arguments when calling a function can cause errors.
- Confusing
printwithreturn:printjust shows something on the screen;returnactually sends a value back from the function for further use. - Naming functions poorly: Choose clear, descriptive names that indicate what the function does.
5. Now Try It
Exercise: Write a Python function called fahrenheit_to_celsius that takes one temperature in Fahrenheit as input and returns the equivalent temperature in Celsius. The formula is C = (F - 32) * 5/9.
What to do:
1. Define the function fahrenheit_to_celsius with one parameter (e.g., fahrenheit_temp).
2. Inside the function, apply the conversion formula.
3. Return the calculated Celsius temperature.
4. Call your function with at least two different Fahrenheit temperatures (e.g., 32°F and 212°F) and print the results.
What success looks like:
Your output should show something like:
0.0
100.0
(Or whatever you get for your chosen temperatures, accurately converted).
Frequently asked about Introduction to Functions and Basic Concepts
Study this next
Get the full Functions curriculum
Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.
Create Free Account