Function, simply speaking, is a code block with a name.
In the first reading, we introduced main, which is a function.
Little is known about the basic components of functions:
Return value type, function name, function parameter list, function body
When we call a function, we call it by calling the operator ().
// valfactorial int fact(int val) { int ret = 1; // Local variables are used to save computation results. while(val > 1) ret *= val--; // Assign the product of RET and val to RET, then Val minus 1. return ret; // Return to the result } int main() { int j = fact(5); cout << "5! is " << j << endl; return 0; }
The two steps of function call:
1. Initialize the corresponding parameter with real reference.
Two, transfer control power to the called function.
Difference between formal parameter and real parameter
In the above code, the Val in the list of function parameters is parameter.
The 5 of fact (5) in the call is the argument.
Row participation is a one-to-one correspondence, but the order of parameter initialization is not specified at the C++ language level.
This is a system-level requirement, and this is another problem, which I’ll cover in a separate article.