Function prototype declaration
The function prototype declaration only performs the function name, argument, and return value in the function definition.
// Function prototype declaration int32_t foo (int32_t args1, double args2);
For practical purposes, function prototype declarations are made above the function definition.
// Function prototype declaration int32_t foo (int32_t args1, double args2); // Function definition int32_t foo (int32_t args1, double args2) { // process }
In a program configured to split compilation, the function prototype declaration is placed in the header.
mylib.h
// Function prototype declaration int32_t foo (int32_t args1, double args2); <pre> <b> mylib.c </b> <pre> // Function definition int32_t foo (int32_t args1, double args2) { // process }
What is the function prototype declaration for?
What is the function prototype declaration for?
You need to know a little about the C language specifications and compilation techniques.
C language cannot find a function if there is a function call after the function definition.
int main (void) { // can not find foo (1, 2.5); } // Function definition int32_t foo (int32_t args1, double args2) { // process }
Function prototype declarations can solve this.
int32_t foo (int32_t args1, double args2); int main (void) { // Found foo (1, 2.5); } // Function definition int32_t foo (int32_t args1, double args2) { // process }
Suppose you want to call this function from yet another source file. In such a case, only the function specifications, that is, the name, arguments, and return value, are exposed in the header, not the function definition that is the substance of the process.
mylib.h
int32_t foo (int32_t args1, double args2);
mylib.c
#include "mylib.h" // Function definition int32_t foo (int32_t args1, double args2) { // process }
myapp.c
#include "mylib.h" int main (void) { // Found foo (1, 2.5); }
This is all you need to compile other source files (here myapp.c). Of course, you can also compile mylib.c. In other words, in order to compile, you don't need the substance of the function, you just need to know the specifications of the function.
# Split compilation gcc -c -o myapp.o myapp.c gcc -c -o mylib.o mylib.c
Then, at the timing of link, the entity of the function is connected.
#Link to generate executable file gcc -o myapp myapp.o mylib.o