Night 12: Tricks with a twist
คุณเรียนจบคืนนี้แล้วด้วย Python หรือรหัสเทียม และนับรวมในทั้งสองภาษา คุณสามารถฝึกอีกครั้งได้ตามต้องการ
Tonight: tricks with a twist
- Last night your pet learned tricks. But a trick did the same thing every time.
- Tonight you give a trick some information, so it can change.
- You will learn one thing: parameters.
Words for tonight
| Word | Meaning |
|---|---|
| parameter | a name in the brackets of PROCEDURE; it gets a value |
| argument | the value you give in the call |
| pass | give a value to a procedure |
A procedure with a parameter
- Write a name and its type in the brackets. This is the parameter.
- When you call the procedure, give a value. This is the argument.
PROCEDURE Hello(Name : STRING)
OUTPUT "Hello, ", Name
ENDPROCEDURE
CALL Hello("Kit")
CALL Hello("Ana")
- First call:
Nameis"Kit". Second call:Nameis"Ana".

More than one parameter
- Use a comma between parameters. The values go in in order.
PROCEDURE Feed(Food : STRING, Amount : INTEGER)
OUTPUT "Kit eats ", Amount, " ", Food
ENDPROCEDURE
CALL Feed("apples", 3)
CALL Feed("fish", 1)
| Call | Food |
Amount |
|---|---|---|
CALL Feed("apples", 3) |
"apples" |
3 |
CALL Feed("fish", 1) |
"fish" |
1 |
Build a line of stars
- Join text with
&. Add one star each time round a loop.
PROCEDURE StarRow(N : INTEGER)
DECLARE Row : STRING
DECLARE Count : INTEGER
Row ← ""
FOR Count ← 1 TO N
Row ← Row & "*"
NEXT Count
OUTPUT Row
ENDPROCEDURE
CALL StarRow(2)
CALL StarRow(5)
Names in the task
- The checker calls your procedure with its own values, like
CALL Hello("Bo"). - So the procedure must work for any value, not only the one in your code.
- Now give your pet's tricks a twist!
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Write Hello(Name : STRING). It outputs Hello, and the name. CALL Hello("Kit") outputs Hello, Kit. Call it for Kit and for Ana.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Write Jump(Times : INTEGER). It outputs Jump! Times times, one per line. Call Jump(3).
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Write Feed(Food : STRING, Amount : INTEGER) with สอง parameters. CALL Feed("apples", 3) outputs Kit eats 3 apples.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Write StarRow(N : INTEGER). It outputs one line of N stars, like *** for StarRow(3). Then use a loop to call it for 1, 2, 3 and 4. You get a triangle.
คลิก Run เพื่อดูผลลัพธ์ที่นี่