0
0
C++programming~20 mins

Function calling in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function Calling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A10
B24
C20
D14
Attempts:
2 left
💡 Hint
Remember to evaluate the inner function call first before the outer one.
Predict Output
intermediate
2: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;
}
AHello, Alice!\nHello, Guest!
BHello, !\nHello, Alice!
CHello, Guest!\nHello, Guest!
DHello, Guest!\nHello, Alice!
Attempts:
2 left
💡 Hint
Check what happens when you call greet() without arguments.
🔧 Debug
advanced
2: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;
}
AOutput: 5
BCompilation error: too few arguments to function 'printSum'
CRuntime error: segmentation fault
DOutput: 0
Attempts:
2 left
💡 Hint
Check how many arguments the function expects versus how many are given.
Predict Output
advanced
2: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;
}
A11
B10
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
The function changes the original variable by reference.
🧠 Conceptual
expert
3: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;
}
A24
B10
C120
D1
Attempts:
2 left
💡 Hint
Recall how factorial is calculated recursively.