# Julia's assignment behavior differs from Fortran?

**URL:** https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389
**Category:** General Usage
**Tags:** fortran
**Created:** [November 18, 2020, 1:24pm UTC](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389 "2020-11-18T13:24:15Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [November 18, 2020, 1:24pm UTC](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389/1 "2020-11-18T13:24:15Z")

</div>

When I was first starting I was horrified by this, when trying to literally translate Fortran to Julia:

```julia
julia> function f()
          local x = Vector{Float64}(undef,3)
          local y = Vector{Float64}(undef,3)
          y[1] = 1.0
          x = y
          y[1] = 2.0
          println(x[1])
       end
f (generic function with 1 method)

julia> f()
2.0

```

Now I am used to it, and see that that is a price to pay to have a dynamically typed language. The fact that `x` is not anymore the vector I defined at start was surprising and, of course, a source of errors that Fortran does not have. There are trade-offs.

edit: In fortran this returns “1.0”:

```fortran
program main
  implicit none
  double precision :: x(3), y(3)
  y(1) = 1.
  x = y
  y(1) = 2. 
  write(*,*) x(1)
end program main

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [November 18, 2020, 1:27pm UTC](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389/2 "2020-11-18T13:27:10Z")

</div>

> [@Julia's loop scope behavior differs from Fortran?](https://discourse.julialang.org/t/julias-loop-scope-behavior-differs-from-fortran/50275/30):
>
> the entire “scope” setup of variables in Julia is a horror, plain and simple.

Please consider the possibility that it is just unfamiliar, and after a few months or years of programming, you may appreciate Julia’s scoping rules as very well-designed.

> [@Julia's loop scope behavior differs from Fortran?](https://discourse.julialang.org/t/julias-loop-scope-behavior-differs-from-fortran/50275/30):
>
> just writing a simple program with a detailed control flow leads constantly to errors and spurious results

It is hard to say more without a specific example, but you may just be fighting the language here by not writing idiomatic code. Again, just give it time.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [November 18, 2020, 4:29pm UTC](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389/3 "2020-11-18T16:29:47Z")

</div>

6 posts were merged into an existing topic: [Julia’s loop scope behavior differs from Fortran?](https://discourse.julialang.org/t/julias-loop-scope-behavior-differs-from-fortran/50275/35)

---

<div class="post-metadata">

### Author: ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)
#### Post date: [November 18, 2020, 2:40pm UTC](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389/9 "2020-11-18T14:40:03Z")

</div>

> [@lmiq](#):
>
> When I was first starting I was horrified by this, when trying to literally translate Fortran to Julia:
> 
> ```julia
> julia> function f()
> local x = Vector{Float64}(undef,3)
> local y = Vector{Float64}(undef,3)
> y[1] = 1.0
> x = y
> y[1] = 2.0
> println(x[1])
> end
> f (generic function with 1 method)
> 
> julia> f()
> 
> ```

LeandroM, I’m flabbergasted that this program you wrote produces that result. It’s like there’s no sense of causality. So `x = y` creates some kind of life-long bond between the variables (within the function), so that if 500 lines later, you update the value of `y`, then the value of `x` gets silently updated too?? I can’t believe that.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [November 18, 2020, 2:44pm UTC](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389/10 "2020-11-18T14:44:06Z")

</div>

Told ya. 🙂

That is how things work in every dynamically typed language. `x = y ` means that the name `x` is bound now to the value that was associated with the name `y`. The “previous” `x` is lost forever. If you want that the elements of `x `are associated with the values that are assigned to the elements of `y`, you need to say that explicitly:

```julia
for i in 1:length(x)
  x[i] = y[i]
end

```

(and there are compact forms of expressing that, as `x .= y`).

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [November 18, 2020, 2:47pm UTC](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389/11 "2020-11-18T14:47:22Z")

</div>

Assignment is naming. That’s it. Just remember that and you’ll be in good shape. When you say `x = Vector{Float64}(undef, 3)`, you’re saying to Julia that you want to use the name `x` for that undef vector.

When you later say `x = y` you’re saying to Julia that you have a better use for the name `x` — it’ll now be a name for whatever `y` was at that point. You could later decide you have a better use for the name `y` and it’ll not change `x`.

When you say `x[1] = 1.0` you’re doing indexed assignment — which is wholly distinct from naming. It’s changing the first value of whatever `x` means.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [November 18, 2020, 2:48pm UTC](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389/12 "2020-11-18T14:48:49Z")

</div>

> [@CosmoProf](#):
>
> so that if 500 lines later, you update the value of `y` , then the value of `x` gets silently updated too?? I can’t believe that.

This is exactly what would have happened with the equivalent Python, Java, or C code (for the old pointer arrays).

> [@CosmoProf](#):
>
> LeandroM, I’m flabbergasted that this program you wrote produces that result. It’s like there’s no sense of causality. So `x = y` creates some kind of life-long bond between the variables (within the function),

You notice you say there is no link of causality and then you are able to find the link immediately, no?

Please see my post why [_you should look at variables as labels not boxes_](https://discourse.julialang.org/t/is-it-appropriate-to-say-a-variable-is-a-label-or-box/46068/19).

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [November 18, 2020, 2:49pm UTC](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389/13 "2020-11-18T14:49:50Z")

</div>

Not just dynamically typed languages, it’s the same thing in C and C++ and Java and C# and Scala, etc. If you have `double *x, *y;` and later you do `x = y` what happens? It causes `x` to point to the same memory as `y`. If you modify that memory it is visible through both references (because it’s the same memory). Fortran is very much the odd man out here in that `x = y` does more than change what object `x` points at, it implicitly does a massive copy. The vast majority of languages do not have that kind of semantics for assignment of arrays. Matlab and R do have copy semantics for array assignment (but not other kinds of objects), probably because they were influenced by Fortran, but they are unusual in this.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [November 18, 2020, 2:55pm UTC](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389/14 "2020-11-18T14:55:07Z")

</div>

To be fair, the C++ equivalent would probably be `Vector` which does copy the array. C++ and their possibility of redefining how attribution works for each different class also seems like an odd duck.

---

<div class="post-metadata">

### Author: ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)
#### Post date: [November 18, 2020, 3:00pm UTC](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389/15 "2020-11-18T15:00:32Z")

</div>

So, what should I expect this function to do?

```julia
function abc()
    xOld = 1
    xNew = xOld + 1
    xOld = xNew
    println("xOld = ",xOld,", xNew = ",xNew)
    return
end

```

By the previous logic, `xNew` is bound to `xOld+1`. But then `xOld` is bound to `xNew`, so that means `xOld` must equal `xOld+1`. So either there’s a contradiction, or `xOld` and `xNew` get ratcheted up to infinity together.

But if you run the program, you just get xOld = xNew = 2, which is what an old Fortran programmer would have expected. So I’m confused.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [November 18, 2020, 3:03pm UTC](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389/16 "2020-11-18T15:03:24Z")

</div>

You can try it and see. Note that this example is different as there is no mutation of any objects, only assignment. The array example involves mutation of arrays.

---

<div class="post-metadata">

### Author: ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)
#### Post date: [November 18, 2020, 3:09pm UTC](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389/17 "2020-11-18T15:09:19Z")

</div>

I’m afraid I don’t understand the difference how `x=y` is mutation but `xOld=xNew` is assignment. Does the equal (`=`) command mean something different for arrays than for numbers?

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [November 18, 2020, 3:09pm UTC](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389/18 "2020-11-18T15:09:29Z")

</div>

Numbers always “copy” in julia. There is no concept of mutating a number because numbers are not mutable structs.

---

<div class="post-metadata">

### Author: ![WschW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wschw/32/6575_2.png) [@WschW](https://discourse.julialang.org/u/WschW)
#### Post date: [November 18, 2020, 3:10pm UTC](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389/20 "2020-11-18T15:10:24Z")

</div>

Think more along the lines of eager evaluation then lazy evaluation. The expression on the right hand side is evaluated before it is assigned to the label on the left hand side.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [November 18, 2020, 3:11pm UTC](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389/21 "2020-11-18T15:11:09Z")

</div>

There’s never any mutation with a plain `x=y` thing. It’s just naming and values. Names on the left, values on the right.

Just like how we use natural languages (like English), a particular thing can have no names, one name, or many names. The names you use for a particular thing can change over time and can be different depending upon which room you’re in (scope).

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [November 18, 2020, 3:11pm UTC](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389/22 "2020-11-18T15:11:42Z")

</div>

You are still thinking of variables as boxes, please look at [the post I linked above](https://discourse.julialang.org/t/is-it-appropriate-to-say-a-variable-is-a-label-or-box/46068/19).

> [@CosmoProf](#):
>
> By the previous logic, `xNew` is bound to `xOld+1` .

No. `xNew` is bound to the value resulting from `xOld+1` which is `2`. `xNew` is not a closure which will always recompute its value when the variables used to define it are changed. What happened previously happened because you were dealing with a `mutable` object (a `Vector`/`Array`). You used two variables/labels to refer to the same object, and you changed _inside_ the mutable object. So it reflected in the two variables because they are just names for the same object that now is different than it previously was.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [November 18, 2020, 3:17pm UTC](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389/23 "2020-11-18T15:17:28Z")

</div>

They’re the same — they’re both assignment. It’s the `y[1] = 1.0` and `y[1] = 2.0` bit that’s mutation. Without that, arrays and scalars behave the same. Example:

```julia
function example_no_mut_scalar()
    xOld = 1
    xNew = 2
    xOld = xNew
    println("xOld = ",xOld,", xNew = ",xNew)
    return
end

function example_no_mut_array()
    xOld = [1]
    xNew = [2]
    xOld = xNew
    println("xOld = ",xOld,", xNew = ",xNew)
    return
end

```

They work the same way:

```julia
julia> example_no_mut_scalar()
xOld = 2, xNew = 2

julia> example_no_mut_array()
xOld = [2], xNew = [2]

```

The example you were surprised by involves mutation of an array, which cannot be done in the scalar case because integers are not mutable.

```julia
function example_mut_scalar()
    xOld = 1
    xNew = 2
    xOld = xNew
    xNew[1] = 1 # not actually possible
    println("xOld = ",xOld,", xNew = ",xNew)
    return
end

function example_mut_array()
    xOld = [1]
    xNew = [2]
    xOld = xNew
    xNew[1] = 1
    println("xOld = ",xOld,", xNew = ",xNew)
    return
end

```

These behave differently, but only in that mutating a scalar is an error:

```julia
julia> example_mut_scalar()
ERROR: MethodError: no method matching setindex!(::Int64, ::Int64, ::Int64)

julia> example_mut_array()
xOld = [1], xNew = [1]

```

Bottom line: no matter what kind of object you’re dealing with when you do `x = y` then `x` points at the same object as `y`.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [November 18, 2020, 3:18pm UTC](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389/24 "2020-11-18T15:18:23Z")

</div>

> [@CosmoProf](#):
>
> Does the equal ( `=` ) command mean something different for arrays than for numbers?

Yes and no. As @StefanKarpinski pointed out, it is not the type that matters, but if it is `x = y` or `x[i] = y`. `x = y` is defined by the language it (creates a new name `x` if there was not one and) just binds the value to that name in that scope. `x[1] = 2` is a completely different animal, it lowered to `(setindex!(x, 2, 1); 2)` (i.e., call the function `setindex!` for the appropriate type of `x` and return the value passed to it, in this example it is `2`), you can see more typing `?setindex!` in the Julia REPL.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [November 18, 2020, 3:20pm UTC](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389/25 "2020-11-18T15:20:34Z")

</div>

To clarify, `x = y` means the same thing for all values regardless of their kind. However `x = y` and `x[i] = y` are totally different.

---

<div class="post-metadata">

### Author: ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)
#### Post date: [November 18, 2020, 3:21pm UTC](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389/26 "2020-11-18T15:21:53Z")

</div>

I appreciate all of these detailed explanations, guys, but I’m still not confident that I know what’s going to happen in a given program. I think I’m going to have to program, print out, and pray.

[Next page](https://discourse.julialang.org/t/julias-assignment-behavior-differs-from-fortran/50389.md?page=2)
