A function declaration tells the compiler about a function's name, return type, and parameters without providing the body. It acts like a promise that the function will be defined later.
Click to reveal answer
beginner
What is a function definition in C++?
A function definition provides the actual body of the function. It includes the code that runs when the function is called.
Click to reveal answer
intermediate
Why do we separate function declaration and definition?
Separating declaration and definition helps organize code. Declarations can be in header files so other files know about the function, while definitions are in source files with the actual code.
Click to reveal answer
beginner
Example: Write a function declaration for a function named add that takes two integers and returns an integer.
int add(int a, int b);
Click to reveal answer
beginner
Example: Write a function definition for add that returns the sum of two integers.
int add(int a, int b) {
return a + b;
}
Click to reveal answer
What does a function declaration NOT include?
AThe function's body (code inside)
BThe function's name
CThe function's return type
DThe function's parameters
✗ Incorrect
A function declaration includes the name, return type, and parameters but does not include the body.
Where is a function definition usually placed?
AIn a header (.h) file only
BInside main() function
CIn a source (.cpp) file
DIn a comment block
✗ Incorrect
Function definitions are usually placed in source files (.cpp) where the actual code is written.
What keyword is used to declare a function that returns no value?
Aempty
Bvoid
Cnull
Dnone
✗ Incorrect
The keyword 'void' is used to declare functions that do not return any value.
Which of the following is a correct function declaration?
Aint multiply x, y;
Bint multiply(int x, int y) { return x * y; }
Cmultiply(int x, int y);
Dint multiply(int x, int y);
✗ Incorrect
Option D is a correct function declaration with return type, name, and parameters.
What happens if you call a function that is declared but not defined?
ALinker error occurs
BProgram runs normally
CCompiler error occurs
DFunction runs with empty body
✗ Incorrect
If a function is declared but not defined, the linker cannot find the function's code and throws an error.
Explain the difference between a function declaration and a function definition in C++.
Think about what each part tells the compiler and where the code lives.
You got /6 concepts.
Write a simple function declaration and definition for a function that prints "Hello" to the screen.
Use void for no return and cout for printing.
You got /4 concepts.
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
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.
Step 2: Differentiate declaration from definition
The function definition contains the actual code to run, while declaration only tells the program what to expect.
Final Answer:
To tell the program the function's name, return type, and parameters -> Option B
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
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.
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.
Final Answer:
int add(int a, int b); -> Option A
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
Step 1: Understand function declaration and definition
The function multiply is declared before main and defined after main, which is allowed in C++.
Step 2: Calculate multiply(3, 4)
The function returns 3 * 4 = 12, which is printed by std::cout.
Final Answer:
12 -> Option D
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
The function greet is declared and defined correctly with void return type and no parameters.
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.
Final Answer:
Missing semicolon after std::endl -> Option C
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
Step 1: Understand function purpose
The function max should return the larger of the two integers a and b.
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.
Final Answer:
int max(int a, int b) { if (a > b) return a; else return b; } -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.