The scope of variables declared inside a function or any other inner block is simply their own function or their own block and cannot be used outside of them. For instance, it would have been not possible to use the variables a, b or r directly in function main since they were variables local to function addition. In addition, it would have been not possible to use the variable z directly within function addition, because this was a variable local to the function main.
So, the scope of local variables is limited to the same block level in which they are declared. But, we also have the possibility to declare global variables; these are visible from any point of the code, inside and outside all functions. To declare global variables you just have to declare the variable outside any function or block; that means, directly in the body of the program.
No comments:
Post a Comment