Computer-Science-A-level-Ocr
-
3-3-networks8 主题
-
3-2-databases7 主题
-
3-1-compression-encryption-and-hashing4 主题
-
2-5-object-oriented-languages7 主题
-
2-4-types-of-programming-language4 主题
-
2-3-software-development5 主题
-
2-2-applications-generation6 主题
-
2-1-systems-software8 主题
-
1-3-input-output-and-storage2 主题
-
1-2-types-of-processor3 主题
-
1-1-structure-and-function-of-the-processor1 主题
-
structuring-your-responses3 主题
-
the-exam-papers2 主题
-
8-2-algorithms-for-the-main-data-structures4 主题
-
8-1-algorithms10 主题
-
7-2-computational-methods11 主题
-
7-1-programming-techniques14 主题
-
capturing-selecting-managing-and-exchanging-data
-
entity-relationship-diagrams
-
data-normalisation
-
relational-databases
-
hashing
-
symmetric-vs-asymmetric-encryption
-
run-length-encoding-and-dictionary-coding
-
lossy-and-lossless-compression
-
polymorphism-oop
-
encapsulation-oop
-
inheritance-oop
-
attributes-oop
-
methods-oop
-
objects-oop
-
capturing-selecting-managing-and-exchanging-data
-
6-5-thinking-concurrently2 主题
-
6-4-thinking-logically2 主题
-
6-3-thinking-procedurally3 主题
-
6-2-thinking-ahead1 主题
-
6-1-thinking-abstractly3 主题
-
5-2-moral-and-ethical-issues9 主题
-
5-1-computing-related-legislation4 主题
-
4-3-boolean-algebra5 主题
-
4-2-data-structures10 主题
-
4-1-data-types9 主题
-
3-4-web-technologies16 主题
-
environmental-effects
-
automated-decision-making
-
computers-in-the-workforce
-
layout-colour-paradigms-and-character-sets
-
piracy-and-offensive-communications
-
analysing-personal-information
-
monitoring-behaviour
-
censorship-and-the-internet
-
artificial-intelligence
-
the-regulation-of-investigatory-powers-act-2000
-
the-copyright-design-and-patents-act-1988
-
the-computer-misuse-act-1990
-
the-data-protection-act-1998
-
adder-circuits
-
flip-flop-circuits
-
simplifying-boolean-algebra
-
environmental-effects
global-and-local-variables
Global Variables
What is a global variable?
-
A global variable is a variable declared at the outermost level of a program
-
This means that they are declared outside any modules such as functions or procedures.
-
Global variables have a global scope, which means they can be accessed and modified from any part of the program.
Python example
In this python code, you can see that the globalVariable (with the value 10) is declared outside of the function printValue. This means that this function and any other modules can access and change the value in the global variable.
globalVariable = 10 # Defines a global variable
def printValue(): global globalVariable # Access the global variable inside a function print("The value into the variable is:", globalVariable)
printValue() # Call the function
Usage & need for global variables
-
Data Sharing: Global variables facilitate data sharing between different parts of the program, allowing data to be passed between different modules easily without the use of parameter passing.
-
Persistent Storage: Global variables retain their values throughout the program’s execution, making them suitable for storing data that needs to persist across function calls
-
Global Configuration: Global variables can be used to store configuration settings or constants that are relevant across the entire program
Benefits and drawbacks
|
Benefits |
Drawbacks |
|---|---|
|
The global variable only needs to be declared once. |
The global variables are always stored in memory while the program is running which can use up memory. |
|
You don’t need to keep passing parameters between the different modules. |
Makes the program difficult to maintain as it’s difficult to understand where variables are changed in the program. |
|
|
Makes it difficult to test a single block of code as the programmer will need run the entire program to setup the global variables. |
Local Variables
What is a local variable?
-
A local variable is a variable declared within a specific scope, such as a function or a code block
-
Local variables are accessible only within the block in which they are defined, and their lifetime is limited to that particular block
-
Once the execution of the block ends, the local variable is destroyed, and its memory is released
Python example
In this python code, you can see that the localVariable (with the value 10) is declared inside of the function printValue. This means that only this function can access and change the value in the local variable. It cannot be accessed by other modules in the program.
def printValue(): localVariable = 10 # Defines a local variable inside the function print("The value of the local variable is:", localVariable)
printValue() # Call the function
Benefits and drawbacks
|
Benefits |
Drawbacks |
|---|---|
|
Local variables encapsulate data within a particular function or block, providing data hiding and preventing unintended access from other parts of the program. |
Repeatedly creating and destroying local variables within a loop or recursive function can incur unnecessary memory overhead. |
|
Local variables enable you to use the same variable name in different functions or blocks without causing conflicts, as each local variable is confined to its own scope. |
Excessive use of local variables within deeply nested blocks can lead to code clutter and reduce code readability. |
|
Local variables have a limited lifetime, and their memory is automatically reclaimed when the code block ends, making them memory efficient. |
|
Converting Between Global and Local Variables
-
In this Python code below, a global variable named
global_stringis declared and initialised with the string “Hello” -
Two functions are defined,
add_worldandadd_name -
The
add_worldfunction appends the string “, world!” toglobal_string -
The
add_namefunction takes a stringnameas an argument and appends it toglobal_string -
add_worldis called and printsglobal_string, which results in “Hello, world!” -
After that,
add_nameis called with the argument “Alice” and printsglobal_stringagain, which results in “Hello, world! Alice”
# Global variableglobal_string = "Hello"
def add_world(): global global_string global_string += ", world!"
def add_name(name): global global_string global_string += " " + name
# Call the functionsadd_world()print(global_string) # Outputs: Hello, world!
add_name("Alice")print(global_string) # Outputs: Hello, world! Alice
-
In this revised Python code, a local variable named
local_stringis declared and initialised with the string “Hello” -
Two functions are defined,
add_worldandadd_name -
The
add_worldfunction takes a stringsas an argument, appends the string “, world!” to it and returns the modified string -
The
add_namefunction takes two arguments: a stringsand a stringname. It appendsnametosand returns the modified string -
add_worldis called withlocal_stringas an argument and assigns the result back tolocal_string.local_stringis printed, which resulted in “Hello, world!” -
add_nameis called withlocal_stringand “Alice” as arguments and assigns the result back tolocal_string.local_stringis printed again, which results in “Hello, world! Alice”
def add_world(s): s += ", world!" return s
def add_name(s, name): s += " " + name return s
# Initialize a local variablelocal_string = "Hello"
# Call the functionslocal_string = add_world(local_string)print(local_string) # Outputs: Hello, world!
local_string = add_name(local_string, "Alice")print(local_string) # Outputs: Hello, world! Alice
Responses