Functions: Making Life Easier

A function is a chunk of code that does a certain job. This is useful if you have to do the same thing repeatedly, especially if there are some variants to it.
Hide Notes ▼
Examine the test5.py file, which lights up every other light (blue) for two seconds and then turns them all off:

            import board
            import neopixel
            import time

            nPix = 20
            pixels = neopixel.NeoPixel(board.D18, nPix)

            def turnOff(n):
            	for i in range(n):
            		pixels[i] = (0,0,0)

            for i in range(0, nPix, 2):
            	pixels[i] = (0, 0, 255)

            time.sleep(2)
            turnOff(nPix)
          

Lines 8-10: The function


              def turnOff(n):
              	for i in range(n):
              		pixels[i] = (0,0,0)
            
This is the function that turns off the lights. Looking at line 8 def turnOff(n):.
  • def: The keyword in python that indicates a function is coming
  • turnOff: This is the name of the function. You can name your function almost anything, as long as you follow the same rules for variable names and don't conflict with a built in function name (like len).
  • (n): This indicates that the function needs some sort of input that is going to be given the variable name n. It does not say what type of input (string, integer, etc.), nor does it have a default value (you can set a default by using (n=10)) for example). But it's mostly up to you to send the right type of information.

Lines 9 and 10 are the body of the function that just turn off the number of lights you told it to. Note that the body is indented, which is how python indicates what's inside the function.

  • The variable n is a local variable. It was defined in the function definition, and only exists within the function. If you were to try to access it somewhere else in the program, you'd get an error. But this does mean that you could have a different variable named n somewhere else in the program and there would be no conflict. This also means that you don't have to worry about using the same variable name in different functions.
  • The variable pixels is a global variable. It was defined in the main part of the program so the function can use it.

Line 16: Calling the function.


            turnOff(nPix)
            
This calls the function and passes it the number of pixels (nPix) which was set to 20 on line 5.
  • The value of nPix (20) is sent to the function, and inside the function that value is given the variable name n.
  • Notice the sequence of the program. The function is defined on line 8-10, before the loop that turns on the lights (lines 12-13) and the pause (line 15), but it is not executed until the function call on line 16.

Exercises

1) Create a function that lights up a given number of lights. Get it to:
  • light up 5 lights
  • wait 2 seconds
  • light up 10 lights
  • wait 2 seconds
  • light up 15 lights
  • wait 2 seconds
  • light up 3 lights
Show Notes ▼
Show Solution ▼
2) Create a function that you can tell to light up a given number of lights (same as question 1 above), but also set the color. Get it to:
  • light up 5 lights as green
  • wait 2 seconds
  • light up 10 lights as red
  • wait 2 seconds
  • light up 15 lights, as yellow
  • wait 2 seconds
  • light up 3 lights as blue
One option would be to adapt the function from the previous question and add the color as a second, input but give it a default value so you don't have to give the color unless you want to (see this Notes section).
Show Notes ▼
Show Solution ▼
3) Create a function that lights up the lights but you can specify:
  • the time between each light lighting up
  • the color of the lights