0
0
C++programming~5 mins

Function declaration and definition in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AThe function's body (code inside)
BThe function's name
CThe function's return type
DThe function's parameters
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
What keyword is used to declare a function that returns no value?
Aempty
Bvoid
Cnull
Dnone
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);
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
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.