A function is a named set of instructions you can use again and again. Instead of writing the steps for “draw a square” four times, you write them once, name them, and call the name whenever you need them.
Apa yang akan dipelajari anak anda
- Know what a function is and why it is useful
- Predict what happens when a function is called several times
- Use functions with inputs (parameters), like size or name
- Work out what a simple function gives back
Write once, use many times
In Scratch, “Make a Block” creates a function. Fix a bug inside it and it’s fixed everywhere the block is used. Functions can take inputs: drawSquare(5) and drawSquare(10) use the same steps to draw different sizes.
Contoh soalan
Berikut ialah beberapa soalan seperti dalam misi pertama. Bolehkah anda menjawabnya?
1. Which is the best name for a function that makes a character wave?
👋- wave
- banana
- x
- thing2
Tunjuk jawapan
Jawapan: wave. Good names say what the function does.
2. Your “brush teeth” routine has the same steps every time. In coding, we could make it a…
🪥- function
- sound
- bug
- score
Tunjuk jawapan
Jawapan: function. Repeated routines make great functions.
3. True or false? A function can be used (called) many times in a program.
Tunjuk jawapan
Jawapan: Betul. That’s what makes functions so useful.
Tip untuk orang dewasa
- Invent a family “function”, like “bedtime routine”, and list its steps.
- Play a function machine game: “double” — you say 4, they say 8.