How do we access a global variable in nested functions?

How do we access a global variable in nested functions?
A. Simply seek the variable from the primary function
B. Make a copy of the global variables from the primary function
C. Declare the variable within the function
D. Declare the variable as global
 
Answer: d
Explanation: Any global variable, in the primary function, if required to be accessed by a nested function- the variable needs to be declared with the global keyword within the function which requires access to it. This allows sharing the variable amongst the primary and other functions. If the variable is declared without using the global keyword, the variable might bring an error to the function.
 

Tìm kiếm

Back
Top