Slide # 1

Slide # 1

Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts Read More

Slide # 2

Slide # 2

Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts Read More

Slide # 3

Slide # 3

Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts Read More

Slide # 4

Slide # 4

Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts Read More

Slide # 5

Slide # 5

Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts Read More

Friday, November 12, 2010

C++ - Variables

Variables

Variables which are declared into the frames from one function are called local variables. They can be used in expressions which are just in the function. The local variables aren’t available for the others outside functions.






#include <iostream>


using namespace std;

void func();

int main(){

                int x;

                x = 10;


                func();

                cout<<x<<endl;

                return 0;

}


void func()

{

                int x;

                x = -199;

                cout<<x<<endl;


}


The local variable is designing when invoke the function, but it is destroying when it goes out from the function. That means that the function doesn’t retains its value between two invokes from the function. In the literature the local variable often is called and automatic and somewhere it can meets the modification on the variable auto, but for lots of compilers that implies and there isn’t necessity of especially investiture.


Which is the manner for the variable to retain its value between few calls from the functions? One solution is the global variable. The global variable retains its value while the program exists. The global variable is declared outside from the all functions. Global variable can be addressed to each function, it is available for all functions it the program.







#include <iostream>

using namespace std;

void func1();

void func2();


int count;


int main()


{

                int i;

                for(i=0; i<10; i++)

                {

                                count = i * 2;


                                func1();

                }

                return 0;

}

void func1()


{

                cout<<"count: "<<count<<endl;

                func2();

}


void func2()

{

                int count;

                for(count=0; count<3; count++)

                                cout<<"."<<endl;


}


 

0 comments:

Post a Comment