C Language Study Notes
Function call and variable scope
A program cannot be separated from a function. A function is the unit of a program, whether it is a high-level language or a C language. All types of functions play both their own roles and play an important role in the whole.
I. function call
Basic functions:
Common form of non-parameter functions |
Common parameter functions |
Function type description) { Function body; } |
Function type description) { Function body; } |
Some precautions for using a program instance.
# Include <stdio. h> float ave (float B [], int m); // function declaration main () {float a [20], aver; int j, n; printf ("Number of input class \ n"); scanf ("% d", & n); printf ("input score: \ n"); for (j = 0; j <n; j ++) {scanf ("% f", & a [j]);}/* call the ave function. Values a and n are real parameters, a is the array name, representing the first address of the array, and n is the value. */Aver = ave (a, n); printf ("average score: % f \ n", aver);} float ave (float B [], int m) // functions ave B [] and m are form parameters, array B accepts the first address of a [], and m accepts the value of n {int I; float aver1, sum = 0; for (I = 0; I <m; I ++) {sum + = B [I];} aver1 = sum/m; return (aver1 ); // return the average value to the main function for output}
|
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/213H510Z-0.png "title =" image 1.png "/> |
1. Each function must be defined separately. Nesting is not allowed. That is, another function cannot be defined within a function.
2. parameters: parameters set during Function Definition
Real parameters: parameters used for Parameter Function calls
3. How to pass the real parameter to the form parameter:
One-way value transfer mode)
The real parameter a in the above program is passed to the form parameter B [], and a is the array name, representing the first address of the array. The real parameter array name a does not pass the score of all the students in data a to the form parameter array B, but passes the first address of the real parameter array to B, so that the two arrays share the same bucket, that is, a [0] = B [0] ......
Address Transfer Method
The real parameter n in the above program is passed to the form Parameter m, and n and m are real values.
Note: In the called function, the type of the parameter must be the same as that of the real parameter. For example, the [] of the parameter is float, the array B [] in the form parameter must also be of the float type.
4. function return value
Use the return statement in the called function to obtain the return value of the function. A function can return only one value using the return statement.
The expression type in the return statement should be consistent with the function type description. For example, in a program, the aver1 type in the return (aver1) function of the aver function is the same as that of the function of the aver function, that is, they are all float.
However, when the return type of the expression in the return statement is different from that of the function, the return type must be the same as that of the function type description. The system automatically converts the return value to the type of the function type specifier.
When the data type returned by a function is int, the function type specifier can be omitted during function definition. That is to say, the default return type is integer.
5. function declaration
Everything in the program follows the principle of first defining and then using, and the function is no exception.
In the above program, the aver function is defined after the main function, so it does not follow the principle of first definition and then use. To prevent such a thing from happening, we should first use a function declaration to avoid this, that is, the function declaration statement added before the main function: float ave (float B [], int m );
If you put the function aver before the main function, you do not need to add this statement.
II. Scope of Variables
Int m, n; Double fun (int x, int y) { Int m, n; .... } Main () { Int a, B; If () { Int x, y; } } |
1. Local Variables
Local variables can only be used locally. In the above functions, x, y, m, and n in fun can only be used in function fun. Once the category of fun is applied, it will be useless. For example, x and y in if can only be used in if.
2. Global Variables
The m and n in the first line are global variables, which can be used in all functions.
However, we can see that m and n are the same as m and n in function fun. When we execute m and n in function fun, we should use global variables or local variables, the answer is local variables.
This article from the "Zhao Yuqiang blog" blog, please be sure to keep this source http://zhaoyuqiang.blog.51cto.com/6328846/1264485