Update array

Try either

 function add!(list, a)
    list .= list .+a
   return nothing
 end

or

 function add!(list, a)
    list .+= a
   return nothing
 end

Here’s a demo:

julia>  function add!(list, a)
           list .= list .+a
          return nothing
        end
add! (generic function with 2 methods)

julia> add!(Val(1), view(list,:), a)

julia> list
2-element Vector{Int64}:
 2
 3

The reason your version does not work is that list .+ a creates a new array, and this array is bound the local variable list, abandoning the array previous bound to list. However, the new list is not returned.

My version updates each element of the original array list by using .= or .+=. I think this is what you want based on the use the exclamation mark.

1 Like