Skip to content

Commit 493f913

Browse files
committed
Merge pull request #14693 from rfourquet/faq-binding
Update FAQ: cosmetic changes
2 parents debf467 + 53e97d3 commit 493f913

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

doc/manual/faq.rst

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,34 +59,57 @@ I passed an argument ``x`` to a function, modified it inside that function, but
5959
Suppose you call a function like this::
6060

6161
julia> x = 10
62-
julia> function change_value!(y) # Create a new function
62+
10
63+
64+
julia> function change_value!(y)
6365
y = 17
6466
end
67+
change_value! (generic function with 1 method)
68+
6569
julia> change_value!(x)
70+
17
71+
6672
julia> x # x is unchanged!
6773
10
6874

69-
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.
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.
7081

71-
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::
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::
7286

7387
julia> x = [1,2,3]
7488
3-element Array{Int64,1}:
7589
1
7690
2
7791
3
7892

79-
julia> function change_array!(A) # Create a new function
93+
julia> function change_array!(A)
8094
A[1] = 5
8195
end
96+
change_array! (generic function with 1 method)
97+
8298
julia> change_array!(x)
99+
5
100+
83101
julia> x
84102
3-element Array{Int64,1}:
85103
5
86104
2
87105
3
88106

89-
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.
90113

91114

92115
Can I use ``using`` or ``import`` inside a function?

0 commit comments

Comments
 (0)