C Function Prototypes
In this page:
return_type function_name(type param1, type param2); // prototype
return_type function_name(type param1, type param2) { // definition
// body
}
What is a Function Prototype?
A function prototype declares a function's return type, name, and parameter types to the compiler before the function's full body appears later in the file, so the compiler knows how to correctly interpret any earlier calls to it.
Example: What is a Function Prototype?
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b);
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", add(2, 3));
// Return 0 to signal that the program finished successfully
return 0;
}
int add(int a, int b) {
// Send a + b back to the caller
return a + b;
}
Login to try C/C++/Java/PHP code in the editor
Why Use Prototypes?
Because C compilers read a file top to bottom, calling a function before its definition appears would otherwise cause an error -- prototypes solve this by giving the compiler enough information about the function upfront.
Example: Why Use Prototypes?
// Include standard input/output (printf, scanf)
#include <stdio.h>
int square(int n);
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", square(4));
// Return 0 to signal that the program finished successfully
return 0;
}
int square(int n) {
// Send n * n back to the caller
return n * n;
}
Login to try C/C++/Java/PHP code in the editor
Prototype Syntax
A prototype repeats the function's header exactly as it will appear in the definition, but ends with a semicolon instead of a body, and the parameter names inside it are optional since only their types actually matter to the compiler.
Example: Prototype Syntax
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int, int);
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", add(3, 4));
// Return 0 to signal that the program finished successfully
return 0;
}
int add(int a, int b) {
// Send a + b back to the caller
return a + b;
}
Login to try C/C++/Java/PHP code in the editor
Preventing Compiler Warnings
Modern compilers check every function call against its prototype's declared parameter types and count, catching mismatched arguments as a compile error rather than letting them slip through as a runtime bug.
Example: Preventing Compiler Warnings
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b);
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", add(2, 3));
// Return 0 to signal that the program finished successfully
return 0;
}
int add(int a, int b) {
// Send a + b back to the caller
return a + b;
}
Login to try C/C++/Java/PHP code in the editor
Multiple Prototypes
Listing all of a program's function prototypes together near the top of the file is standard C practice, giving anyone reading the file a quick overview of every function it defines before digging into the implementations.
Example: Multiple Prototypes
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b);
int square(int n);
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d %d", add(2, 3), square(4));
// Return 0 to signal that the program finished successfully
return 0;
}
int add(int a, int b) {
// Send a + b back to the caller
return a + b;
}
int square(int n) {
// Send n * n back to the caller
return n * n;
}
Login to try C/C++/Java/PHP code in the editor
- Calling a function defined after
mainwithout a prototype, which gives an implicit declaration warning or error. - Writing a prototype whose parameter types differ from the definition, so the compiler reports conflicting types.
- Forgetting the semicolon at the end of a prototype line, which causes a syntax error.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: