Home > Article > Backend Development > Can variables with the same name be used in different functions?
can use. The variables described in the function in C language are local variables, which only work within the function and will not affect other functions. Using the same variable name in different functions does not mean they are the same variable.
The operating environment of this tutorial: windows7 system, c99 version, Dell G3 computer.
Variables with the same name can be used in different functions.
Tutorial recommendation: "c language tutorial video"
Variables (local variables) defined inside the function
A variable defined inside a function has its scope limited to the inside of the function and cannot be used outside the function. We call such a variable a local variable (Local Variable). The formal parameters of a function are also local variables and can only be used inside the function.
int f1(int a){ int b,c; //a,b,c仅在函数f1()内有效 return a+b+c; } int main(){ int m,n; //m,n仅在函数main()内有效 return 0; }
A few notes:
1) Variables defined in the main function are also local variables and can only be used in the main function; at the same time, the variables defined in other functions cannot be used in the main function. Variables. The main function is also a function and has equal status with other functions.
2) Formal parameter variables and variables defined in the function body are all local variables. The process of transferring values from actual parameters to formal parameters is also the process of assigning values to local variables.
3) You can use the same variable name in different functions. They represent different data and allocate different memories without interfering with each other or causing confusion.
4) Variables can also be defined in statement blocks, and their scope is limited to the current statement block.
About variable naming
Every piece of runnable C language code contains multiple scopes, even the simplest C language code.
int main(){ return 0; }
This is the simplest, runnable C language code. It contains two scopes, one is the local scope inside the main() function, and the other is the global scope outside the main() function. area.
C language stipulates that two variables with the same name cannot appear in the same scope, otherwise a naming conflict will occur; however, variables with the same name are allowed to appear in different scopes, and their scopes are different and will not conflict with each other. This sentence has two meanings:
Variables with the same name can appear inside different functions, and different functions have different local scopes;
Variables with the same name can appear inside and outside the function. The local scope is inside the function, and the global scope is outside the function.
1) Variables with the same name inside different functions are two completely independent variables. They have no correlation or influence on each other.
#include <stdio.h> void func_a(){ int n = 100; printf("func_a: n = %d\n", n); n = 86; printf("func_a: n = %d\n", n); } void func_b(){ int n = 29; printf("func_b: n = %d\n", n); func_a(); //调用func_a() printf("func_b: n = %d\n", n); } int main(){ func_b(); return 0; }
Running results:
func_b: n = 29 func_a: n = 100 func_a: n = 86 func_b: n = 29
Both func_a() and func_b() define a variable n internally. In func_b(), the initial value of n is 29. After calling func_a() , the n value is still 29, which shows that the n inside func_b() does not affect the n inside func_a(). These two n's are completely different variables. They "don't know" each other at all. They just have the same name. It's like celebrities matching clothes. There are people named Li Hong in Beijing and Yunnan. It's just a coincidence.
2) When the local variable inside the function has the same name as the global variable outside the function, the global variable will be "shielded" in the local scope of the current function and will no longer have any effect. In other words, local variables are used inside the function, not global variables.
The use of variables follows the principle of proximity. If a variable with the same name is found in the current local scope, it will not be searched in the larger global scope. In addition, you can only look for variables from a small scope to a large scope, but not vice versa, using variables in a smaller scope.
Let’s use a specific example to illustrate:
#include <stdio.h> int n = 10; //全局变量 void func1(){ int n = 20; //局部变量 printf("func1 n: %d\n", n); } void func2(int n){ printf("func2 n: %d\n", n); } void func3(){ printf("func3 n: %d\n", n); } int main(){ int n = 30; //局部变量 func1(); func2(n); func3(); printf("main n: %d\n", n); return 0; }
Running results:
func1 n: 20 func2 n: 30 func3 n: 10 main n: 30
Although multiple variables n with the same name are defined in the code, their scopes are different. , so there will be no naming conflicts.
The following is an analysis of the output results:
For func1(), the output result is 20. Obviously, the internal n of func1() is used instead of the external n.
When func2() is called, the actual parameter n in main() will be passed to the formal parameter n in func2(). At this time, the value of the formal parameter n becomes 30. The formal parameter n is also a local variable, so it is used.
func3() outputs 10, using global variables. Because there is no local variable n in func3(), the compiler can only look for variable n outside the function, that is, in the global scope.
The printf() statement in main() outputs 30, indicating that n in main() is used instead of external n.
For more programming-related knowledge, please visit: Programming Teaching! !
The above is the detailed content of Can variables with the same name be used in different functions?. For more information, please follow other related articles on the PHP Chinese website!