Bird
Raised Fist0
C++programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a function declaration in C++?
easy
A. To execute the function's code
B. To tell the program the function's name, return type, and parameters
C. To create a variable inside the function
D. To print output to the screen

Solution

  1. Step 1: Understand function declaration role

    A function declaration informs the compiler about the function's name, return type, and parameters without providing the actual code.
  2. Step 2: Differentiate declaration from definition

    The function definition contains the actual code to run, while declaration only tells the program what to expect.
  3. Final Answer:

    To tell the program the function's name, return type, and parameters -> Option B
  4. Quick Check:

    Function declaration = function signature [OK]
Hint: Declaration = function signature only, no code [OK]
Common Mistakes:
  • Confusing declaration with definition
  • Thinking declaration runs code
  • Mixing variable creation with function declaration
2. Which of the following is a correct function declaration in C++?
easy
A. int add(int a, int b);
B. int add(int a, int b) { return a + b; }
C. int add = (int a, int b);
D. add(int a, int b) int;

Solution

  1. Step 1: Identify correct syntax for declaration

    A function declaration includes the return type, function name, parameter list, and ends with a semicolon without a body.
  2. Step 2: Check each option

    int add(int a, int b); matches declaration syntax. int add(int a, int b) { return a + b; } is a definition. Options C and D have invalid syntax.
  3. Final Answer:

    int add(int a, int b); -> Option A
  4. Quick Check:

    Declaration ends with semicolon, no body [OK]
Hint: Declaration ends with semicolon, no braces [OK]
Common Mistakes:
  • Writing function body in declaration
  • Missing semicolon at end
  • Incorrect parameter syntax
3. What will be the output of this code?
int multiply(int x, int y);

int main() {
int result = multiply(3, 4);
std::cout << result;
return 0;
}

int multiply(int x, int y) {
return x * y;
}
medium
A. 0
B. 7
C. Error: multiply not defined
D. 12

Solution

  1. Step 1: Understand function declaration and definition

    The function multiply is declared before main and defined after main, which is allowed in C++.
  2. Step 2: Calculate multiply(3, 4)

    The function returns 3 * 4 = 12, which is printed by std::cout.
  3. Final Answer:

    12 -> Option D
  4. Quick Check:

    3 * 4 = 12 [OK]
Hint: Declaration before use, definition after is valid [OK]
Common Mistakes:
  • Thinking definition must be before main
  • Confusing declaration with definition
  • Expecting compile error without definition before main
4. Identify the error in this code:
void greet();

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

void greet() {
std::cout << "Hello!" << std::endl
}
medium
A. Return type of greet should be int
B. Function greet is not declared
C. Missing semicolon after std::endl
D. Missing parentheses in greet call

Solution

  1. Step 1: Check function declaration and definition

    The function greet is declared and defined correctly with void return type and no parameters.
  2. Step 2: Identify syntax error in function body

    The line with std::cout << "Hello!" << std::endl is missing a semicolon at the end, causing a syntax error.
  3. Final Answer:

    Missing semicolon after std::endl -> Option C
  4. Quick Check:

    Statements must end with semicolon [OK]
Hint: Check semicolons after output statements [OK]
Common Mistakes:
  • Forgetting semicolon after std::endl
  • Thinking declaration missing causes error
  • Assuming wrong return type
5. You want to declare a function int max(int a, int b); and define it later. Which of these definitions correctly implements it to return the larger number?
hard
A. int max(int a, int b) { if (a > b) return a; else return b; }
B. int max(int a, int b) { return a + b; }
C. int max(int a, int b) { if (a < b) return a; else return b; }
D. int max(int a, int b) { return 0; }

Solution

  1. Step 1: Understand function purpose

    The function max should return the larger of the two integers a and b.
  2. Step 2: Evaluate each definition

    int max(int a, int b) { if (a > b) return a; else return b; } returns a if a > b, else b, correctly returning the maximum. Options A, B, and C do not correctly return the maximum value.
  3. Final Answer:

    int max(int a, int b) { if (a > b) return a; else return b; } -> Option A
  4. Quick Check:

    Return larger value using if-else [OK]
Hint: Use if (a > b) return a; else return b; [OK]
Common Mistakes:
  • Returning sum instead of max
  • Returning smaller value by mistake
  • Returning constant zero
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard