Skip to main content
Module 1 · Learning to Drive

Lesson 1-11 · Python Functions — Knowledge ChecksAnswer key

Correct answers are marked and the explanation follows each question.

  1. Which keyword defines a function in Python?

    1. A.function
    2. B.def✓
    3. C.define
    4. D.func

    Python uses def, followed by the function name, parentheses, and a colon.

  2. Given def square(side_length):, what does calling square(45) make the robot do?

    1. A.Draw a square with 45 cm sides✓
    2. B.Draw a square with 90 cm sides
    3. C.Turn 45 degrees
    4. D.Cause an error — you must pass two numbers

    The argument 45 fills the side_length parameter, so each Straight drives 45 cm. One parameter, one argument.

  3. A variable created inside a function (like angle above) can be used…

    1. A.Anywhere in the program
    2. B.Only inside that function — it is local✓
    3. C.Only after the program ends
    4. D.Only in other functions

    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.

  4. If a function's variables are local, how can square() use drivetrain, which was created in the main program?

    1. A.Because drivetrain is special to XRPLib
    2. B.When a name is not local, Python looks outside the function for it — functions can read names from the surrounding program✓
    3. C.It does not work; that program has a bug
    4. D.Because drivetrain is passed in automatically

    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.

  5. What's wrong with this? def multiply(x, y)

    1. A.multiply should be capitalized
    2. B.It is missing the colon: def multiply(x, y):✓
    3. C.x and y need quotes
    4. D.Nothing is wrong

    A def line, like a for line, must end with a colon before its indented body.

  6. Given this definition, what does calling polygon(6) draw?

    def polygon(sides, side_length=30):
    1. A.A hexagon with 30 cm sides✓
    2. B.A hexagon with 6 cm sides
    3. C.A 30-sided shape
    4. D.An error — polygon needs two arguments

    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.