BZU PAGES: Find Presentations, Reports, Student's Assignments and Daily Discussion; Bahauddin Zakariya University Multan

BZU PAGES: Find Presentations, Reports, Student's Assignments and Daily Discussion; Bahauddin Zakariya University Multan (http://bzupages.com/)
-   Object Oriented Programming (http://bzupages.com/35-object-oriented-programming/)
-   -   Scope of Variables C++ (http://bzupages.com/f35/scope-variables-c-993/)

BSIT07-01 14-10-2008 01:51 AM

Scope of Variables C++
 
1 Attachment(s)
Scope of variables

The scope of variables declared within a function or any other inner block is only their own function or their own block and cannot be used outside of them. For example, in the previous example it would have been impossible to use the variables a, b or r directly in function main since they were variables local to function addition. Also, it would have been impossible to use the variable z directly within function addition, since this was a variable local to the function main.


Therefore, the scope of local variables is limited to the same block level in which they are declared. Nevertheless, we also have the possibility to declare global variables; These are visible from any point of the code, inside and outside all functions. In order to declare global variables you simply have to declare the variable outside any function or block; that means, directly in the body of the program.

BSIT07-01 14-10-2008 02:05 AM

Re: Scope of Variables C++
 
Static Variable

Syntax:
Code:

static int n;
we use static keyword before declaration.

One use of Static Variables is in functions,
when we want to store a value within a function, and not want to destroy it even when the function is ended...
eg: we want to know how many times a function is called.
Code:

void myfunction()
{
static int count=0;
/* Body of Our Function

*/

count++;

}

Static variable count is created when first time we call this function. And is not destroyed until execution of whole program is completed.

when ever we call this function, count is incremented by 1.
So We may easily calculate the number of times a function is executed


All times are GMT +5. The time now is 12:42 PM.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.