You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/manual/faq.rst
+17-4Lines changed: 17 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -72,9 +72,17 @@ Suppose you call a function like this::
72
72
julia> x # x is unchanged!
73
73
10
74
74
75
-
In Julia, any function (including ``change_value!()``) can't change the binding of a local variable. If ``x`` (in the calling scope) is bound to a immutable object (like a real number), you can't modify the object; likewise, if x is bound in the calling scope to a Dict, you can't change it to be bound to an ASCIIString.
76
-
77
-
But here is a thing you should pay attention to: suppose ``x`` is bound to an Array (or any other mutable type). You cannot "unbind" ``x`` from this Array. But, since an Array is a *mutable* type, you can change its content. For example::
75
+
In Julia, the binding of a variable ``x`` cannot be changed by passing
76
+
``x`` as an argument to a function. When calling ``change_value!(x)``
77
+
in the above example, ``y`` is a newly created variable, bound
78
+
initially to the value of ``x``, i.e. ``10``; then ``y`` is rebound to
79
+
the constant ``17``, while the variable ``x`` of the outer scope is
80
+
left untouched.
81
+
82
+
But here is a thing you should pay attention to: suppose ``x`` is
83
+
bound to an object of type ``Array`` (or any other *mutable* type).
84
+
From within the function, you cannot "unbind" ``x`` from this Array,
85
+
but you can change its content. For example::
78
86
79
87
julia> x = [1,2,3]
80
88
3-element Array{Int64,1}:
@@ -96,7 +104,12 @@ But here is a thing you should pay attention to: suppose ``x`` is bound to an Ar
96
104
2
97
105
3
98
106
99
-
Here we created a function ``change_array!()``, that assigns ``5`` to the first element of the Array. We passed ``x`` (which was previously bound to an Array) to the function. Notice that, after the function call, ``x`` is still bound to the same Array, but the content of that Array changed.
107
+
Here we created a function ``change_array!()``, that assigns ``5`` to
108
+
the first element of the passed array (bound to ``x`` at the call
109
+
site, and bound to ``A`` within the function). Notice that, after the
110
+
function call, ``x`` is still bound to the same array, but the content
111
+
of that array changed: the variables ``A`` and ``x`` were distinct
112
+
bindings refering to the same mutable ``Array`` object.
100
113
101
114
102
115
Can I use ``using`` or ``import`` inside a function?
0 commit comments