Functions#

Introduction#

Functions are a fundamental component of Python programming, packaging code snippets in to 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.

Below is a list of all available Function resources:

  • def – Defines a function.

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

  • vr_threads – Execute functions at the start of a project.

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.

# Define a function to display a message
def greeting():
    brain.print("Hello!")

def main():
    # Call the function to display the message
    greeting()

# VR threads — Do not delete
vr_thread(main)

Functions with Parameters#

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

# Define a function with a parameter
def named_greeting(name):
    brain.print("Hello, " + name + "!")

def main():
    named_greeting("Stranger")

# VR threads — Do not delete
vr_thread(main)

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.

# Define a function with a parameter and a default argument
def named_greeting(name = "Stranger"):
    brain.print("Hello, " + name + "!")

def main():
    # Use the default argument
    named_greeting()
    brain.new_line()
    # Change the parameter to a different name
    named_greeting("VR")

# VR threads — Do not delete
vr_thread(main)

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.

# Define a function that multiplies numbers by 2
def times_two(number):
    return number * 2

def main():
    # Display the return value
    brain.print(times_two(2))

# VR threads — Do not delete
vr_thread(main)

vr_thread#

The VR Thread function must be used to call functions at the global level of the script.

You can take any function you have made previously, and pass it to vr_thread() at the global level. Calling at least one function inside of this is required to run a program. You do not need to do all function calls with vr_thread(), just ones that are at the global level.

If more than one vr_thread() call is made, the functions will execute in parallel.

The functions will, in general, execute in the order they are called. However, if a function waits, it will pass execution to the next function, and continue when the other function finishes, or when the other function also waits.

# Define the function "main".
def main():
    drivetrain.drive_for(FORWARD, 400, MM)

# Call VR thread for any previously defined functions.
vr_thread(main)