return

Return from Function


Syntax: return value
Description: Return from function, and returns a single integer value
Parameters value Integer value returned.
Returns:
Related:  def


The return command is placed at the end of a function definition. It is used to mark the end of the function. It is also used to return a value back to the code.

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_if_pressed( pin ):
    sw = inp(P1_3)
    if sw == 0:
       out(pin,1)
       wait(300)
       out(pin,0)
       wait(300)
    return sw

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

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

This example defines the function flash_if_pressed() . Which will read the switch input on pin P1_3, 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.