# A particular function that modifies the input

**URL:** https://discourse.julialang.org/t/a-particular-function-that-modifies-the-input/68387
**Category:** General Usage
**Tags:** question
**Created:** [September 19, 2021, 12:12am UTC](https://discourse.julialang.org/t/a-particular-function-that-modifies-the-input/68387 "2021-09-19T00:12:54Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![maoc](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@maoc](https://discourse.julialang.org/u/maoc)
#### Post date: [September 19, 2021, 12:12am UTC](https://discourse.julialang.org/t/a-particular-function-that-modifies-the-input/68387/1 "2021-09-19T00:12:54Z")

</div>

I think I’m going to learn a lot whith this question.

First, my principal goal is to transform any matrix according the following rule:  
For each column, find the first element equal to `1.1`. Suposse the position of this elememt is `[n,j]`. So, replaces all elemnts `[n:end,j]` with `[n-1,j]`.  
My scripts for this is the following:

```julia
function tabela_taxas(tab)
    for j in 1:size(tab)[2]
        aux = tab[:,j]
        i = 1
        while aux[i] != 1.1
            i = i+1
        end
        element_aux = aux[i-1]
        tab[i:end,j] .= element_aux
    end
    return tab
end

```

My first question, but not the most important, is that I think there is other way (more elegant) to writte the `tabela_taxas` function.

The second question is related to something strange. First, I will run the `tabela_taxas` function. For this, I’m going to simulate a specific matrix that allows me to test my function

```julia
tabela = rand(6,5)
tabela[3,1] = 1.1
tabela[2,2] = 1.1
tabela[5,3] = 1.1
tabela[2,4] = 1.1
tabela[2,5] = 1.1

```

Now, using my function:

```julia
tabela2 = tabela_taxas(tabela)

```

You can see that the original matrix, tabela, has been modified. Which shouldn’t happen because the matrix tabela is an input and not an output. It’s as if I had done:

```julia
tabela = tabela_taxas(tabela)

```

Can someone explain?

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [September 19, 2021, 12:32am UTC](https://discourse.julialang.org/t/a-particular-function-that-modifies-the-input/68387/2 "2021-09-19T00:32:22Z")

</div>

> [@maoc](#):
>
> You can see that the original matrix, tabela, has been modified. Which shouldn’t happen because the matrix tabela is an input and not an output

The input matrix is the output matrix – notice how your functions modifies and returns the input `tab`, without reassigning to it. If you don’t want to do this, you can add `tab = copy(tab)` to your function before modifying `tab`.  
You can confirm that they’re the same matrix via `tabela === tabela2`.

For example:

```julia
function tabela_taxas!(tab) # mutating definition
    for j in axes(tab, 2)
        aux = @view(tab[:,j])
        i = findfirst(==(1.1), aux)
        i === nothing || (aux[i:end] .= aux[i-1])
    end
    return tab
end

# non-mutating
tabela_taxas(tab) = tabela_taxas!(copy(tab))

```

---

<div class="post-metadata">

### Author: ![chunjiw](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@chunjiw](https://discourse.julialang.org/u/chunjiw)
#### Post date: [September 19, 2021, 12:39am UTC](https://discourse.julialang.org/t/a-particular-function-that-modifies-the-input/68387/3 "2021-09-19T00:39:13Z")

</div>

In Julia [docs for array](https://docs.julialang.org/en/v1/manual/arrays/):

> In Julia, all arguments to functions are [passed by sharing](https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing) (i.e. by pointers).

You can read the Wikipedia section to understand passing by sharing. Happy learning!

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [September 19, 2021, 5:32am UTC](https://discourse.julialang.org/t/a-particular-function-that-modifies-the-input/68387/4 "2021-09-19T05:32:26Z")

</div>

A pretty common pattern is to write two functions:

```julia
myfun(x) = myfun!(copy(x))

function myfun!(x) 
    x[3] = 5 # or some other mutating code
    return x
end

```

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [September 19, 2021, 5:55pm UTC](https://discourse.julialang.org/t/a-particular-function-that-modifies-the-input/68387/5 "2021-09-19T17:55:25Z")

</div>

> [@maoc](#):
>
> My first question, but not the most important, is that I think there is other way (more elegant) to writte the `tabela_taxas` function.

I’m not sure this is elegant, and won’t be as fast as @Elrod’s one above, but you can write many such things with `accumulate`:

```julia
julia> tabela
6×5 Matrix{Float64}:
 0.0653491 0.982055 0.419218 0.242059 0.168234
 0.149178 1.1 0.398028 1.1 1.1
 1.1 0.190122 0.678386 0.854605 0.0642094
 0.763535 0.433054 0.278625 0.433437 0.231305
 0.0410386 0.528489 1.1 0.229503 0.779562
 0.757617 0.383551 0.806496 0.151491 0.782904

julia> accumulate(tabela; dims=1, init=(false, NaN)) do (flag,val),x
        (flag || x==1.1) ? (true,val) : (false,x)
       end .|> last
6×5 Matrix{Float64}:
 0.0653491 0.982055 0.419218 0.242059 0.168234
 0.149178 0.982055 0.398028 0.242059 0.168234
 0.149178 0.982055 0.678386 0.242059 0.168234
 0.149178 0.982055 0.278625 0.242059 0.168234
 0.149178 0.982055 0.278625 0.242059 0.168234
 0.149178 0.982055 0.278625 0.242059 0.168234

```
