SunFounder ESP32 Starter Kit
def my_function():
print("Your first function")
print(my_function())
>>> %Run -c $EDITOR_CONTENT
Your first function
None
Here, None is the return value, because the return statement is not used.
Arguments
Information can be passed to the function as arguments.
Specify arguments in parentheses after the function name. You can add as many arguments as you need, just separate
them with commas.
def welcome(name, msg):
"""This is a welcome function for
the person with the provided message"""
print("Hello", name + ', ' + msg)
welcome("Lily", "Welcome to China!")
>>> %Run -c $EDITOR_CONTENT
Hello Lily, Welcome to China!
Number of Arguments
By default, a function must be called with the correct number of arguments. Meaning that if your function expects 2
parameters, you have to call the function with 2 arguments, not more, and not less.
def welcome(name, msg):
"""This is a welcome function for
the person with the provided message"""
print("Hello", name + ', ' + msg)
welcome("Lily", "Welcome to China!")
Herethe function welcome() has 2 parameters.
Since we called this function with two arguments, the function runs smoothly without any errors.
If it is called with a different number of arguments, the interpreter will display an error message.
The following is the call to this function, which contains one and one no arguments and their respective error messages.
welcome("Lily")Only one argument
>>> %Run -c $EDITOR_CONTENT
Traceback (most recent call last):
(continues on next page)
294 Chapter 3. For MicroPython User