Lesson 5: Functions
עמוד זה דורש דפדפן עדכני (תומך ב-SharedArrayBuffer). אנא עדכן את Chrome, Edge, Firefox או Safari לגרסה העדכנית ביותר.
English
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) |
English
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.
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
Finish the function square(size), then call it twice in main() to draw a square of 300 mm and a square of 600 mm.
לחץ על הרץ כדי לראות את התוצא כאן.
רמת קושי 3/5 · 200 מטבעות + בונוס קטן בהשלמה הראשונה היום
Write a function hexagon() that draws a shape with 6 sides of 400 mm, and call it. Turn LEFT 60 degrees at each corner.
לחץ על הרץ כדי לראות את התוצא כאן.