C language entry to C ++ master: C language entry function (5)

Chapter 7 Functions

Section 5 Local Variables and Global Variables

Local variable

Variables defined inside a function are called local variables, and are only valid within the scope of this function, that is, they can only be used
within a function , and these variables cannot be used outside the function

  • Different functions can use the same variable name without interfering with each other (see memory addresses are not the same)

  • The formal parameter is also a local variable and is only valid in the corresponding function

#include <iostream>

void func1(int tmp) {
    // 无法使用main函数中定义的m,n,k
    // 这里能使用的变量是tmp,x,y
    int x, y;//局部变量
    int m, n;//和main函数中m,n不相同
    return;
}

int main() {
    // m.n,k这三个变量只在主函数中有效,虽然主函数调用了其他函数,但在其他函数中依旧无法使用主函数中的局部变量
    int m, n;
    int k = 4;

    return 0;
}
  • A special way of writing
#include <iostream>

int main() {
    int a = 2, b = 3;
    {
        // 用大括号写一段代码,大括号扩起来的叫复合语句,
        // 在复合语句中定义的变量,这些变量只在本复合语句中有效
        // 复合语句也叫程序块
        int c;//有效范围只在符合语句内,一旦离开复合语句,变量c的内存就被系统释放了
        c = a + b;
    }
    return 0;
}

2. Global variables

  • Definition: Variables defined outside the function are called global variables (external variables)

  • Global variables can be shared by other functions in this file.The effective range of global variables starts from the position where the variable is defined to the end of the source program file.

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

The advantages and disadvantages of global variables

advantage:
  • Increased the data communication channel between functions.If a function changes the value of a global variable, it can affect other functions, which is
    equivalent to having a direct transmission channel between each function. Parameters to pass parameters
Disadvantages:
  • Use global variables only when necessary, because global variables occupy memory during the entire program running cycle,
    unlike local variables inside functions.The characteristic of local variables inside functions is that after the function is executed, these local variables occupy Of memory will be reclaimed by the system

  • Reduced the versatility of the function, because the function execution depends on these external global variables.If the function is migrated to another file,
    then these related external variables have to be migrated together, and if you migrate to another file There is also a global variable with the same name, then there is a conflict

  • The global quantity reduces the clarity and readability of the program, and it is difficult for the person reading the program to judge the value of each external variable (the function can modify the value of the global variable)

#include <iostream>

int v_g = 100;//全局变量,在函数外定义的变量

int f1(int a) {
   v_g = 88;
   return 1;
}


char c1_g, c2_g;

char f2(int x, int y) {
   int i, j;
   return 0;
}

int main() {
   int v = 1;
   f1(v);
   printf("v_g in main : %d", v_g);
   return 0;
}

Explanation

  • If a function wants to refer to the global variable defined after it, you can use a keyword called extern to make an external global variable description,
    indicating that the variable is defined outside the function. So, the global variable is placed before all functions. Omit global variable description

  • Distinguish between external variable descriptions and external variable descriptions: a) The external variable definition can only be defined once, the location is outside of all functions, memory is allocated when defined,
    and values ​​can be initialized when defined, and there can be many external variable descriptions in the same file Second, external variables indicate that no memory is allocated

#include <iostream>

extern int v_g;// 外部变量说明(不分配内存), 表示在某个地方定义了c1,c2
int f1(int a) {
    v_g = 88;
    return 1;
}

int v_g = 100;//全局变量,在函数外定义的变量


int main() {
    int v = 1;
    f1(v);
    printf("v_g in main : %d", v_g);
    return 0;
}
  • In the same source file, if the global variable and the local variable have the same name, the global variable does not work within the scope of the local variable
#include <iostream>

extern int v_g;// 外部变量说明(不分配内存), 表示在某个地方定义了c1,c2
int f1(int v_g) {
    v_g = 88;//这里v_g是局部变量,要注意,仔细考虑一下,调用v_g并不会改变全局的v_g
    return 1;
}

int v_g = 100;//全局变量,在函数外定义的变量


int main() {
    int v = 1;

    v_g = 3;
    printf("v_g in main : %d\n", v_g);
    
    {
        int v_g;
        v_g = 1;
        printf("v_g in {} : %d\n", v_g);
    }
    f1(v);
    printf("v_g in main after f1: %d", v_g);
    return 0;
}
Published 359 original articles · praised 248 · 660,000 views

Guess you like

Origin blog.csdn.net/Felaim/article/details/105666115