Global variable wrapped in a function - scope

I'm learning Julia as part of my PhD in economics but I've come across an issue that doesn't make sense to me. I'm trying to write a function that performs some preliminary calculations then conducts a while loop and returns some value. I want to do this without using global variables. For some reason I can't get it to work. See the minimal working example below which returns a undefined variable error for z.
function test_me(n)
x = 2 + 1
y = x - 1
i = y
while i <= n
println(i)
i += 1
z = 3*i
end
return z
end
I can solve the problem easily by making z a global variable.
function test_me2(n)
x = 2 + 1
y = x - 1
i = y
while i <= n
println(i)
i += 1
global z = 3*i
end
return z
end
I'm confused as I was under the impression that by wrapping the while loop in a function implies that z is in the local scope and the global declaration is unnecessary. For example the code below works as expected.
function test_me3(n)
i = 1
while i <= n
println(i)
i += 1
z = 3*i
end
return z
end
I apologise if this issue is trivial. Any help is really appreciated. Thanks.

Just put a local z or alternatively a z = 0 before your while loop such that z is defined in the loop.
For more on this check out the scoping page of the Julia documentation and the local keyword docstring.
See also this question/answer: In Julia, is there a way to pass a variable from a local scope to the enclosing local scope?

Related

Why this following code prints x values as 1 and y values as 10

var y;
function modify(x) {
var z = 5;
x += 2;
y += x + z
}
var x = 1,
y = 2,
z = 3
modify(x)
Above snippet result is x=1,y=10,z=3 Please explain this code. Thanks in advance
In the modify function, "x" is in the scope of modify, and is not going to use the x in the global scope. In addition, primitive types, such as numbers, are passed by value instead of reference. Therefore, x is always going to remain 1.
Also in the modify function, you are declaring a new variable of z in the scope of the modify function, so just like x, z is going to remain as 3.
Since y isn't declared in the function, it's going to use the global scope, so that's the only one that's going to change. In this case it's 2 + ((1 + 2) + 5), which is how you get 10.

A small question about the change() function,how to understand the process about it?

I am learning the basic usage of python,and I'm confusing about how the variable runs in a practice question. Here are the code below:
x = 1
def change(a):
a = x + 1
print(x)
change(x)
x = 1
def change(a)
x = x + 1
print(x)
change(x)
This is how I think the process:
in the first code:change(x) means: x = x + 1 - print (x) - output:2
but in fact the result is 1.So the real process is: x(symbol in the function) = x(global variable) + 1, print(x), this x is the global variable.
is that right?
in the second code,I think still the output should be 2,but it shows me that UnboundLocalError: local variable 'x' referenced before assignment
so in the python,we can't use function to change the global variable?
Inside a function, you can read global variables but you cannot modify them without explicitly declaring them as global like this:
x = 1
def change(a):
global x # <- this is required since you're assigning a new value to x
x = x + 1
print(x)
change(x)
In the first case, with a = x + 1, the global declaration is not required since you're only reading the value of x, not modifying it. Also, the output is 1 in the first case, since you're printing x not a.

Julia UndefVarError

for i in 1:2
if i == 2
print(x)
end
if i == 1
x = 0
end
end
UndefVarError : x not defined
Why does the code gives that error instead of printing 0 in julia?
While in python the following code print 0?
for i in range(2):
if i==1:
print(x)
if i==0:
x=0
The reason is because in the loop a variable gets a new binding each time a loop is executed, see https://docs.julialang.org/en/latest/manual/variables-and-scoping/#For-Loops-and-Comprehensions-1.
In fact while loop changed this behavior between Julia 0.6.3 and Julia 0.7 (in Julia 0.6.3 a new binding was not created). Therefore the following code:
function f()
i=0
while i < 2
i+=1
if i == 2
print(x)
end
if i == 1
x = 0
end
end
end
Gives the following output.
Julia 0.6.3
julia> function f()
i=0
while i < 2
i+=1
if i == 2
print(x)
end
if i == 1
x = 0
end
end
end
f (generic function with 1 method)
julia> f()
0
Julia 0.7.0
julia> function f()
i=0
while i < 2
i+=1
if i == 2
print(x)
end
if i == 1
x = 0
end
end
end
f (generic function with 1 method)
julia> f()
ERROR: UndefVarError: x not defined
Stacktrace:
[1] f() at .\REPL[2]:6
[2] top-level scope
For-loop created a new binding already in Julia 0.6.3 at each iteration so it fails both under Julia 0.6.3 and Julia 0.7.0.
EDIT: I have wrapped the examples in a function, but you would get the same result if you executed the while loop in global scope.
Ignore my comment andgo with Bogumil's answer as that's is the real rwason why your x variable disapears in thesecond iteration.
If you want your code to work like in Python, you can add the global keyword to your assignment of x:
for i in 1:2
if i == 2
print(x)
end
if i == 1
global x = 0
end
end
Note that this is not recommended in most cases as it'll make your code performance suffer. Julia likes local variables that the compiler can optimise away easily.

Haskell - How to modify existing function values

i have a basic question regarding Haskell that boggles my mind since i am new to functional programming.
i've got simple functions for example
foo 1 1 = 0
foo 1 2 = 1
foo 2 1 = 1
foo 2 2 = 0
and i want to change the function values depending on a condition via another function (for example from 1 to 0, if the value is 1). How can i do that? I'm comming from python and am somehow stuck in the way of thought that i can simply assign the new value in the function body.
im trying something along this lines:
changeValue x y
|(foo x y == 1) = foo x y = 0
A little hint would be appreciated, since it feels like a simple question that i just can't find a solution for. Thanks!
Maybe having a look at http://learnyouahaskell.com/syntax-in-functions helps? Think of haskell functions as mathematical functions, there's no assignment there either.
Anyway, you can ask an other function for a value to compare to, and e.g. in that case return 1:
foo 0 1 = 1
foo x y
| otherfunction x y == 7 = 1
| otherwise = 0

How to use Global Variable in Python

I'm trying to write a simple code that uses a global variable. I'm getting the following error
UnboundLocalError: local variable 'x' referenced before assignment
global x
def update():
x = x + 1
x = 0
update()
print(x)
Your error occurred because in the function update, you are trying to edit a variable (x) that is not defined, at least not locally.
The global keyword should be inside the function, and hence tell that the x you are speaking about is the one defined outside of the function (therefore globally defined) :
def update():
global x
x = x + 1
x = 0
update()
print(x)
This would output 1, as expected.
You can take a look at this well detailed answer regarding the use of the global keyword.

Resources