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.
출력을 보려면 '실행'을 클릭하세요.