6
Name visibility : Named entities, such as variables, functions, and compound types need to be declared before being used in programs. The point in the program where this declaration happens influences its visibility. An entity declared outside any block has global scope called Global variables” , meaning that its name is valid anywhere in the code. While an entity declared within a block, in a function or as a function parameters has block scope, and is only visible within the specific block in which it is declared, but not outside it. Variables with block scope are known as Local variables”. For example : int a; // global variable void function1 () { int b; // local variable b = 0; } void function2 () { a = 1; // a is a global variable b = 2; // b is a local variable but not visible as it is not declared in this function } we can also call a variable as Local variable ,If we can also declare it in the block or constructor. void c() { int b=2; // b is a local variable { // starting mark of block int c=3; // c is also a local variable cout<<c<<endl; } // ending mark of block }

Name visibility--elobal or local

Embed Size (px)

DESCRIPTION

The variables that we will declare in the program have different scope depending on where we had declare the variables either in block or in function etc.In this slide i have explained it in a better way for easy to understand

Citation preview

  • Name visibility :

    Named entities, such as variables, functions, and compound typesneed to be declared before being used in programs. The point in the programwhere this declaration happens influences its visibility.

    An entity declared outside any block has global scope calledGlobal variables , meaning that its name is valid anywhere in the code.While an entity declared within a block, in a function or as a functionparameters has block scope, and is only visible within the specific block inwhich it is declared, but not outside it. Variables with block scope are known asLocal variables.

    For example :

    int a; // global variable

    void function1 (){ int b; // local variable b = 0;}

    void function2 (){ a = 1; // a is a global variable b = 2; // b is a local variable but not visible as it is not

    declared in this function}

    we can also call a variable as Local variable ,If we can also declare it inthe block or constructor.void c() {

    int b=2; // b is a local variable

    { // starting mark of block int c=3; // c is also a local variablecout

  • Here b can be accessed inside the block also.

    If the global variable value is changed anywhere in the program (i.e. without declaring it ) ,it's value will be changed.

    #include using namespace std; int b=0; // b is a global variable void c() {

    cout

  • Here is a set of programs for u for practice.

    1.#include using namespace std ;int main () {

    int x = 1; int y = 2; {

    x = 3; int y; y = 5; cout

  • }3.

    #include using namespace std ;

    int x=0;

    void c(){

    cout

  • cout
  • {

    int a=10; b=12;

    d(); e();

    cout