1. function declaration
In C and C Programs, a typical way to complete a job is to call a function to do that. Defining a function is a way for programmers to portray how to complete an operation. A function can be called only after being declared first.
In the life of a function, you need to give the name of the function, the type of the return value of the function, and the number and type of parameters that must be provided when the function is called. See the following statement:
Void fun (x, y); // This declaration is meaningless because the parameter type is unknown.
2. Function Definition
To call a function in a program, you must first define it in a certain place (only once ). The definition of a function is the function declaration of the function body. For example:
Extern void swap (int *, int *); // declare
Void swap (int * p, int * q)
{
Int t = * p;
* P = * q;
* Q = t;
}
The Function Definition and all its declarations must describe the same type. Because the parameter name is not part of the type, the parameter names do not need to be consistent.
3. Static variables
Local variables are initialized when the thread reaches its definition. By default, this happens when a function is called, and each function call has its own copy of a local variable. However, when a local variable is declared as static, it will only have a unique static object, and its initialization only occurs when the thread executes its definition for the first time.
See the following program:
# Include <iostream>
Using namespace std;
Void showstatic (int)
{
While (--)
{
Static int n = 0; // initialize once
Int x = 0; // initialize a time for each showstatic () call
Cout <"n =" <n <", x =" <x <endl;
}
}
Int main ()
{
Showstatic (3 );
}
The output of this program is:
N = 0, x = 0
N = 1, x = 0
N = 2, x = 0
As shown in the preceding output, static int n is initialized to 0 only when the function is called for the first time, and local variable x is initialized to 0 every time the function is executed to int x.