SunFounder Thales Kit for Raspberry Pi Pico, Release 1.0
Having a positional argument after a keyword argument will result in an error.
For example, if the function call as follows:
welcome(name
=
"Lily"
,
"Welcome to China!"
)
Will result in an error:
>>> %Run -c $EDITOR_CONTENT
Traceback (most recent call last):
File "<stdin>", line 5, in <module>
SyntaxError: non-keyword arg after keyword arg
Arbitrary Arguments
Sometimes, if you do not know the number of arguments that will be passed to the function in advance.
In the function definition, we can add an asterisk (*) before the parameter name.
def
welcome
(
*
names):
"""This function welcomes all the person
in the name tuple"""
#names is a tuple with arguments
for
name
in
names:
(
"Welcome to China!"
, name)
welcome(
"Lily"
,
"John"
,
"Wendy"
)
>>> %Run -c $EDITOR_CONTENT
Welcome to China! Lily
Welcome to China! John
Welcome to China! Wendy
Here, we have called the function with multiple arguments. These arguments are packed into a tuple before being
passed into the function.
Inside the function, we use a for loop to retrieve all the arguments.
Recursion
In Python, we know that a function can call other functions. It is even possible for the function to call itself. These
types of construct are termed as recursive functions.
This has the benefit of meaning that you can loop through data to reach a result.
The developer should be very careful with recursion as it can be quite easy to slip into writing a function which never
terminates, or one that uses excess amounts of memory or processor power. However, when written correctly recursion
can be a very efficient and mathematically-elegant approach to programming.
def
rec_func
(i):
if
(i
>
0
):
result
=
i
+
rec_func(i
-
1
)
(result)
else
:
result
=
0
return
result
(continues on next page)
3.5. MicroPython Basic Syntax
125
Summary of Contents for Thales Kit
Page 1: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 Jimmy SunFounder Jun 04 2021 ...
Page 2: ......
Page 4: ...ii ...
Page 6: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 2 CONTENTS ...
Page 140: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 136 Chapter 3 For MicroPython User ...
Page 164: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 160 Chapter 4 For Arduino User ...