By the end of this lesson, you'll be able to:
Definition: A function is a group of predefined codes that will only run when it's called.
Think of functions like a recipe! :emoji: When you want to make your favorite snack, you follow the same steps every time. Functions work the same way in programming!
To create a function, we have to start with the define block.
The define block is used to define the name of a group of codes.
:information_source: Remember! Functions are like creating your own custom blocks in EduBlocks! Once you create them, you can use them over and over again.
Functions are useful in grouping codes that need to be repeated. Imagine the steps to make a hot Milo drink:
Each time you want to make the drink, you have to repeat the steps.
If you're making 4 cups of hot Milo, you don't want to have the steps written repeatedly 4 times.
Using a function can simplify your code by grouping and just call it whenever you want to make hot Milo.
Input | Output |
---|---|
![]() |
![]() |
The call function blocks are selected with red rectangles.
As you can see, we already defined our function. So, just call the function whenever we want to make hot Milo.
:bulb: Pro Tip!
In real Python, functions help programmers avoid copying and pasting the same code. Instead of writing the same instructions 10 times, you write them once in a function and call it 10 times!
Let's create our first function step by step:
:bulb: Naming Tips!
Choose names that describe what your function does! Good names:
make_milo
(tells us it makes Milo)draw_square
(tells us it draws a square)say_hello
(tells us it says hello)Avoid confusing names like
function1
orxyz
:information_source: Remember! Creating a function is like writing a recipe - it doesn't make the food until someone follows the recipe (calls the function)!
Now let's learn how to use (or "call") the function we created:
:bulb: Watch Out!
The function name when calling MUST be exactly the same as when you defined it. If you defined
make_milo
, you can't callmakemilo
orMake_Milo
- it won't work!
Input | Output |
---|---|
![]() |
![]() |
Create a function called my_greeting
that prints three different messages, then call it twice to see what happens!
:information_source: Remember!
Let's use the make Milo function we've created above, and use parameters.
By using a variable value block, we can pass as parameters:
n
for number)Input | Output |
---|---|
![]() |
![]() |
The function_name block is used to call and input an argument.
An argument is a value passed into a function.
The function will recognise the input() value as an argument for the n variable.
:bulb: Naming Freedom!
Parameters can be any names you want! Common choices:
n
for numbersname
for textx
andy
for coordinatescolor
for colors Choose names that make sense for what they represent!
Input | Output |
---|---|
![]() |
![]() |
Input any number and it will multiply by 2.
Again, this works because the input() is an argument passed into the function as parameter n .
:bulb: Connection to Python!
In real Python code, this would look like:
python
def multiply_by_two(n): print(n * 2) multiply_by_two(5) # This prints 10
See how similar it is to your blocks? :emoji:
Sometimes your functions need more than one piece of information! Let's learn how to use multiple parameters:
:information_source: Remember! Think of parameters like ordering pizza:
order_pizza(large, pepperoni)
is different from order_pizza(pepperoni, large)
!The parameters are separated with commas.
Anything added to the brackets when calling the function is an argument.
The order of the parameters and arguments are important.
In the example above, the first argument (1st num) will become x , and the second argument (2nd num) will become y .
:bulb: Pro Tip!
You can have 2, 3, 4, or even more parameters! Just remember:
- More parameters = more flexible function
- But too many parameters can be confusing
- 2-3 parameters is usually perfect!
Input | Output |
---|---|
![]() |
![]() |
Create a function called draw_rectangle
that takes two parameters: width
and height
. Make it print a rectangle pattern using stars (*) based on the dimensions you give it!
Wow! You've learned how to:
Functions are one of the most powerful tools in programming. You're now able to create your own custom blocks that can do amazing things!
Practice creating different functions:
Keep experimenting and have fun with functions! Remember, every professional programmer uses functions every day - and now you know how to use them too! :tada: