Functions
Functions
As programs grow larger, they often repeat the same group of instructions many times.
Instead of rebuilding those instructions over and over, programmers use functions.Imagine building a robot that needs to blink an LED every time it completes a task.
Without functions, you might repeatedly create the same sequence:
LED ON
↓
Wait
↓
LED OFF
Then later:
LED ON
↓
Wait
↓
LED OFF
And again:
LED ON
↓
Wait
↓
LED OFF
The program becomes longer and harder to maintain.
Instead, you can create a function called:
blink_led
The instructions are stored inside the function once.
Whenever the LED needs to blink, the program simply uses:
blink_led()This saves time, reduces repetition, and makes programs easier to understand.
What Is a Function?
A function is a named group of instructions that performs a specific task.
Instead of writing the same instructions multiple times, you can place them inside a function and call the function whenever those instructions are needed.
Simple Example
Function Name:
blink_led
Instructions inside the function:
Turn LED ON
Wait
Turn LED OFF
Whenever the program needs those instructions, it simply calls:
blink_led()Functions in Everyday Life
Functions are similar to following a recipe.
Imagine baking cookies.
You might have instructions like:
Mix Ingredients
Shape Cookies
Bake
Cool
Instead of explaining all those steps every time, you could simply say:
Bake Cookies
The name represents the complete procedure.
Functions work the same way.
They give a name to a set of instructions.
Functions Category
[INSERT CATEGORY IMAGE HERE: Functions Category]
Figure 5.1 - Blocks available in the Functions category.
The Functions category includes blocks for:
- Defining Functions
- Using Parameters
- Calling Functions
- Returning Values
- Conditional Returns
- Working with Global Variables
The overall workflow is:
DEFINE
↓
CALL
↓
OPTIONAL RETURN
Why Use Functions?
Functions provide several important advantages.
Reduce Repetition
Instead of writing the same instructions repeatedly:
LED ON
Wait
LED OFF
You define them once:
blink_led
Then use them whenever needed.
Improve Organization
Functions help divide large projects into smaller parts.
For example:
Main Program
↓
Read Sensor
↓
Process Data
↓
Display Information
↓
Control Outputs
Each task can become its own function.
Simplify Debugging
When programs are divided into smaller functions, problems are often easier to find and fix.
Function Blocks
Functions in Clu-Blocks generally follow this pattern:
Define Function
↓
Add Instructions
↓
Call Function
↓
Function Runs
Block 1: Define Function
[INSERT BLOCK IMAGE HERE: Define Function]
The Define Function block creates a new function.
Example:
def my_function():This creates a function named:
my_function
The instructions belonging to that function are placed underneath it.
Important
Creating a function does not automatically run it.
It only tells the program:
"These are the instructions to use later."
Visual Model
Function Name
↓
Instructions
↓
Stored for Later Use
Defining Is Not Calling
This is the most important concept in the Functions category.
DEFINE
Create the instructions.
def blink_led():Meaning:
What should happen?
CALL
Run the instructions.
blink_led()Meaning:
Do it now.
Memory Tip
DEFINE = Create the instructions
CALL = Run the instructions
Many beginners confuse these two steps.
Remember:
Defining creates the function.
Calling executes the function.
Block 2: Call Function
[INSERT BLOCK IMAGE HERE: Call Function]
The Call Function block runs the instructions stored inside a function.
Example:
my_function()When the program reaches this block:
- The function starts running.
- The instructions inside the function execute.
- The program returns to where it left off.
Visual Flow
Main Program
↓
Call Function
↓
Run Function Instructions
↓
Return to Main Program
Functions Without Inputs
The simplest functions do not require any information from the outside.
Example:
def blink_led():This type of function simply performs a task.
Examples:
- Blink an LED
- Play a sound
- Display a welcome message
- Move a robot forward
Action Functions
These functions focus on doing something.
Call Function
↓
Perform Action
No information needs to be returned.
Functions With Parameters
[INSERT BLOCK IMAGE HERE: Function With Parameter]
Functions can also receive information.
This information is called a parameter.
Example:
def show_score(x):The parameter is:
x
The function can use the value stored in x while it runs.
What Is a Parameter?
A parameter is information that enters a function.
Think of it as an input.
Visual Model
Value
↓
Function Input
↓
Function Uses Value
Real Example
show_score(150)The value:150
is sent into the function.
The function can then display, calculate, compare, or process that value.
Functions Without Parameters vs Functions With Parameters
Without Parameter
def blink_led():Always performs the same task.
With Parameter
def show_score(x):Works with information that is supplied when the function is called.
Comparison
| Function Type | Purpose |
|---|---|
| No Parameter | Perform a task |
| Parameter | Perform a task using supplied information |
Inputs → Processing → Outputs
As students gain experience, functions can be thought of as systems.
Every system has:
Input
↓
Process
↓
Output
Functions work exactly the same way.
Input
The parameter.
xProcess
The instructions inside the function.
Math
Logic
Comparisons
Display
Output
The returned result.
Block 3: Return
[INSERT BLOCK IMAGE HERE: Return]
Some functions produce information.
Instead of simply performing actions, they calculate a result and send it back.
Example:
return resultA return statement sends information out of the function.
Visual Flow
Function
↓
Process Information
↓
Return Result
Concept Example
Sensor Reading
↓
Function
↓
Process Reading
↓
Return Result
↓
Program Uses Result
Functions That Return Values
These functions answer a question or perform a calculation.
Examples:
- Calculate an average
- Convert a temperature
- Determine a score
- Process sensor information
Result Functions
Call Function
↓
Calculate Something
↓
Return Result
Functions Without Return Values
Not every function needs to return information.
Example:
blink_led()The goal is simply:
Run Instructions
No value needs to come back.
Two Families of Functions
| Function Type | Main Purpose |
|---|---|
| Action Function | Perform instructions |
| Result Function | Return information |
Conditional Returns
[INSERT BLOCK IMAGE HERE: If Return]
A return statement can be connected to a condition.
Example:
if condition:return value
This means:
Only return the value if the condition is true.
Visual Model
Ask Question
↓
Condition True?
↓
Return Value
Conditional returns combine:
- Functions
- Logic
- Control Structures
Understanding None
Students first encountered None in the Variables lesson.
Functions may also return:
return NoneThis means:
No normal value is being returned.
This creates an important connection between Variables, Logic, and Functions.
Variables
x = NoneFunctions
return NoneBoth use the same special value.
Functions and Variables
Functions frequently work with variables.
Example:
Variable
↓
Function Uses Value
↓
Process Information
↓
Program Continues
Variables provide information.
Functions organize how that information is used.
Together, they form the foundation of most programs.
Global Variables
[INSERT BLOCK IMAGE HERE: Global Variable]
Advanced programs sometimes use global variables.
Example:
global xThis allows a function to work with a variable that was created elsewhere in the program.
Beginner Rule
For now, focus on:
Create Function
↓
Call Function
↓
Use Parameters
↓
Use Return Values
Global variables become important later when programs grow more complex.
Functions and Sensors
Functions are especially useful when working with sensors.
Example:
P=
Sensor Reading
↓
Function Parameter
↓
Process Sensor Data
↓
Return Result
↓
Display / Control
As projects become larger, functions help avoid repeating the same sensor-processing steps.
Functions and Logic
Functions often contain decisions.
Example:
Receive Value
↓
Compare Value
↓
True or False
↓
Return Result
This combines:
Functions
Variables
Logic
Control Structures
Functions and the Serial Port
Functions can feel invisible because all their work happens inside the program.
The Serial Monitor helps students observe when functions execute.
Example:
Main Program
↓
Call Function
↓
"Function Started"
↓
Perform Action
↓
"Function Finished"
Watching the messages appear in the Serial Monitor helps students understand exactly when the function is running.
The Gear Icon
[INSERT IMAGE HERE: Function Gear Icon]
The Define Function block includes a gear icon that allows additional configuration.
For example, functions may be created:
def my_function():or
def my_function(x):Depending on how the function is configured.
As students gain experience, the gear icon becomes an important tool for creating more advanced function structures.
Key Distinction: Parameter vs Global Variable
Students often confuse these concepts.
Parameter
def my_function(x):The value enters the function when it is called.
Outside
↓
Inside Function
Global Variable
global xThe function accesses a variable that already exists elsewhere in the program.
Program Variable
↕
Function
Key Distinction: Define vs Call vs Return
These three ideas form the foundation of the Functions category.
Define
What should the function do?
Call
Run the function.
Return
Give a result back.
Core Mental Model
DEFINE
↓
CALL
↓
RETURN
Students should master Define and Call before focusing on Return values.
Key Vocabulary
| Term | Definition |
|---|---|
| Function | A named group of instructions |
| Define | Create a function |
| Call | Run a function |
| Parameter | Information that enters a function |
| Return | Information sent back from a function |
| Global Variable | A variable shared across different parts of a program |
| Procedure | The instructions that belong to a function |
| Conditional Return | Returning a value only when a condition is true |
Summary
In this lesson, you learned how functions allow programs to organize and reuse instructions.
You explored how to:
Define functions
Call functions
Reuse instructions
Pass information using parameters
Return information to a program
Connect functions with variables and logic
Build more organized programs
Functions are one of the most powerful tools in programming because they help transform a collection of individual instructions into organized, reusable, and easier-to-understand systems.
Key Takeaway
A function is a named set of instructions that can be used whenever it is needed.
By learning to define, call, and reuse functions, you can build programs that are easier to understand, easier to maintain, and much more powerful as your projects grow.
