Night 11: Teach your pet a trick
이미 Python 또는 의사 코드로 이날의 학습을 마쳤습니다. 두 언어 모두에서 완료한 것으로 인정됩니다. 원하면 다시 연습하세요.
이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
English
Tonight: teach your pet a trick
- Your pet can learn tricks! You teach a trick once, then use it again and again.
- You will learn one thing: procedures.
Words for tonight
| Word | Meaning |
|---|---|
| procedure | a named block of code; a trick with a name |
| define | say what something is; teach the trick |
| call | use the procedure; ask for the trick |
English
Define a procedure
PROCEDURE, then the name and brackets().ENDPROCEDUREends it.- The lines between them are the trick.
PROCEDURE Wave()
OUTPUT "*waves*"
OUTPUT "Hello!"
ENDPROCEDURE
OUTPUT "I know a trick now."
- Press Run. Only the last line runs! You taught the trick, but you did not ask for it.
English
Call the procedure
- To use the trick, write
CALLand its name:CALL Wave().
PROCEDURE Wave()
OUTPUT "*waves*"
ENDPROCEDURE
CALL Wave()
CALL Wave()
- Each call runs all the lines inside the procedure.
English
Why use procedures?
| Without a procedure | With a procedure |
|---|---|
| copy the same lines again and again | write them once |
| a change means changing every copy | change one place |
| long code that is hard to read | short names like Wave() |
English
Procedures can call procedures
- A big trick can be made from small tricks.
PROCEDURE Wave()
OUTPUT "*waves*"
ENDPROCEDURE
PROCEDURE Jump()
OUTPUT "*jumps*"
ENDPROCEDURE
PROCEDURE Party()
CALL Wave()
CALL Jump()
OUTPUT "Party time!"
ENDPROCEDURE
CALL Party()
- In 9618, a procedure does a job. On night 13 you meet a function, which gives back a value.
- Now teach your pet some tricks in the tasks!
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
Teach your pet its first trick. Write a procedure Wave() that outputs *waves*. Then call it two times with CALL.
출력을 보려면 '실행'을 클릭하세요.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
Write two more tricks: Sit() outputs *sits* 및 Spin() outputs *spins*. Then call Sit(), Spin() 및 Sit() again.
출력을 보려면 '실행'을 클릭하세요.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Write Show(). It calls Wave(), then Sit(), then outputs Ta-da!. Call Show() once.
출력을 보려면 '실행'을 클릭하세요.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Write Bedtime(). It uses a FOR loop to output Zzz three times. Call it once.
출력을 보려면 '실행'을 클릭하세요.