def fname ( parameter1, parameter2, … )

Function Definition


Syntax: def fname( par1, par2, …  ):
Description: Definition of a function. A function with the name fname is defined. The lines that follow are the function block definition. The function may have parameters, which can be used to pass data into the function. One data value can be returned from the function using the return command. The function can be run by using the command fname( par1, par2, … ).
Name fname The name of the function. The name can contain one or more letters, numbers or _ characters. However the first character cannot be a number.
Parameters par1, par2, … Optional parameters pass data into the function. There may be zero, one or more parameters. When the function is used it must have the same number of parameters as the function definition. Parameters may be any variable name. The value of the parameter is only defined for the function (ie the parameter is a local variable). Parameter names can contain one or more letters, numbers or _ characters. However the first character cannot be a number.
Returns: One data value may be returned from the function using the return command.
Related: global, return


A function is a self contained piece of code that can be used multiple times. The purpose of a function is to simplify the your code. Operations that need to be run more than once can be placed into a function and the function can be run instead.

Example:

def flash_green_led():
    out(P1_6,1)
    wait(500)
    out(P1_6,0)
    wait(500)

pindir(P1_6,OUT)
flash_green_led()
flash_green_led()

This example shows the use of a function. The function flash_green_led() is defined, it contains four lines that flash the green led once. The def function line has brackets at the end, but there are no parameters. The line ends with a : character to indicate a block of code will follow. The code that follows is indented (with 4 spaces) to indicate that these lines are part of the flash_green_led() function definition block.   After the function definition block the main program is defined.  The function is used (or called) by using the command flash_green_led(). The  flash_green_led() is run twice and the the led will be flashed twice.

The def function definition must be located after the define_micro() command and after any micro() commands, but must occur before the main program code.

Example:

def flash_led(pin, time):
    out(pin,1)
    wait(time)
    out(pin,0)
    wait(time)

pindir(P1_6)
dirout(P1_0)
flash_led(GREENLED,300)
flash_led(REDLED,600)

In this example function flash_led() is defined, and two parameters are passed into the function, pin, and time. The function flash_led() is run twice. Using parameters allows the function to be run differently each time. The first time it is run the GREENLED is used with a time of 300 milliseconds, and the second flash_led() uses the REDLED with a time of 600 miliseconds. Using parameters allows functions to be run differently each time, this makes functions very useful and powerful.

Example:

def flash_if_pressed( pin ):
    sw = inp(SWITCH)
    if sw == 0:
       out(pin,1)
       wait(300)
       out(pin,0)
       wait(300)
    return sw

pindir(P1_3)
pindir(P1_0)

while 1:
     val = flash_if_pressed(REDLED)
     print( 'switch value is: ', val )

This example defines the function flash_if_pressed() . Which will read the switch input on pin SWITCH, and if pressed will flash the led defined with parameter pin.  It also uses the return command to return the value of the switch with variable sw.

The main part of the program uses a while 1: infinite loop which runs the flash_if_pressed() function  passing in the REDLED as a paramter which will cause the REDLED to flash if the switch is pressed.  Regardless of whether the switch is pressed the flash_if_pressed() function returns the switch value into variable val. Then the print_value() command prints out  val onto the mpyEditor uart console.