Lesson 10 – Functions with Finch

Python Concepts

Functions

Teacher Materials

Get Access

Throughout these lessons, you have used a wide variety of Finch methods. Methods are functions that are associated with an object, in this case the Finch. You can also write your own functions that use the Finch!

Defining and Calling a Function

To define a function, use the def keyword, followed by whatever you want to name your function. The name should not contain any spaces or punctuation. The name is followed by a pair of parentheses and a colon. All indented statements after the colon are the contents of the function. For example, this code defines a function named drawSquare() that moves the Finch in a square. 

# This function makes the Finch draw a square 
def drawSquare(): 
for i in range(4): 
bird.setMove('F',15,50) 
bird.setTurn('R',90,50) 
drawSquare() # Call the function

After you define a function, you can use the name to call that function. For example, the code above calls the function drawSquare() after defining it. Functions enable you to easily reuse code. Functions are also a tool for abstraction. Abstraction is the idea that you can make programs easier to use and understand by placing self-contained groups of actions within functions. A programmer can then use the functions without understanding the details of the code within them. For example, someone could use the drawSquare() function without needing to know what is inside it. Good documentation is essential to ensure that a function can be used correctly by another programmer, so be sure to comment your function!

Exercise 1

Define the drawSquare() function. Then use a for loop to draw 6 squares. Turn left 60° between each square. What other patterns can you draw with the drawSquare() function?

Exercise 2

Write a drawCircle() function, and then use it to create interesting patterns. 

Exercise 3

Write a function called lineTrack() that includes your favorite line tracking algorithm from the last lesson. Now, any time you need to track a line, you will be able to reuse this code! Test your function using the code shown here.

# Track the line until you find an obstacle 
while (bird.getDistance() > 20): 
lineTrack() 
bird.stop()

Exercise 4

Create a “wall following” function. To follow a wall, your function should include these three steps:

  • Move forward in a curved path (curving to the left or right) until an obstacle is detected.
  • Move straight back a short distance.
  • Rotate in the opposite direction (45° to 90°). 

To test your function, write a program that calls it repeatedly. You will need to adjust the distances and angles in your function to make your robot perform well. Your program should be able to handle corners as well as straight sections of wall. Test your program on your classroom wall, or construct a small maze out of cardboard! 

Functions with Parameters

You can also create functions that take parameters. For example, this is an alternate function to draw a square. It takes a parameter named size that  is named inside the parentheses that follow the function name. This parameter creates a variable inside the function. When you call the function, you provide a value for the parameter (5 in the example below), and the function is executed with the parameter variable equal to that value.

# This function makes the Finch draw a square. It requires 
# an integer parameter that is the side length of the square 
# in centimeters. 
def drawSquare2(size): 
for i in range(4): 
bird.setMove('F',size,50) 
bird.setTurn('R',90,50) 
drawSquare2(10) # Call the function

Exercise 5

Define the drawSquare2() function. Then use a for loop to draw 5 squares with random sizes between 5 and 20 cm. What other patterns can you draw with the drawSquare2() function?

Exercise 6

Write a function named blinkAll() that takes three parameters named red, green, and blue. This function should blink all of the Finch LEDs (beak and tail) in the color given by the parameters. The lights should blink on and off just once when you call the function. Test your function by using it to blink the lights until button A is pressed.

def blinkAll(red, green, blue): 
# Add your code here

Functions that Return Values

Finally, your functions can return values as well. For example, this function returns the mean of the values of the Finch light sensors. The code that calls this function can then use the returned value. In this case, it prints the returned value to the screen.

# This function returns the mean of the two Finch light sensors. 
def lightMean(): 
return 0.5*(bird.getLight('L') + bird.getLight('R')) 
print(lightMean())

Exercise 7

Write a function named isLevel() that returns a Boolean value. The function should return True when the Finch is in a level position and False otherwise. Use this function and your blinkAll() function to blink the Finch lights until the Finch is moved out of the level position.