what is function prototype in c++
A function prototype in C++ is a declaration of a function before its definition. It tells the compiler the function’s name, return type, and parameter types , so the compiler can check calls to it correctly.
Quick Scoop
Example:
cpp
int add(int a, int b); // function prototype
int main() {
int x = add(2, 3);
}
int add(int a, int b) { // function definition
return a + b;
}
What it does
- Lets you call a function before its body is written.
- Helps the compiler do type checking on arguments and return values.
- Is often placed in a header file for code sharing across multiple files.
Prototype vs definition
Item| Prototype| Definition
---|---|---
Purpose| Announces the function| Implements the function
Body| No body| Has a body
Ends with ;| Yes| No
Simple rule
If you only write:
cpp
int add(int, int);
that is a prototype. If you write the full code inside { ... }, that is the
definition.
Bottom line
A function prototype is basically the function’s promise to the compiler: “Here is my name, what I return, and what inputs I need”.