Lesson 5: Functions
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version.
Your own commands
- A function is a name for some lines of code.
def square(size):makes a new command calledsquare. sizeis a parameter: a value you give the function each time you call it.main()is a function too. You have used it from the start.
Words for this lesson
| Word | Meaning |
|---|---|
| function | a named group of commands |
| def | define (make) a function |
| parameter | a value the function gets, like size |
| call | run a function: square(300) |
Make a command, use it twice
from vexcode_vr import *
def square(size):
for i in range(4):
drivetrain.drive_for(FORWARD, size, MM)
drivetrain.turn_for(RIGHT, 90, DEGREES)
def main():
pen.move(DOWN)
square(200)
square(400)
vr_thread(main)
square(200)draws a small square,square(400)a big one. The code inside is written once.
Difficulty 2/5 · 140 coins + a small bonus on first completion today
Finish the function square(size), then call it twice in main() to draw a square of 300 mm and a square of 600 mm.
Click Run to see the output here.
Difficulty 3/5 · 200 coins + a small bonus on first completion today
Write a function hexagon() that draws a shape with 6 sides of 400 mm, and call it. Turn LEFT 60 degrees at each corner.
Click Run to see the output here.