Ints are immutable objects, so you can’t mutate them in any way
Even if xwas mutable (like an array), your method would not mutate it. All you are doing is assigning a new binding inside the function. The outer scope doesn’t know about that.
You might want to use a vector here.
function foo!(x)
x[1] = x[1] + 1
end
x = [3]
foo!(x)
If you want to mutate a global variable in a function, you should declare it to be global, or else all variables in your function will be treated as local variables. This works for your example:
julia> function foo!(x)
global x += 1
end
foo! (generic function with 1 method)
julia> x = 3
3
julia> foo!(x)
4
julia> x
4
Which is not mutating, is assigning the label x to a new value.
Mutability is a property of the value, not of the label. And an Int is not mutable.
Concerning the original question, probably what the OP wants is
x = 1
f(x) = x + 1
# call reassigning x to the return value
x = f(x)
(Although technically correct, I don’t think suggesting to use arrays or refs is actually what will solve the problem, and may lead to very bad initial programming habits and experience with Julia; this may help: Assignment and mutation · JuliaNotes.jl).