Local variables can only be used within the function they are declared in. Global variables have scope throughout the entire program. Static variables have the scope of a local variable but the lifetime of a global variable - they retain their value between function calls.
Local variables can only be used within the function they are declared in. Global variables have scope throughout the entire program. Static variables have the scope of a local variable but the lifetime of a global variable - they retain their value between function calls.
Local variables can only be used within the function they are declared in. Global variables have scope throughout the entire program. Static variables have the scope of a local variable but the lifetime of a global variable - they retain their value between function calls.
Variables Before moving to Functions, we have to understand LOCAL, GLOBAL and STATIC variables. LOCAL VARIABLES WHAT IS A LOCAL VARIABLE?
Local variable that is declared within
a subprogram (function) and can only be used in that function where it was declared.
The program uses a declared
function sqr().
The program will not run because of
error: 'num' was not declared in this scope LOCAL VARIABLES – correct code LOCAL VARIABLES The variable num is local to the msg function since it was declared in that function. Being a local variable, each time the msg function is called, the variable num is created, and each time the msg function ends, the variable num is destroyed. The variable num is not continued but rather, the variable num starts all over again each time the msg function is called. GLOBAL VARIABLES WHAT IS A GLOBAL VARIABLE?
Global Variable has scope
throughout the program. Since the variable has scope throughout the program, its lifetime does not end until the program ends. To make variable global, it must be decaled above all function definition, generally with function prototypes. GLOBAL VARIABLES GLOBAL VARIABLES While the good news is that global variables can be accessed throughout your program, this is also the bad news. The fact that global variable can be accessed and changed anywhere in your program makes it difficult to determine its present value if the code is complex. Since all functions can accessed that global variable any code within any function might affect its value. STATIC VARIABLES WHAT IS A STATIC VARIABLE?
Normally local variable’s scope is limited to
the function in which it was declared, and its lifetime ends when the function ends. While a global variable had scope throughout the entire program; thus, its lifetime will not end until the entire program ends.
A static local variable different. A static
local variable has the scope of a local variable but the lifetime of a global variable. The following code will demonstrate the effect of static local variable: -=* END *=- Credits to: Maria Ivy M. Guese Used by: John Mark S. Policarpio REFERENCES: http://www.w3schools.in https://www.programiz.com/cpp-programming