By the end of this lesson, you will:
:information_source: Remember! A function is a group of predefined codes that will only run when it's called. In this lesson, we're making our functions even more powerful!
Have you ever used a vending machine? You put money in (input), and it gives you a snack back (output). Functions can work the same way using return statements!
Imagine a function like a factory in coding:
This is called a return statement, and we can create one with the return block.
Step One: Find the return block in your EduBlocks toolbox.
:bulb: Pro Tip!
The return block must always be inside a function - it can't work on its own! Think of it as the factory's delivery door - it needs to be part of the factory building.
Step 2: When using functions with return values, replace the regular function_name block with the special function value block.
When your function returns a value, you can:
Input | Output |
---|---|
![]() |
![]() |
:bulb: Troubleshooting!
If your function isn't returning anything:
- Check that the return block is inside your function
- Make sure you're using the function value block (not the regular function block)
- Verify that your return block has a value attached to it
Sometimes you need to change a variable that was created outside of your function. This is where global variables come in handy!
When you try to change a variable from outside a function, Python gets confused:
Input | Output |
---|---|
![]() |
![]() |
To fix this, you need to tell Python that the variable is global (available everywhere in your program).
Step One: Find the global block in the Statements category.
Step 2: Place the global block at the beginning of your function.
Step 3: Attach your variable name to the global block.
Input | Output |
---|---|
![]() |
![]() |
:information_source: Remember! Think of global variables like announcements on a school PA system - everyone in the school (your program) can hear them! Local variables are like whispers in a classroom - only people in that room (function) can hear them.
Create a function that:
Create a game score system:
score
starting at 0add_points
that adds 10 to the global scoredouble_score
that doubles the global scoreCreate three functions where:
:bulb: Connecting to Real Python! :emoji:
In text-based Python, return statements look like this:
python
def add_numbers(a, b): return a + b result = add_numbers(5, 3) print(result) # Outputs: 8
And global variables work like this:
python
score = 0 def add_points(): global score score = score + 10
Fantastic work learning about return statements and global variables! You've just unlocked two powerful features that make your functions much more flexible and useful.
With return statements, your functions can now give back values - just like a vending machine gives you snacks! And with global variables, you can share information across your entire program.
Keep experimenting with these new blocks, and remember: every expert coder started exactly where you are now. You're building amazing skills one block at a time! :tada: