2.3: Void Functions
Please remove AA batteries while plugged into your computer.
We found an issue in early CoDrone EDU controllers where it may get into a state where it can’t boot up when connected to a computer. For now, you can avoid this issue by removing your batteries.
Learn more: robolink.com/edu-controller-issueGoals:
By the end of this lesson, I will be able to:
- Explain the value of functions in a program
- Give examples of functions that you have already used
- Explain how functions are a form of abstraction
- Create and use a function in a program
- Explain the purpose of parameters in functions
- Create a function that takes one or more parameters and uses them to perform actions
Steps to Success:
- What is a Function
- Defining and Calling Functions
- Function Practice Exercises
- Parameters in Functions
- Parameter Practice Exercises
- Challenge 1: Dizzy Drone Rewrite
- Challenge 2: Maze Runner
You are now going to write a function to fly CoDrone EDU in a square. To do this, you can copy the code below or try creating it on your own. Make sure you import CoDrone EDU, pair, and create the drone object (those steps aren’t in the code below).
def square(): drone.takeoff() for i in range(4): drone.set_yaw(0) drone.set_pitch(30) drone.move(1) drone.set_pitch(0) drone.set_yaw(20) drone.move(2) drone.land() square()
Now that you created your function, run the code and see what happened! Did it work? Were you able to fly in a square? This time, try putting your square function in a for loop and have your drone make multiple squares. This is the power of functions! You can use them over and over again as many times as you want.Now that we have learned how to define functions, let’s create some functions and then play around with using them in a program! To do these exercises, create a new file called function_ex_1.py
In the SAME FILE, create ALL of the following functions:
- Make the LED blink five times WITHOUT using any patterns.
- Make the drone fly in a circle
- Make the drone play a 5 note sequence
Now, let’s practice calling these functions by doing the following IN ORDER:
- Blink 15 times
- Play the 5 note sequence
- Fly in three circles
- Play the 5 note sequence
- Blink 10 times
Once you have successfully run your program, try to estimate how many lines of code you saved by defining the functions instead of repeating the code over and over. Did you save lines? If so, was it a small or large number of lines?After every function are parentheses, and this is where the parameters go. Parameters are variables the function needs to use, but not all functions will have parameters!
To practice using parameters in our function, let’s go back to our square function. We now want to make the speed and duration change so that we can get different sized squares using the same function! Follow along below to create your custom squares function:
def square(power, duration)
Then you can change some of the parameters in your program:
def square(power, duration): drone.takeoff() for i in range(4): drone.set_yaw(0) drone.set_pitch(power) drone.move(duration) drone.set_pitch(0) drone.set_yaw(20) drone.move(2) drone.land() square()
The power and time will be set when the function is called. For example, if you want the power to be set at 30 and the time for each command to be 1 second, then you would call your function in your main program like this:
Try running your program now. Did you notice anything different from when you had no parameters in your function?Let’s practice creating some functions with parameters! To do this, create a new file called functions_ex_2 in your 3_Level directory. Now, define the following functions in the same file:
- A function that takes in a number of tones and a note and plays that note the number of times specified.
- A function that takes in a red, green, and blue value and creates a custom pattern using that color.
- A function that takes in the options “jump, bounce, or rebound”
- For jump, the drone flies half a circle forward and away from you
- For bounce, the drone flies two circle halves, one after the other (like it is bouncing away)
- For rebound, the drone flies half a circle forward and away and then completes half a circle back to you
Now, have a partner or student provide you with choices for each function and call them to see what the drone does.Remember how we made our drone spin in circles a set number of times? This time we are going to make that program a little more complex. We want to make our drone spin a number of times specified by the user. After each rotation, we want the drone to display a light sequence, rise a little higher, and complete another rotation. The drone will continue this procedure until it has done all of its rotations. To successfully complete this challenge, your program should:
- Take in a user input for the number of spins
- Define 3 functions:
- A function for the light show
- A function to spin
- A function that calls the spin function, makes the drone increase altitude, and calls the light show
- Use a loop to call the 3rd function (the one that calls the others) the number of times the user inputs
You may have heard about Morse code, a language made up of combining dots and dashs to create letters and numbers. For this program, we want to create functions that will produce the dots and dashes that make up each letter using the CoDrone EDU’s buzzer. To complete this challenge, your program must do the following:
- Have defined functions for making the dot and dash in Morse code.
- Using those dot and dash functions, try creating the word ‘hello’.
For an advanced challenge, try creating functions for each individual letter and using them to spell out your name.In this lesson, you learned how to create void functions! You learned the structure of defining a function, the purpose of using paramters, and how to call your functions in a program.