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
+28-5Lines changed: 28 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -59,34 +59,57 @@ I passed an argument ``x`` to a function, modified it inside that function, but
59
59
Suppose you call a function like this::
60
60
61
61
julia> x = 10
62
-
julia> function change_value!(y) # Create a new function
62
+
10
63
+
64
+
julia> function change_value!(y)
63
65
y = 17
64
66
end
67
+
change_value! (generic function with 1 method)
68
+
65
69
julia> change_value!(x)
70
+
17
71
+
66
72
julia> x # x is unchanged!
67
73
10
68
74
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.
70
81
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::
72
86
73
87
julia> x = [1,2,3]
74
88
3-element Array{Int64,1}:
75
89
1
76
90
2
77
91
3
78
92
79
-
julia> function change_array!(A) # Create a new function
93
+
julia> function change_array!(A)
80
94
A[1] = 5
81
95
end
96
+
change_array! (generic function with 1 method)
97
+
82
98
julia> change_array!(x)
99
+
5
100
+
83
101
julia> x
84
102
3-element Array{Int64,1}:
85
103
5
86
104
2
87
105
3
88
106
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.
90
113
91
114
92
115
Can I use ``using`` or ``import`` inside a function?
0 commit comments