Lesson 5: Functions
Esta página precisa de um navegador recente (com suporte a SharedArrayBuffer). Por favor, atualize Chrome, Edge, Firefox ou Safari para a versão mais recente.
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.
Dificuldade 2/5 · Moedas 140 + pequeno bônus na primeira conclusão hoje
Finish the function square(size), then call it twice in main() to draw a square of 300 mm and a square of 600 mm.
Clique em Executar para ver a saída aqui.
Dificuldade 3/5 · Moedas 200 + pequeno bônus na primeira conclusão hoje
Write a function hexagon() that draws a shape with 6 sides of 400 mm, and call it. Turn LEFT 60 degrees at each corner.
Clique em Executar para ver a saída aqui.