Junior — Middle
Can you explain what is meant by a local variable in programming?
sobes.tech AI
Answer from AI
A local variable is a variable declared inside a function or a block of code, and is only accessible within that block. It is created when entering the function and destroyed when exiting it, which helps avoid name conflicts and keeps data only during the function's execution.
Example in Python:
def example():
x = 10 # local variable
print(x)
example()
# print(x) # will cause an error because x is not accessible outside the function