Monday, March 9, 2015

Global and Local Variables

Scope

  • The scope of a declaration is the block of code where the identifier is valid for use

  • A global declaration is made outside the bodies of all function and outside the main program. it is normally grouped with the other global declarations and places at the beginning of the the program file.
  • Local declaration is one that is made inside the body of a function. locally declared variables cannot be accessed outside of the function they were declared in
  • It is possible to declare the same identifier name is different parts of the program.

Local Variables

  • We have used thus have been local variables
  • A Local variable is a variable which is either a variable declared within the function.
  • As you may have encountered in your programming if we declare variables in function then we can only use them within that function. This is a direct result of placing our declaration statements inside functions.


Global variable

  • A global variable def is a variable which is accessible in multiple scopes
  • It is important to note that global variables are only accessible after they have been declared.
  • Undisciplined use of global variables may lead to confusion and debugging difficulties.
  • Instead of using global variable in function, try passing local variables  by reference.


No comments:

Post a Comment