[julia-users] Re: Writing a mutable function (exclamation mark function)

2015-06-08 Thread andrew cooke

does this help any?

julia a = 1
1

julia function inc()
a = a+1
   end
inc (generic function with 1 method)

julia inc()
ERROR: UndefVarError: a not defined
 in inc at none:2

julia function inc()
global a = a+1
   end
inc (generic function with 1 method)

julia inc()
2

julia inc()
3

julia inc()
4

julia inc()
5

julia function make_inc(start)
a = start
function inc()
 a = a + 1
end
   end
make_inc (generic function with 1 method)

julia inc2 = make_inc(99)
inc (generic function with 1 method)

julia inc2()
100

julia inc2()
101

julia inc2()
102

also http://julia.readthedocs.org/en/latest/manual/variables-and-scoping/

andrew


On Monday, 8 June 2015 11:34:47 UTC-3, dwo...@gmail.com wrote:

 I'm currently trying to understand how functions with an exclamation mark 
 at the end work. I know the exclamation mark is just a notational point 
 however I'm currently confused at how to actually write a mutable function.
 If a = 1

 function add_one(a)
 return a + 1
 end

 running add_one(a) twice outputs:
 2
 2

 how would I create add_one!(a) to output:
 2
 3



[julia-users] Re: Writing a mutable function (exclamation mark function)

2015-06-08 Thread John Myles White
http://julia.readthedocs.org/en/release-0.3/manual/faq/#i-passed-an-argument-x-to-a-function-modified-it-inside-that-function-but-on-the-outside-the-variable-x-is-still-unchanged-why

http://www.johnmyleswhite.com/notebook/2014/09/06/values-vs-bindings-the-map-is-not-the-territory/

On Monday, June 8, 2015 at 7:34:47 AM UTC-7, dwor...@gmail.com wrote:

 I'm currently trying to understand how functions with an exclamation mark 
 at the end work. I know the exclamation mark is just a notational point 
 however I'm currently confused at how to actually write a mutable function.
 If a = 1

 function add_one(a)
 return a + 1
 end

 running add_one(a) twice outputs:
 2
 2

 how would I create add_one!(a) to output:
 2
 3