Fundamentals of Programming: Global and Local Variables

From Wikibooks, open books for an open world
Jump to: navigation, search

UNIT 1 - ⇑ Fundamentals of Programming ⇑

← Functions and Procedures Global and Local Variables Two-Dimensional Arrays →


Global variable - declared at the start of the program, their global scope means they can be used in any procedure or subroutine in the program


It is seldom advisable to use Global variables as they are liable to cause bugs, waste memory and can be hard to follow when tracing code. If you declare a global variable it will continue to use memory whilst a program is running even if you no longer need/use it.

Local variable - declared within subroutines or programming blocks, their local scope means they can only be used within the subroutine or program block they were declared in


Local variables are initiated within a limited scope, this means they are declared when a function or subroutine is called, and once the function ends, the memory taken up by the variable is released. This contrasts with global variables which do not release memory.

Take a look at this example:

  1. Module Glocals
    
  2. 	Dim number1 as integer = 123
    
  3.  
    
  4. 	Sub Main()
    
  5. 		console.writeline(number1)
    
  6. 		printLocalNumber()
    
  7. 		printGlobalNumber()
    
  8. 	End Sub
    
  9.  
    
  10. 	Sub printLocalNumber
    
  11. 		Dim number1 as integer = 234
    
  12. 		console.writeline(number1)
    
  13. 	End Sub
    
  14.  
    
  15. 	Sub printGlobalNumber
    
  16. 		console.writeline(number1)
    
  17. 	End Sub
    
  18. End Module
    

What would the output be?

  Blank.svg Code Output
Down arrow Hexagonal Icon.svg

123
234
123


Why is this? Well we seem to have two versions of the variable number1.

  • The first version is declared on line 2, this isn't declared inside any sub routines so the variable has Global scope
  • The second version is declared inside the printLocalNumber sub routine. As it is declared inside a sub routine it is only able to be used inside this subroutine. And on line 12 when we use: console.writeline(number1) it prints out the local variable

So looking at the code inside the main sub routine we have 3 different ways of printing out the variable number1.

  1. Line 5. console.writeline(number1):This uses the global value of number1, as it is inside a sub routine with no other local declarations
  2. Line 6. printLocalNumber():This is calling on the subroutine printLocalNumber() which has a local variable number1 contained within it on line 11, therefore it uses the number1 value declared on line 11.
  3. Line 7. printGlobalNumber():This is calling on the subroutine printGlobalNumber() which has no local variable for number1, therefore it uses the global value for number1
We can visualise the scope of the different variables

Rules of thumb: If you want to quickly tell the difference between a global and a local variable use these quick rules. But be warned they might try to trick you!

  • If a variable is declared inside a function or a procedure it is a local variable
  • If a variable is declared inside a iterative or selective statement it is local
  • If the declaration is indented from the left hand boundary it probably meets one of the above criteria and is local
  • If it meets none of the above statements and is declared in the main body of code it is a global variable
Title
What is the difference between a global and a local variable?

Answer :

Global variables are accessible from all parts of a program, whilst local variables are only accessible within a programming construct such as a loop, function or procedure
Why is it a good idea to use local variables instead of global variable?

Answer :

Local variables release memory when you have finished with them, global variables are always stored in memory whether you need them or not
In what situation might you want to use a global variable?

Answer :

When you want to declare a variable that needs to be accessible by all parts of your code
List the global and local variables for the following. What will be the output for input 16:
  1. Module calcAge
    
  2. 	Sub Main()
    
  3. 		dim age as integer
    
  4. 		console.writeline("age?")
    
  5. 		age = console.readline()
    
  6. 		printMonths(age)
    
  7. 		printDays(age)
    
  8. 	End Sub
    
  9. 	Dim months as integer
    
  10. 	Sub printDays(a)
    
  11. 		Dim d as integer
    
  12. 		d = a * 365
    
  13. 		console.writeline(d)
    
  14. 	End Sub
    
  15. 	Sub printMonths(a)
    
  16. 		months = a * 12
    
  17. 		console.writeline(months)
    
  18. 	End Sub
    
  19. End Module
    

Answer :

Locals: age, d

Globals: months

  Blank.svg Code Output
Down arrow Hexagonal Icon.svg

age?
16
192
5840

For the above code how could you make the code more efficient, and why would it be more efficient?

Answer :

Make the months variable a local variable by putting it inside the printMonths(a) sub routine, if you leave it as a global variable it will be taking up memory even when you don't need it.
List the Global and Local variables in the following code, list the output:
  1. Module greetings
    
  2. 	Dim q as integer = 6
    
  3. 	Sub sayGoodbye()
    
  4. 		for y = 1 to q
    
  5. 			console.write("bye,")
    
  6. 		loop
    
  7. 	End Sub
    
  8. 	Sub sayHello()
    
  9. 		dim q as integer = 4
    
  10. 		if q =< 4 then
    
  11. 			console.write("hi,")
    
  12. 		else
    
  13. 			console.write("hello,")
    
  14. 		endif
    
  15. 	End Sub
    
  16. 	Sub Main()
    
  17. 		console.writeline(q)
    
  18. 		sayHello()
    
  19. 		sayGoodbye()
    
  20. 		console.writeline(q)
    
  21. 	End Sub
    
  22. End Module
    

Answer :

Locals: y (on line 4), q (on line 9)

Globals: q (on line 2)

  Blank.svg Code Output
Down arrow Hexagonal Icon.svg

6
hi,bye,bye,bye,bye,bye,bye
6