Functions#

Introduction#

Functions are a fundamental component of Python programming, packaging code snippets into reusable, efficient sections of code designed to perform a specific task. Functions can be called multiple times within a program, making code organization easier, and helping to avoid repeated code. Functions also make code easier to debug.

Important: Defining a function alone doesn’t make it run. Use start_thread to start running a function right when your project begins, instead of waiting for an event or callback.

  • def defines a function.

  • return sends the function’s output back to the main program.

Usage:

def function_name(parameters):
    # Code to execute when the function is called
    return result  # Optional, used to return a value

Parameters

Description

function_name

A name you give to your function.

parameters

Optional. Variables that accept input values when the function is called, allowing data to be passed into the function.

result

Optional. Let the function send a result back to the caller. If a function does not include a return statement, it will return None by default.

Note: A function must always be defined before it is called.

Defining and Calling Functions#

Functions with No Parameters#

If a function does not require input, you can define it without parameters.

def greeting():
    console.print("Hello!")

# Call the greeting function
greeting()

Functions with Parameters#

You can also add parameters to functions, which let you pass in information the function needs to work.

def move_square(moves):
    for index in range(moves):
        drivetrain.drive_for(FORWARD, 150)
        drivetrain.turn_for(RIGHT, 90)

# Call the move_square function
move_square(4)

Functions with Default Arguments#

A default argument is a parameter that assumes a default value if a value is not provided in the function call for that argument.

def move_square(moves=4):
    for index in range(moves):
        drivetrain.drive_for(FORWARD, 150)
        drivetrain.turn_for(RIGHT, 90)

# Call the move_square function
move_square()

Return Values from Functions#

Functions can send data back to the caller using the return keyword. This allows you to capture and use the output in your program.

def times_two(number):
    return number * 2

# Display the return value
console.print(times_two(2))

start_thread#

start_thread calls a function so it begins running right away at the start of the project.

If more than one start_thread is used, the functions will happen at the same time.

Usage:
start_thread(function)

Parameter

Description

function

The name of a previously defined function.

def move_square():
    # Move in a square path
    while True:
        drivetrain.drive_for(FORWARD, 150)
        drivetrain.turn_for(RIGHT, 90)

def lights():
    # Blink Optical Sensor lights
    while True:
        optical_sensor.set_light(LEDStateType.ON)
        wait(0.5, SECONDS)
        optical_sensor.set_light(LEDStateType.OFF)
        wait(0.5, SECONDS)

# Start threads — Do not delete
start_thread(move_square)
start_thread(lights)