C language-custom functions, custom functions
C language custom functions
-----------------------
-- 1 -- custom Function Definition1.1 No parameter No Return Value Function1.2 return value functions without Parameters1.3 functions with parameters without return values1.4 functions with parameters and returned values-- 2 -- function parameters2.1 introduction and use of formal parameters2.2 introduction and use of actual parameters2.3 parameter passing process of functions-- 3 -- function declaration and call3.1 function declaration3.2 function call
-----------------------
[Written at the beginning :]
"A function is a piece of code that can be used repeatedly to complete a function independently. It can receive or not receive data transmitted by users.
Custom functions are divided:
No parameter No Return Value Function
Return Value Function Without Parameters
Function Without Return Value
Function with parameters and returned values
Writing format:
Return Value Type Function Name (function parameter ){
Function body;
}
Functions are defined in three steps: 1. Declaration 2. Function Definition 3. function call
』
-- 1 -- custom function definition 1.1 No parameter No Return Value Function
Definition Format
General format:
Void function name (){
Declaration part; // defines local variables
Statement;
}
For example:
Void sum () {// statement}
1.2 return value functions without Parameters
Definition Format
General format:
Return Value Type Function Name (){
Declaration part;
Statement;
Return value type: variable or constant;
}
For example:
int sum(){ return 1 + 2;}
1.3 functions with parameters without return values
Definition Format
General format:
Void function name (form parameter table column ){
Declaration part;
Statement;
}
For example:
Void sum (int x, int y) {// operation}
1.4 functions with parameters and returned values
Definition Format
General format:
Return Value Type Function Name (shape parameter ){
Declaration part;
Statement;
Return value type variable or constant;
}
For example:
Int sum (int x, int y) {// return x + y ;}
-- 2 -- introduction and use of function parameters in the form of parameter 2.1
When defining a function, the parameter in parentheses after the function name is referred to as the row parameter.
/*** Custom sum function ** @ param x parameter 1 * @ param y row parameter 2 ** @ return returns the operation result */int sum (int x, int y) {// return x + y ;}
Note:
1) space is not allocated immediately after the parameter is defined.
When a function is called, the storage space is actually allocated.
2) if multiple parameters exist, separate them with commas (,).
3) The parameter types can be different.
4) The variable with the same name as the form parameter cannot be defined in the function.
The parameter only appears in the function definition and can be used within the entire function body. If you leave the function, you cannot use it again.
2.2 introduction and use of actual parameters
When a function is called, the parameters following the function name are short for real parameters.
Real parameters appear in the main function. After the function is called, the real parameters cannot be used.
The function of the form parameter and the real parameter is to transfer the value. When a function is called, the main function transfers the value of the real parameter to the shape parameter of the called function, so that the main function transfers data to the called function.
Int main (int argc, const char * argv []) {int sum (int x, int y); // function declaration int result = sum (3, 5 ); // here 3, 5 is the real parameter return 0 ;} /*** custom sum function ** @ param x parameter 1 * @ param y row parameter 2 ** @ return returns the operation result */int sum (int x, int y) {// return x + y ;}
Note:
Real parameters can be variables, constants, or expressions.
The main and called functions are relatively speaking.
2.3 parameter passing process of functions
1) The parameters are allocated only when called. The allocated memory units are immediately released at the end of the call.
2) real parameters can be constants, variables, expressions, and functions. No matter what type of real parameters are, they must have a definite value during function calling, to pass these values to the form parameter.
Therefore, values should be pre-assigned, input, and other methods to obtain a definite value for the real parameter.
3) the quantity, type, and sequence of real parameters and form parameters must be strictly consistent.
-- 3 -- Declaration of the function and declaration of calling the 3.1 Function
In the main function, you should describe (declare) The called function before calling the called function ). If no declaration is made, the called function should be written above the main function.
General format:
The return value type is called several (type parameters, type parameters ...);
Or
The return value type is called by a number (type, type ...);
Int sum (int x, int y); // function declaration
3.2 function call
The general format is:
Function Name (real-name table );
Int result = sum (3, 5); // call the sum (int, int) function.
Note:
1) function expression
A function appears in an expression.
M = max (34,10) + 10;
2) function call is used as the real parameter of another function.
3) nested function call
Max (34,44), 299 );
4) The function name cannot be the same as the variable name.
Int sum = sum (3, 5); // The function name cannot be the same as the variable name.
[Written at the end :]
"Encapsulation, as one of the four features of object-oriented, hides the internal details of the code and provides an external interface. Although the C language is a process-oriented language, you can combine some common user-defined functions into a tool class for reuse 』