Lesson 1-11 · Python Functions — Knowledge ChecksAnswer key
Which keyword defines a function in Python?
Python uses def, followed by the function name, parentheses, and a colon.
Given def square(side_length):, what does calling square(45) make the robot do?
The argument 45 fills the side_length parameter, so each Straight drives 45 cm. One parameter, one argument.
A variable created inside a function (like angle above) can be used…
Variables defined in a function are local to it: they're created when the function runs and gone when it returns. Code outside can't see angle.
If a function's variables are local, how can square() use drivetrain, which was created in the main program?
Python checks for a local name first, then looks outward. Reading an outer name works, which is why one drivetrain made at the top serves every function. Assigning to it from inside is the part that doesn't behave the way you'd expect.
What's wrong with this? def multiply(x, y)
A def line, like a for line, must end with a colon before its indented body.
Given this definition, what does calling polygon(6) draw?
def polygon(sides, side_length=30):
Arguments fill parameters from the left, so the 6 goes to sides. No second argument was given, so side_length falls back to its default of 30.