When we define a basic function, we only specify the function name and code (commands). For example, if we were writing a Python program, we could write a function to print the results of flipping a coin:
def print_coin_flip(): # The next line checks whether a random value is 0 or 1 if random.randrange(0, 2) == 0: print('heads') else: print('tails')
We can call it as many times as we need to, using the name and empty parentheses:
print_coin_flip() print_coin_flip() print_coin_flip()
And the result would be something like this:
tails heads tails
Now we’re ready to try using functions.
For this exercise, we will need to define a function
A that holds logic moving Codey towards the goal.
The goal is in a straight diagonal line from Codey. If we move up and over repeatedly we will reach the goal. Define function
A by placing
move commands inside of it, then call it repeatedly to get Codey to the goal.
Throughout this course we will use Blockly commands to guide Codey towards their goal. Drag the commands into the right hand box and connect them together to make a program.
For this exercise, combine the movement commands together to have Codey move onto the flag.
Define Function A Move Up Move Right Call Function A Call Function A Call Function A Call Function A Call Function A