1

3.4: Return Functions

Goals:

By the end of this lesson, I will be able to:

  1. Explain the difference between return and void functions
  2. Describe when return functions should be used in a program
  3. Describe when void functions should be used in a program
  4. Create a return function and use its output to influence the program

Steps to Success:

  1. Return Functions
  2. Final Code
  3. Return Functions with User Input
  4. Final Code
  5. Challenge: Conversion Calculator

A return function is slightly different from the functions you just saw. A return function returns a value or set of values. Let’s see how we can use return functions to create an inches to centimeter conversion calculator:

Now we know how to create a very basic calculator! In this activity, your CoDrone Mini will simulate a calculator. After adding two numbers in the return function together, it will throttle up and down for the value of the answer.

After importing the CoDrone Mini and time libraries, creating the drone object, and pairing, define a function called  add  with x and y as parameters. Underneath, have the function return  x+y .

import CoDrone_mini

import time

drone = CoDrone_mini.CoDrone()

drone.pair()


def add(x,y):

    return x+y

Create a variable called  answer that will store the return function. Next, include a print command that will display “The answer is” along with the answer. 

answer = add(2, 3)

print("The answer is", answer)

Program your drone to take off, and then create a for loop with answer as its parameter. Within the loop, have the drone throttle up for half a second at 30% power, and then down for the same amount of time and power. Finally, have the drone land.

drone.takeoff()

for i in range(answer):

    drone.set_throttle(30)

    drone.move(0.5)

    drone.set_throttle(-30)

    drone.move(0.5)
 
drone.land()
drone.close()

Your program should look like this:

import CoDrone_mini

import time

drone = CoDrone_mini.CoDrone()

drone.pair()


def add(x,y):

    return x+y


answer = add(2, 3)

print("The answer is", answer)

drone.takeoff()

for i in range(answer):

    drone.set_throttle(30)

    drone.move(0.5)

    drone.set_throttle(-30)

    drone.move(0.5)

drone.land()
drone.close()

Try this out with multiple numbers! What happens when your drone gets to bigger numbers?In this step, you’re going to take the calculator simulation even further by having a user input the numbers to add together!

Keep the first portion of your program the same:

import CoDrone_mini

import time

drone = CoDrone_mini.CoDrone()

drone.pair()


def add(x,y):

  return x+y

Set up a while loop so that while true, the program will run. Inside the loop, create a variable called  num1 and set its value to  input() . In the input parameters, write “Input first number (or q to quit): “. Next, write an if statement so that if “q” is input, the program will break out of the loop. Create another variable called num2 and set its value to input() as well. Write “Input second number: ” in the parameters.

while True:

    num1 = input("Input first number (or q to quit): ")

    if num1 == "q":

        break

    num2 = input("Input second number: ")

While still in the while loop, create a variable called  answer that will use the  add(x,y) function to return the sum of num1 and num2. Next, include a print command that will print the answer.

answer = add(int(num1), int(num2))

    print("The answer is ", answer)

The rest of your program will be the same!

drone.takeoff()

    for i in range(answer):

        drone.set_throttle(30)

        drone.move(0.5)

        drone.set_throttle(-30)

        drone.move(0.5)

    drone.land()
drone.close()

Your final program should look like this:

import CoDrone_mini

import time

drone = CoDrone_mini.CoDrone()

drone.pair()


def add(x,y):

    return x+y


while True:

    num1 = input("Input first number (or q to quit): ")

    if num1 == "q":

        break

    num2 = input("Input second number: ")

 
    answer = add(int(num1), int(num2))

    print("The answer is ", answer)

    drone.takeoff()

    for i in range(answer):

        drone.set_throttle(30)

        drone.move(0.5)

        drone.set_throttle(-30)

        drone.move(0.5)

    drone.land()
drone.close()

Many countries and systems use different ways of measuring quantities such as distance, mass, force, etc…  For this exercise, you will create a conversion calculator that takes in 4 different measurements and converts them to a different measurement. For example, we created a conversion function for inches to centimeters earlier in the lesson. You can choose any conversions you would like. To successfully complete this challenge, your program must:

  1. Have a list that contains the abbreviations a user will choose from for the measurement to be converted
  2. Prompt for 2 input values, the amount and measurement
  3. Have a function that takes in the two input values and returns the calculated value and measurement as a string
  4. Store the result as a variable
  5. Print the result in a sentence that tells the user what they input and what the result was (For example: “You entered 5 inches which is 12.7 centimeters)

A few notes:

  • You can add strings together by using the + sign
    • print(“Hello” + “World”) will print “Hello World”
  • You can do the same for variables and strings:
    • name = John
    • print(“Hello” + name) will print “Hello John”

To print text and variable