Local and global variables
A variable declared (created) inside a function is called a local variable, and it can only be accessed from within the function. Outside the function, it is as if the variable never existed at all. Check the following code:
function my_function() name = "Lisa" end_function my_function() print name
Here, we create and assign a value to the name
variable inside the my_function
function. Outside the function, we first call the function, and then we try to print the name. The program will crash with an error on the line where we try to print the name. The reason is that the name
variable is unknown in this part of the program. It is only valid as long as we execute code inside the function.
This is a local variable. It is local as it is created inside the function.
If we instead change the program so it looks like this, things will be different:
name = "Bill" function my_function() ...