Deep understanding of local variables and global variables _c languages in C + + programming

Source: Internet
Author: User
Tags function definition function prototype

Local variables

Variables defined within a function are internal variables that are valid only within the scope of this function, that is, they can only be used within this function and cannot be used outside of this function. Similarly, variables defined in compound statements are valid only within the scope of this compound statement. This is referred to as a local variable (locally variable). Such as:


Some instructions for local variables:
1 the variable (m, n) defined in main function main is also valid only in the main function and not in the entire file or program because it is defined in the main function. The main function also cannot use variables defined in other functions.

2 variables with the same name can be used in different functions, which represent different objects and do not interfere with each other. For example, variables B and C are defined in the F1 function, and if variables B and C are defined in the F2 function, they take up different cells in memory and are not confused.

3 You can define variables in a compound statement within a function that are valid only in this compound statement, which is also called a sub program or program block.

4 The formal parameter is also a local variable. For example, the formal parameter A in the F1 function is also valid only in the F1 function. Other functions cannot be invoked.

5 The name of the parameter that appears in the function declaration, whose scope is within the parentheses of the bank. In fact, the compiler system ignores variable names in function declarations, even if they are not allocated storage cells when they are invoked. For example:

int max (int a, int b);//function declaration A, B
int max (int x, int y)//function definition, parameter is x, y
{
 Cout<<x<<y<<endl ; Legal, x, y valid
 cout<<a<<b<<endl;//illegal in function body, a, B invalid in function body
}

A and B in the body of the Max function are considered undefined at compile time.
Global Variables

As described earlier, the compilation unit of a program is the source program file, and a source file can contain one or several functions. A variable defined within a function is a local variable, and a variable defined outside the function is an external variable, called a global variable (variable, also called the whole variable). The valid range for a global variable is from the location where the variable is defined to the end of the source file. Such as:


P, Q, C1, c2 are all global variables, but their scope is different, in the main function and the F2 function can use global variables p, q, C1, C2, but in the function F1 can only use global variables p, q, and can not use C1 and c2.

In a function, you can use both local variables in this function and valid global variables.

Some instructions for global variables:
1 The function of setting global variables is to increase the channel of data connection between functions.

2 recommends that you do not use global variables when necessary, because:
Global variables occupy storage units throughout the execution of the program, rather than opening the unit only when needed.
It reduces the versatility of the function because it is affected by external variables when executing the function. If you move a function to another file, you also move the relevant external variable along with its value. However, if the external variable has the same name as the variable of other files, the problem arises, which reduces the reliability and versatility of the program. In the program design, the module is required to have strong cohesion and weak coupling with other modules. That is, the function of the module should be single (do not put many unrelated functions into a module), and the interaction with other modules as little as possible, and the use of global variables does not conform to this principle.
General requirements of the program in the function of a closed body, in addition to the "real reference---Formal ginseng" channels and outside contact, there is no other channels. This program has good portability and readability.
Using a global variable too much can reduce the clarity of the program. The value of the global variable can be changed when each function executes, and the program is prone to error. Therefore, you want to restrict the use of global variables.

3 If the global variable has the same name as a local variable in the same source file, the global variable is masked within the scope of the local variable, that is, it does not work.

The valid range of a variable is called scope (scope) of the variable. To sum up, variables have 4 different scopes, file scopes (files scope), functional scopes (function scope), block scopes (blocks scope), and function prototype scopes (functions prototype scope). The file scope is global, and the other three are local.

In addition to variables, any entity represented by an identifier has a scope, and the concept is similar to the scope of the variable.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.