Recall & Review
beginner
What is a function declaration in C++?
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?
✗ Incorrect
A function declaration includes the name, return type, and parameters but does not include the body.
Where is a function definition usually placed?
✗ 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?
✗ Incorrect
The keyword 'void' is used to declare functions that do not return any value.
Which of the following is a correct function declaration?
✗ 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?
✗ 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.