global
keyword. However, its use is typically discouraged, explaining why we won't cover it.The following code illustrates this behavior by redefining a function argument and a global variable. The output reflects that foo
in each example treats the redefined x
as a new local variable, only existing within foo
's scope.
x = 2
function foo(x)
x = 3
end
x
foo(x)
x
#functions can't redefine variables globally, only mutate themx = [1,2]
function foo()
x = [0,0]
end
x
foo()
x
#functions can't redefine variables globally, only mutate them