![]() |
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. |
Re: Scope of Variables C++ Static Variable Syntax: Code: static int n;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()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.