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 a function with default parameter
What is the output of this PHP code?
PHP
<?php function greet($name = "Guest") { return "Hello, $name!"; } echo greet(); ?>
Attempts:
2 left
💡 Hint
Check the default value of the parameter in the function.
✗ Incorrect
The function greet has a default parameter 'Guest'. When called without arguments, it returns 'Hello, Guest!'.
❓ Predict Output
intermediate2:00remaining
Return value from a function call
What will be the output of this PHP code?
PHP
<?php function add($a, $b) { return $a + $b; } $result = add(5, 3); echo $result; ?>
Attempts:
2 left
💡 Hint
The function returns the sum of two numbers.
✗ Incorrect
The function add returns the sum of 5 and 3, which is 8.
🔧 Debug
advanced2:00remaining
Identify the error in function call
What error will this PHP code produce?
PHP
<?php function multiply($x, $y) { return $x * $y; } echo multiply(4); ?>
Attempts:
2 left
💡 Hint
Check how many arguments the function expects and how many are given.
✗ Incorrect
The function multiply expects 2 arguments but only 1 is given, causing a warning about missing argument.
❓ Predict Output
advanced2:00remaining
Output of recursive function
What is the output of this PHP code?
PHP
<?php function factorial($n) { if ($n <= 1) { return 1; } else { return $n * factorial($n - 1); } } echo factorial(4); ?>
Attempts:
2 left
💡 Hint
Factorial of 4 is 4*3*2*1.
✗ Incorrect
The recursive function calculates factorial correctly. 4*3*2*1 = 24.
🧠 Conceptual
expert2:00remaining
Function variable scope behavior
What will be the output of this PHP code?
PHP
<?php $x = 10; function test() { $x = 5; echo $x . ' '; } test(); echo $x; ?>
Attempts:
2 left
💡 Hint
Variables inside functions have their own scope unless declared global.
✗ Incorrect
Inside the function, $x is 5 (local). Outside, $x remains 10 (global). So output is '5 10'.