Functions
Creating Reusable Parts of a Program
What it is
The Functions category contains blocks for organizing instructions into named sections of a program.
A function allows several instructions to be grouped under one name. The program can then call that function when those instructions are needed.
For example, instead of repeatedly creating the same sequence:
LED ON → Wait → LED OFF
the sequence can be placed inside a function:
blink_led
and the program can call:
blink_led()
whenever that action is required.
Think of a function as a named mini-program inside the main program. Define the instructions once, then call the function when those instructions are needed.
Functions Blocks
The confirmed Clu-Blocks Pro generation shows functions working with:
DEFINE → PARAMETERS → GLOBAL VARIABLES → CALL → RETURN
| Block | Block Image | What It Does | Confirmed Generation |
|---|---|---|---|
| Define Function | Creates a named function containing instructions. | def my_function(): | |
| Function with Parameter | Creates a function that receives a parameter. | def my_function4(x): | |
| Function with Return Structure | Creates the function structure associated with returning information. | Confirmed generated functions include structures with return. | |
| Call Function | Calls the selected function. | my_function() | |
| Conditional Return | Returns a value when a condition is satisfied. | if False: return None | |
| Return | Returns a supplied value from a function. | Confirmed example: return None. |
Python Functions and Return Blocks
| Block | Block Image | What It Does | What the Options Mean |
|---|---|---|---|
| Function Definition | Creates a function named my_function. It can access and modify the global variables x and y. | my_function: Function name. global x, y: Declares that x and y refer to global variables. pass: Placeholder that does nothing. | |
| Function with One Parameter | Creates a function that receives one value and can modify the global variable y. | my_function2: Function name. x: Input parameter. global y: Allows the function to modify the global variable y. pass: Placeholder instruction. | |
| Function with Two Parameters | Creates a function that receives two values to use inside its instructions. | my_function3: Function name. x, y: Input parameters. pass: Placeholder for instructions. | |
| Function Definition | Creates another function that can access and modify the global variables x and y. | my_function4: Function name. global x, y: Declares global variables. pass: Does nothing. | |
| Function with One Parameter | Creates a function that receives one input value and can modify the global variable y. | my_function5: Function name. x: Input parameter. global y: Allows modification of the global variable y. pass: Placeholder instruction. | |
| Function with Two Parameters | Creates a function that receives two input values for use in its instructions. | my_function6: Function name. x, y: Input parameters. pass: Placeholder for instructions. | |
| Function Definition | def my_function7():global x, ypass | Creates a function named my_function7. It can access and modify the global variables x and y. | my_function7: Function name. global x, y: Declares global variables. pass: Placeholder that does nothing. |
| Function Call | Executes the instructions inside the function named my_function. | my_function: Name of the function to execute. Parentheses indicate that the function is being called. | |
| Function Call | my_function7() | Executes the instructions inside my_function7. | my_function7: Name of the function being called. The function must have been defined before it is called. |
| Return None | Ends the current function and returns None, meaning that no result is being provided. | return: Ends the function and sends a value back. None: Represents the absence of a value. | |
| Return Variable | return x | Ends the current function and sends the value stored in x back to the place where the function was called. | return: Sends a result back. x: The variable whose value will be returned. |
Important Notes
defcreates a function, but the function does not execute until it is called.- Parameters such as
xandyreceive values when the function is called. globalallows a function to modify variables defined outside that function.passis a placeholder used when a function has no instructions yet.return Noneandreturn xmust be placed inside a function. They cannot be used as standalone instructions at the main program level.my_function()andmy_function7()are function calls; they execute the corresponding functions.- Defining a function with
defcreates it, but does not execute its instructions. It runs only when called. - Parameters (
x,y) receive values when the function is called. For example:my_function3(10, 20). - Global variables are variables created outside a function. The
globaldeclaration is needed when assigning a new value to them inside that function. passis a placeholder. It lets students create a function before adding instructions.return Noneis normally used inside a function, not as a standalone instruction in a Python program.
1. Defining a Function
[ INSERT BLOCK IMAGE HERE: Define Function ]
The basic function-definition block creates a named function.
The real Clu-Blocks Pro generation includes:
def my_function():global x
pass
The most important part for beginners is:
def my_function():This defines a function named:
my_function
The instructions belonging to that function appear underneath the definition.
Conceptually:
FUNCTION NAME
↓
INSTRUCTIONS
For students:
Defining a function explains what the function should do. It does not mean the function is being called at that moment.
2. Procedure
The visual Clu-Blocks Pro block contains a section labeled:
procedure
This is where students place the instructions that belong to the function.
Conceptually:
define my_function
↓
procedure
↓
Instruction 1
Instruction 2
Instruction 3
For example:
define blink_led
↓
Turn LED ON
↓
Delay
↓
Turn LED OFF
All three operations now belong to the function.
3. Defining Is Not Calling
This is the single most important distinction when first learning Functions.
DEFINE
Create the instructions:
def my_function():CALL
Use those instructions:
my_function()A simple memory rule is:
DEFINE = What should happen?
CALL = Do it now.
4. Calling a Function
[ INSERT BLOCK IMAGE HERE: Call Function ]
The actual Clu-Blocks Pro generation confirms:
my_function()When the program reaches this call, the instructions associated with:
Plain Text
my_function
are used.
Conceptually:
MAIN PROGRAM
↓
CALL my_function()
↓
FUNCTION INSTRUCTIONS
↓
RETURN TO PROGRAM FLOW
For beginners, the essential relationship is simply:
DEFINE ONCE → CALL WHEN NEEDED
5. Functions with Parameters
The real generation provides an important piece of information that was missing from our original documentation.
Clu-Blocks Pro can generate:
def my_function4(x):pass
This confirms that a function can receive a parameter.
In this example, the parameter is:
Plain Text
x
Conceptually:
VALUE
↓
CALL FUNCTION
↓
PARAMETER x
↓
FUNCTION USES x
6. What Is a Parameter?
A parameter allows information to enter a function.
Without a parameter:
def my_function():With the confirmed parameter:
def my_function4(x):A beginner-friendly way to think about this is:
A parameter is an input to the function.
The function can perform its instructions using the supplied information.
This gives us a very useful model:
FUNCTION INPUT → FUNCTION PROCESSING
Later, students can add:
FUNCTION INPUT → PROCESSING → RETURN RESULT
7. Function without a Parameter vs. Function with a Parameter
| Function | Confirmed Form | Main Idea |
|---|---|---|
| Without Parameter | def my_function(): | Perform the defined instructions. |
| With Parameter | def my_function4(x): | Receive x and perform the defined instructions. |
This distinction should be introduced gradually.
Students do not need parameters during their first function exercise.
8. Functions and Global Variables
The real generator also shows:
Python
x = Nonefollowed by function structures such as:
def my_function():
global x
pass
and:def my_function2():global x
pass
This establishes an important relationship with the Variables category.
Inside these generated functions, Clu-Blocks Pro includes:
global xThis indicates that the function is working with the x variable from the broader program scope.
9. Understanding global x
Outside the function
There is a variable:
x
Inside the generated function
Clu-Blocks Pro can include:
Python
global xConceptually:
PROGRAM VARIABLE x
↕
FUNCTION USES x
Advanced Concept:
globalbecomes important when a function needs to work with a variable from the broader program scope.
Students should understand ordinary Variables and basic Functions before learning global-variable scope in detail.
10. Function with a Conditional Return
def my_function3(x):
if False:
return NoneThis combines several concepts students have already learned:
FUNCTION
PARAMETER
LOGIC / CONTROL
RETURN
The structure can be understood as:
Receive x
↓
Check Condition
↓
IF condition is true
↓
RETURN a value
11. Conditional Return
[ INSERT BLOCK IMAGE HERE: if return ]
The corresponding structure generates:
Python
if False:
return NoneThe block therefore combines:
Condition
Determines whether the return occurs.
Return Value
Determines what value is returned.
Conceptually:
IF condition
↓
RETURN value
This is more advanced than simply calling a function, so it should be introduced later.
12. Return
[ INSERT BLOCK IMAGE HERE: Return ]
The real generation confirms:
Python
return NoneThe important concept is that return provides a value back from the function.
Conceptually:
FUNCTION
↓
PROCESS
↓
RETURN
↓
RESULT
For example, conceptually:
Function receives a sensor value
↓
Function processes value
↓
Function returns result
↓
Main program uses result
13. None as a Returned Value
The confirmed example is:
return NoneStudents have already encountered:
Nonein both Logic and Variables.
So the concepts now connect:
Logic
None represents the special no-normal-value object.
Variables
A variable may initially appear as:
x = NoneFunctions
A function may return:
return NoneThis creates useful continuity between the categories.
14. Function Inputs and Outputs
Once parameters and return are understood, students can use a very powerful mental model.
Input
Parameter
↓
Process
Function Instructions
↓
Output
Return Value
So:
INPUT → FUNCTION → OUTPUT
This is one of the most useful ways to explain Functions before introducing more formal programming terminology.
15. Functions Without Return Values
Not every function needs to return information.
A function may simply perform actions.
For example:
blink_led()could conceptually:
- Turn an LED ON.
- Wait.
- Turn the LED OFF.
The purpose is:
CALL → DO SOMETHING
There does not need to be a returned value.
16. Functions With Return Values
Other functions are useful because they provide information.
Conceptually:
CALL → CALCULATE SOMETHING → RETURN RESULT
This gives students two basic families.
| Function Type | Main Purpose |
|---|---|
| Action Function | Perform instructions. |
| Result Function | Provide a value through return. |
This distinction is easier for beginners than starting with formal terminology.
17. Using a Returned Value
A returned result can conceptually be used anywhere a compatible value is needed.
For example:
Function
↓
Returned Value
↓
Variable
or:
Function → Math
or:
Function → Logic
or:
Function → Serial Port
or:
Function → Display
This makes a function capable of participating in a larger program rather than existing only as a group of commands.
18. Why Use Functions?
Functions become valuable when the same operation would otherwise be repeated.
Without a function:
LED ON → Delay → LED OFF
later:
LED ON → Delay → LED OFF
later again:
LED ON → Delay → LED OFF
With a function:
DEFINE blink_led ONCE
then:
CALL blink_led
when needed.
The key educational idea is:
💡 Give a useful operation a name, then use that name when the operation is needed.
19. Functions Help Organize Larger Programs
Functions are not useful only because of repetition.
As projects become more complicated, meaningful function names can organize different program responsibilities.
Conceptually:
Main Program
↓
Read Sensor Function
↓
Process Information Function
↓
Control Output Function
However, for your course I would not introduce this decomposition too early.
Students should first learn:
DEFINE → CALL
before using multiple functions to organize a large program.
20. Functions and Variables
The actual code generation makes the relationship between these categories especially clear.
The supplied reference contains:
x = None
and:def my_function():
global x
passTherefore, students eventually need to understand:
VARIABLE
↓
FUNCTION USES VARIABLE
↓
FUNCTION MAY MODIFY / PROCESS INFORMATION
↓
PROGRAM CONTINUES
But again, this belongs after basic Variables and basic Functions.
21. Functions and Logic
A function can contain logical decisions.
For example, the confirmed generated structure includes:
Python
if False:
return None
Conceptually:FUNCTION
↓
ASK QUESTION
↓
TRUE?
↓
RETURN VALUE
This combines:
Functions + Logic + Control
22. Functions and Sensors
Functions can eventually organize repeated sensor-related operations.
Conceptually:
Sensor Value
↓
Function Parameter
↓
Process Sensor Information
↓
Return Result
↓
Serial / Display / Control
But for beginner sensor exercises, direct blocks should remain preferable until students understand why a function would actually help.
This is important for the educational progression of Clu-Blocks Pro.
23. Functions and Serial Port
Serial communication is extremely helpful when students begin learning Functions because function execution is otherwise invisible.
For example:
Main Program
↓
Call Function
↓
Function prints "Function started"
↓
PC
↓
Function performs action
↓
Function prints "Function finished"
↓
PC
Students can observe when the function actually executes.
A useful first progression is:
DEFINE → CALL → REPORT THROUGH SERIAL
24. The Gear Icon
[ INSERT IMAGE HERE: Functions Gear Icon ]
The function-definition blocks include a gear icon.
The actual generated MicroPython confirms that configured function structures can differ.
For example:
Without Parameter
def my_function():With Parameter
def my_function4(x):This means the function configuration can affect the structure generated by the function block.
For the manual, I would avoid claiming additional gear-menu options that are not explicitly confirmed by the supplied block interface or generated code.
25. Important Distinction: Parameter vs. Global Variable
These concepts can easily confuse beginners.
Parameter
def my_function4(x):The x appears as an input to the function definition.
Global
def my_function():
global xThe generated function refers to the broader-scope x.
So the beginner distinction is:
PARAMETER → Information enters through the function
GLOBAL → Function refers to a broader program variable
Do not teach both concepts simultaneously during the first Functions lesson.
26. Important Distinction: Define vs. Call vs. Return
These three words provide the central mental model for the entire category.
DEFINE
What does the function do?
↓
CALL
Run the function.
↓
RETURN
Give a result back.
In compact form:
DEFINE → CALL → RETURN
A function does not necessarily need a return value, so the first two concepts should always come first.
Quick Guide for Students
- Do I want to create a reusable group of instructions? Use: Define Function
- Do I want those instructions to run? Use: Call Function
- Does the function need information supplied to it? Use a parameter.
- Does the function need to provide a result? Use: return
- Should the return happen only under a condition? Use: if → return
- Does the function need to work with the broader program variable generated as
x? The generated structure may use:
global xThis is an advanced concept and should be introduced only when needed.
Quick Reference
| Concept | Confirmed Generation | Main Idea |
|---|---|---|
| Basic Function | def my_function(): | Define a function. |
| Function Parameter | def my_function4(x): | Define a function receiving x. |
| Global Variable | global x | Refer to the broader-scope x inside the generated function. |
| Call Function | my_function() | Execute the function. |
| Conditional Return | if condition: return value | Return when a condition is satisfied. |
| Return | return None | Return a supplied value. |
| Empty Procedure | pass | Generated placeholder when no operation occupies the body. |
