10 Functions

def

Functions are very useful, they allow a you to reuse a piece of code so you don’t have to rewrite it each time you want to do the same thing. This is not only great for lazy people, it is also more efficient,  and can even make your program more reliable.

In the previous example,  the code to flash the GREENLED is almost identical to the code to flash the REDLED.

Using a def statement we can make a function that can flash any of the LEDs. So we can rewrite the previous example like this:

##############################################
def flash_once(pin):  # define a function to flash the LED
    out(pin,1)
    wait(500)
    out(pin,0)
    wait(500)

##############################################
# This is where the program really starts
##############################################

pindir(P1_6, OUT)
pindir(P1_0, OUT)
pindir(P1_3, INPU) # setup the pin as an inputwith pullup resistor

while 1:
    sw = inp(P1_3)        # read the input pin, pressed == 0, not pressed == 1
    if sw == 1:
        flash_once(P1_6) # run the function flash_once() using P1_6 (GREENLED)
    else:
        flash_once(P1_0)   # run the same function using P1_0 (REDLED) 

############################################

The commands that flash the LED have now been put into a function definition. It uses the word def  followed by the function name flash_once which can be any name you want (it must start with a letter or a _ just like a variable name).  The lines that follow the def line are indented and form a block of code.  The line must also must end with a : character.

The def function definitions must be at the beginning of the file, and before the start of the main program. You can have multiple def statements, one for each function you want to define.

parameters

Values can be passed to the function, values passed into a function are more commonly called parameters.  The parameter can be used inside the function to perform some operation on it.  You can have more than one parameter if you want or no parameters. However all the parameters must be enclosed inside round brackets, and each parameter must be separated by a ‘,’ . There must be a : colon at the end of the line. Which tells the program that a block of code will follow.  As usual the block of code must be indented by placing spaces at the beginning of each line within the block.

We can use more parameters to do more fancy operations, like

######################################################
def flash( pinname, flash_count, wait_time ):  # define a function to flash the LED
    while flash_count > 0:
        out(pinname,1)
        wait(wait_time)
        out(pinname,0)
        wait(wait_time)
        flash_count = flash_count - 1

##############################################
# This is where the program really starts
##############################################

pindir(P1_6, OUT)
pindir(P1_0, OUT)
pindir(P1_3, INPU)

while 1:
    sw = inp(P1_3)            # read the input pin, pressed == 0, not pressed == 1
    if sw == 1:
        flash(P1_6, 5, 100)   # flash the GREENLED 5 times with 100ms on time.
    else:
        flash(P1_0, 3, 200)   # flash the REDLED 3 times with 200ms on time
######################################################

In this example we are passing in three parameters to the flash() function, the first parameter is is pinname and is used to select which LED to light just like the previous example. The second parameter is called flash_count and is used to set the number of flashes on the LED.  The third parameter is called wait_time, it is used to specify how long each flash should last. Go ahead and program this and give it a try.


return

The final special feature of the def function is that it can return a value.  If you use a return command at the end of your function it can return a value back to the main code.

############################
def mult(a,b):
    c = a * b
    return c
############################
x = 12
y = 13
z = mult(x,y)
print( ' the multiplication answer is',  z)

This example shows how to return a value from a function. Note that only one value can be returned from a function.