2

I want a function to assign a new value to a global variable:

value = ""

function edit_value(v::String)
    value = v
end

However, it does not assign the global value the new value. Julia creates a new local variable value inside the function.

How can I modify the global variable inside a function?

Ken White
  • 123,280
  • 14
  • 225
  • 444
pooooky
  • 490
  • 2
  • 12

1 Answers1

5

You can do that with the keyword global

function edit_value(v::String)
    global value = v
end

Keep in mind that global variables, especially when changed within a function, should be handled with care.

Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29