Declaration vs Definition | Prototype
Functions in C++ consist of 2 elements:
- Declaration: the function's name with parameters (if they exist).
- Definition: the functionβs body.
void myFunc() { // Declaration
// Definition
}
The function declaration is also called the functionβs prototype. The code body (the definition) can be defined separately. Itβs a good rule to write before the main function prototypes of other functions to understand how many functions we have and which arguments it passes. After the main function, you should specify the functionβs definitions:
// Function prototype
void myFunc(string name);
int main() {
myFunc("Sheldon");
return 0;
}
// Function definition
void myFunc(string name) {
cout << "Hi " << name;
}
It doesnβt affect your code if you specify prototypes or not. We wonβt do this in further chapters since our code is still pretty simple and doesnβt need strict structurization.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Ask me questions about this topic
Summarize this chapter
Show real-world examples
Awesome!
Completion rate improved to 2.94
Declaration vs Definition | Prototype
Swipe to show menu
Functions in C++ consist of 2 elements:
- Declaration: the function's name with parameters (if they exist).
- Definition: the functionβs body.
void myFunc() { // Declaration
// Definition
}
The function declaration is also called the functionβs prototype. The code body (the definition) can be defined separately. Itβs a good rule to write before the main function prototypes of other functions to understand how many functions we have and which arguments it passes. After the main function, you should specify the functionβs definitions:
// Function prototype
void myFunc(string name);
int main() {
myFunc("Sheldon");
return 0;
}
// Function definition
void myFunc(string name) {
cout << "Hi " << name;
}
It doesnβt affect your code if you specify prototypes or not. We wonβt do this in further chapters since our code is still pretty simple and doesnβt need strict structurization.
Thanks for your feedback!