Challenge - 5 Problems
Function Calling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested function calls
What is the output of this C++ code when the
main function runs?C++
#include <iostream> int add(int a, int b) { return a + b; } int multiply(int x, int y) { return x * y; } int main() { int result = multiply(add(2, 3), 4); std::cout << result << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Remember to evaluate the inner function call first before the outer one.
✗ Incorrect
The add function returns 5 (2 + 3). Then multiply(5, 4) returns 20.
❓ Predict Output
intermediate2:00remaining
Function call with default arguments
What will be printed when this program runs?
C++
#include <iostream> #include <string> void greet(std::string name = "Guest") { std::cout << "Hello, " << name << "!" << std::endl; } int main() { greet(); greet("Alice"); return 0; }
Attempts:
2 left
💡 Hint
Check what happens when you call greet() without arguments.
✗ Incorrect
The first call uses the default argument "Guest". The second call uses "Alice".
🔧 Debug
advanced2:00remaining
Identify the error in function call
What error will this code produce when compiled?
C++
#include <iostream> void printSum(int a, int b) { std::cout << a + b << std::endl; } int main() { printSum(5); return 0; }
Attempts:
2 left
💡 Hint
Check how many arguments the function expects versus how many are given.
✗ Incorrect
printSum requires two int arguments, but only one is provided, causing a compile error.
❓ Predict Output
advanced2:00remaining
Function call with reference parameter
What is the output of this program?
C++
#include <iostream> void increment(int &num) { num += 1; } int main() { int x = 10; increment(x); std::cout << x << std::endl; return 0; }
Attempts:
2 left
💡 Hint
The function changes the original variable by reference.
✗ Incorrect
The increment function adds 1 to the original variable x by reference, so x becomes 11.
🧠 Conceptual
expert3:00remaining
Understanding function call stack behavior
Consider this code snippet. What is the value of
result after main finishes?C++
#include <iostream> int factorial(int n) { if (n <= 1) return 1; return n * factorial(n - 1); } int main() { int result = factorial(4); return 0; }
Attempts:
2 left
💡 Hint
Recall how factorial is calculated recursively.
✗ Incorrect
factorial(4) = 4 * 3 * 2 * 1 = 24.