1

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:

  1. Explain the value of functions in a program
  2. Give examples of functions that you have already used
  3. Explain how functions are a form of abstraction
  4. Create and use a function in a program
  5. Explain the purpose of parameters in functions
  6. Create a function that takes one or more parameters and uses them to perform actions

Steps to Success:

  1. What is a Function
  2. Defining and Calling Functions
  3. Function Practice Exercises
  4. Parameters in Functions
  5. Parameter Practice Exercises
  6. Challenge 1: Dizzy Drone Rewrite
  7. 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:

  1. Make the LED blink five times WITHOUT using any patterns.
  2. Make the drone fly in a circle
  3. Make the drone play a 5 note sequence

Now, let’s practice calling these functions by doing the following IN ORDER:

  1. Takeoff
  2. Blink 15 times
  3. Play the 5 note sequence
  4. Fly in three circles
  5. Play the 5 note sequence
  6. Blink 10 times
  7. Land

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:

square(30, 1)

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:

  1. A function that takes in a number of tones and a note and plays that note the number of times specified.
  2. A function that takes in a red, green, and blue value and creates a custom pattern using that color.
  3. A function that takes in the options “jump, bounce, or rebound”
    1. For jump, the drone flies half a circle forward and away from you
    2. For bounce, the drone flies two circle halves, one after the other (like it is bouncing away)
    3. 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:

  1. Take in a user input for the number of spins
  2. Define 3 functions:
    1. A function for the light show
    2. A function to spin
    3. A function that calls the spin function, makes the drone increase altitude, and calls the light show
  3. 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:

  1. Have defined functions for making the dot and dash in Morse code.
  2. 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.