Variable Scope -------------- In this handout, I will describe briefly the basics behind variable scope. Imagine, if you will, that a function is called with some arguments (variables passed into the function). These variables are seen by the calling function and the current function they are going into. The variables that get declared inside of these functions are only seen by this function. For example: void function1() { int a,b; body of function goes here } void function2() { int b,c; body of this function goes here } The variables in function1() are only seen by that function. The same with the second function (function2()). So it is alright to use the same name for a variable as long as they are in different functions. Notice that the variable b overlaps. Each occurrence is separate and unique though. This is known as local scope (meaning the variables are seen only locally----not outside the current function). The opposite notion involves global variables. Global variable are variables that are defined outside of any function including the main() function. For example: int a,b; /* global variables */ main() { int d; main body goes here } void function1() { int c; function body goes here } The variables a and b above are global variables and may be used in any of the functions (main() and/or function1()). This notion is denoted by global scope (meaning that global variables can be seen anywhere (globally)). So the important thing you need to know about this section is that be careful when assigning variables such as globals and using the same name. This may cause problems. If in the above function called function1() you added the local variable a and you addressed it somewhere in that function, there is a possibility that you will modify the wrong variable (either the global variable a or the local variable a). That is the bulk of this section in the C book.