0
0
C++programming~20 mins

Function declaration and definition in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple function call
What is the output of this C++ program?
C++
#include <iostream>

int add(int a, int b);

int main() {
    std::cout << add(3, 4) << std::endl;
    return 0;
}

int add(int a, int b) {
    return a + b;
}
A7
B34
C0
DCompilation error
Attempts:
2 left
💡 Hint
Look at what the function 'add' returns when called with 3 and 4.
Predict Output
intermediate
2:00remaining
Function declaration without definition
What error will this C++ code produce when compiled?
C++
#include <iostream>

int multiply(int x, int y);

int main() {
    std::cout << multiply(2, 3) << std::endl;
    return 0;
}
ALinker error: undefined reference to 'multiply(int, int)'
BRuntime error
COutput: 6
DCompilation error: multiply not declared
Attempts:
2 left
💡 Hint
The function is declared but not defined anywhere.
Predict Output
advanced
2:00remaining
Function overloading and output
What is the output of this C++ program?
C++
#include <iostream>

int compute(int x) {
    return x * 2;
}

double compute(double x) {
    return x / 2.0;
}

int main() {
    std::cout << compute(10) << " " << compute(10.0) << std::endl;
    return 0;
}
A20 0
BCompilation error
C10 5
D20 5
Attempts:
2 left
💡 Hint
Look at which function is called for int and double arguments.
🔧 Debug
advanced
2:00remaining
Identify the error in function definition
What error does this code produce?
C++
#include <iostream>

void greet();

int main() {
    greet();
    return 0;
}

void greet() {
    std::cout << "Hello, world!" << std::endl
}
ALinker error: undefined reference to 'greet()'
BSyntax error: missing semicolon
CRuntime error
DNo error, outputs 'Hello, world!'
Attempts:
2 left
💡 Hint
Check the last line of the function definition.
🧠 Conceptual
expert
2:00remaining
Function declaration and definition order
Which statement is true about this code snippet?
C++
#include <iostream>

int square(int x) {
    return x * x;
}

int main() {
    std::cout << square(5) << std::endl;
    return 0;
}
AFunction must be declared before main to compile
BFunction cannot be called before its declaration or definition
CFunction definition before main allows calling without prior declaration
DThis code will cause a linker error
Attempts:
2 left
💡 Hint
Think about whether a function definition counts as a declaration.