Challenge - 5 Problems
Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of code using a function to reuse logic
What is the output of this PHP code that uses a function to calculate the square of a number?
PHP
<?php function square($num) { return $num * $num; } echo square(4); ?>
Attempts:
2 left
💡 Hint
Think about what the function square does with the input number.
✗ Incorrect
The function square multiplies the input number by itself. For input 4, it returns 16.
🧠 Conceptual
intermediate1:30remaining
Why use functions to avoid repetition?
Why is it better to use functions instead of repeating the same code multiple times?
Attempts:
2 left
💡 Hint
Think about what happens if you want to change repeated code in many places.
✗ Incorrect
Functions let you write code once and reuse it. This saves time and reduces mistakes when updating code.
🔧 Debug
advanced2:00remaining
Identify the error in function usage
What error will this PHP code produce when run?
PHP
<?php function greet() { echo "Hello!"; } greet(5); ?>
Attempts:
2 left
💡 Hint
Check how many parameters the function expects versus how many are given.
✗ Incorrect
The function greet expects no parameters, but 1 is given, causing a warning.
📝 Syntax
advanced1:30remaining
Find the syntax error in function definition
Which option shows the correct way to define a function in PHP?
Attempts:
2 left
💡 Hint
Remember the parentheses and braces syntax for functions.
✗ Incorrect
Functions require parentheses after the name and braces around the code block.
🚀 Application
expert2:30remaining
How functions improve program maintenance
Imagine you have a program that calculates the area of different shapes. Why is it better to use separate functions for each shape's area calculation?
Attempts:
2 left
💡 Hint
Think about how separate functions help when you want to fix or add new shapes.
✗ Incorrect
Using separate functions for each shape keeps code organized and makes it easier to fix or add new shapes without breaking others.