Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

A-level Computing/AQA/Paper 1/Fundamentals of programming/Global and local variables

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Subroutines (functions and procedures) Stack Frames 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: Module Glocals Dim number1 as integer = 123 Sub Main() console.writeline(number1) printLocalNumber() printGlobalNumber() End Sub Sub printLocalNumber Dim number1 as integer = 234 console.writeline(number1) End Sub Sub printGlobalNumber console.writeline(number1) End Sub End Module What would the output be? 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. Line 5. console.writeline(number1):This uses the global value of number1, as it is inside a sub routine with no other local declarations 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. 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 thumb|center|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 an 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 Exercise: Globals and Local variables What is the difference between a global and a local variable? 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? 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? 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: Module calcAge Sub Main() dim age as integer console.writeline("age?") age = console.readline() printMonths(age) printDays(age) End Sub Dim months as integer Sub printDays(a) Dim d as integer d = a * 365 console.writeline(d) End Sub Sub printMonths(a) months = a * 12 console.writeline(months) End Sub End Module Locals: age, d Globals: months age? 16 192 5840 For the above code how could you make the code more efficient, and why would it be more efficient? 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: Module greetings Dim q as integer = 6 Sub sayGoodbye() for y = 1 to q console.write("bye,") loop End Sub Sub sayHello() dim q as integer = 4 if q =< 4 then console.write("hi,") else console.write("hello,") endif End Sub Sub Main() console.writeline(q) sayHello() sayGoodbye() console.writeline(q) End Sub End Module Locals: y (on line 4), q (on line 9) Globals: q (on line 2) 6 hi,bye,bye,bye,bye,bye,bye 6