Chapter 4 — Functions (🦸 Your Own Commands)
🎯 Mission
Imagine you're playing a game.
Every time the player wins, you want to show:
🎉 You Win!
+100 XP
+50 Coins
Would you write those 3 lines 50 times throughout your program?
❌ No.
Instead, create your own command:
showReward()
That's exactly what a function is.
A function is your own custom command.
🟢 LEVEL 1 — Survival
Q1
What is the main purpose of a function?
A) Store data
B) Repeat code without copying it everywhere
C) Compare values
D) Create tables
✅ Answer: B
💡 Write once, use many times.
Q2
Which keyword creates a function?
A) func
B) method
C) function
D) create
✅ Answer: C
Q3
function hello()
print("Hello")
end
What did we just do?
A) Called the function
B) Created the function
C) Deleted the function
D) Printed Hello
✅ Answer: B
💡 The function exists now, but hasn't run yet.
Q4
function hello()
print("Hello")
end
hello()
Output?
A)
Hello
B) Nothing
C) Error
D)
hello
✅ Answer: A
💡 hello() runs the function.
Q5
Which line calls a function?
A)
function hello()
B)
hello()
C)
end
D)
print = hello
✅ Answer: B
Q6
Can you call the same function 100 times?
A) No
B) Yes
C) Only twice
D) Only inside loops
✅ Answer: B
💡 That's the whole point!
🟡 LEVEL 2 — Think Like a Programmer
Q7
function greet()
print("Welcome!")
end
greet()
greet()
Output?
A)
Welcome!
B)
Welcome!
Welcome!
C) Nothing
D) Error
✅ Answer: B
Q8
function jump()
print("Jump!")
end
How many times does "Jump!" print?
A) 0
B) 1
C) 2
D) Infinite
✅ Answer: A
💡 The function is created but never called.
Q9
function attack()
print("Hit!")
end
attack()
attack()
attack()
How many times is "Hit!" printed?
A) 1
B) 2
C) 3
D) 4
✅ Answer: C
Q10
function bye()
print("Bye")
end
print("Start")
bye()
What prints first?
A) Bye
B) Start
C) Error
D) Nothing
✅ Answer: B
Q11
function test()
print("Lua")
end
test()
print("Done")
What's printed last?
A) Lua
B) Done
C) test
D) Error
✅ Answer: B
Q12
Functions help reduce...
A) Bugs caused by copying code
B) Variables
C) Loops
D) Tables
✅ Answer: A
💡 Copy-paste code is one of the biggest sources of bugs.
🟠 LEVEL 3 — Real-Life Programming
Q13
Every time the player dies, you must:
Play sound
Show animation
Respawn player
Best approach?
A) Copy the code everywhere
B) Create playerDied()
C) Use a loop
D) Use if
✅ Answer: B
Q14
A website sends an email after every purchase.
Best design?
A) Write email code every time
B) Create sendEmail()
C) Use while
D) Store it in a variable
✅ Answer: B
Q15
You're making a calculator.
Which names make sense?
A)
abc()
B)
x()
C)
addNumbers()
D)
hello123()
✅ Answer: C
💡 Function names should describe what they do.
Q16
You want to play a button click sound everywhere.
Best approach?
A) Copy the sound code 50 times
B) Make playClickSound()
C) Use while
D) Use print
✅ Answer: B
Q17
Which is easier to read?
A)
a()
B)
x()
C)
calculateTotalPrice()
D)
zzz()
✅ Answer: C
Q18
You want to reuse the same logic in many places.
What's the best tool?
A) Variable
B) Function
C) Table
D) Boolean
✅ Answer: B
🔴 LEVEL 4 — Bug Hunter
Q19
function hello()
print("Hi")
What's wrong?
A) Missing end
B) Missing if
C) Missing for
D) Nothing
✅ Answer: A
Q20
function hello()
print("Hi")
end
Hello()
Problem?
A) Nothing
B) Lua ignores uppercase/lowercase
C) Function name doesn't match
D) Missing print
✅ Answer: C
💡 hello and Hello are different names.
Q21
function run()
print("Running")
end
Why doesn't anything print?
A) Missing run()
B) Missing end
C) Missing while
D) Missing for
✅ Answer: A
Q22
function score()
print("100")
end
score
Problem?
A) Nothing
B) Forgot ()
C) Missing end
D) Missing if
✅ Answer: B
💡 score refers to the function; score() runs it.
Q23
function hello()
print("Hi")
end
hello()
hello()
How many times does it run?
A) 1
B) 2
C) 3
D) Infinite
✅ Answer: B
Q24
What's better?
A)
function a()
B)
function doStuff()
C)
function updatePlayerHealth()
D)
function xyz()
✅ Answer: C
💡 Clear names make code easier to understand months later.
🟣 LEVEL 5 — Boss Battle 👑
Q25
A function is best described as...
A) Memory
B) A custom command
C) A loop
D) A table
✅ Answer: B
Q26
Creating a function automatically runs it.
A) True
B) False
✅ Answer: B
💡 You must call it with ().
Q27
What does this print?
function dance()
print("💃")
end
dance()
dance()
dance()
A)
💃
B)
💃💃
C)
💃
💃
💃
D) Nothing
✅ Answer: C
Q28
Which is the biggest advantage of functions?
A) Faster internet
B) Reuse code
C) Create tables
D) Replace variables
✅ Answer: B
Q29
Which is the most professional function name?
A)
a()
B)
test123()
C)
calculateDamage()
D)
x()
✅ Answer: C
Q30 — Final Boss
If you find yourself copying the same 10 lines of code into 8 different places, what should your first thought be?
A) Keep copying
B) Make the code longer
C) Put those 10 lines inside a function
D) Create more variables
✅ Answer: C
💡 One of the biggest habits of good programmers is this:
If you're copying code, stop and ask: "Should this be a function?"
🧠 Pareto Cheat Sheet
| Situation | Use |
|---|---|
| Create a function | function name() |
| End a function | end |
| Run a function | name() |
| Use functions for | Reusing code |
| Good function names | calculateScore(), playSound(), spawnEnemy() |
| Avoid | a(), x(), test() (unless it's truly a test) |
🏆 Boss Tip
From now on, think of functions like the built-in commands you've already used:
print()→ Built-in function.math.random()→ Built-in function.tonumber()→ Built-in function.
Now you're creating your own commands:
attackEnemy()saveGame()healPlayer()openDoor()
That's a major step—from just using a programming language to extending it with your own reusable building blocks.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.