# Function with "memory"

**URL:** https://discourse.julialang.org/t/function-with-memory/3391
**Category:** General Usage
**Created:** [April 26, 2017, 7:20pm UTC](https://discourse.julialang.org/t/function-with-memory/3391 "2017-04-26T19:20:02Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![robertdj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertdj/32/103_2.png) [@robertdj](https://discourse.julialang.org/u/robertdj)
#### Post date: [April 26, 2017, 7:20pm UTC](https://discourse.julialang.org/t/function-with-memory/3391/1 "2017-04-26T19:20:02Z")

</div>

I want to make a function with “memory”, that only modifies the second argument if the first satisfies certain conditions and otherwise returns the same second argument. Both arguments should be “scalar”/only one scalar value is of interest.

In the solution below `b` is **not** modified if `0 <= a <= 1`:

```julia
function foo(a, b)
if a < 0
    b[1] = 1
elseif a > 1
    b[1] = 0
end
b
end

b = [2]
foo(0.5, b)
foo(1.5, b)
foo(-0.5, b)

```

However, it seems clumsy to use a vector only to act as a container. Any good suggestions for a better solution?

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [April 26, 2017, 7:26pm UTC](https://discourse.julialang.org/t/function-with-memory/3391/2 "2017-04-26T19:26:06Z")

</div>

It’s unclear what exactly do you want. If you just want a condition, you can just return the modified value. Or if you don’t want to use return value, you’ll have to use a mutable input type to get a “output” argument. You can use any mutable types for it including `Array`, `RefValue` or any custom types.

---

<div class="post-metadata">

### Author: ![robertdj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertdj/32/103_2.png) [@robertdj](https://discourse.julialang.org/u/robertdj)
#### Post date: [April 26, 2017, 7:30pm UTC](https://discourse.julialang.org/t/function-with-memory/3391/3 "2017-04-26T19:30:20Z")

</div>

I’m not familiar with `RefValue` and no results turn up when searching in the docs – do you have a reference?

I actually thought that `Array`s where the only mutable (non-custom) type…?

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [April 26, 2017, 7:34pm UTC](https://discourse.julialang.org/t/function-with-memory/3391/4 "2017-04-26T19:34:40Z")

</div>

Don’t think of “modifying” the argument, just return it:

```julia
function foo(a, b)
    if a < 0
        b = 1
    elseif a > 1
        b = 0
    end

    return b
end

a = 1; b = 2
b = foo(a, b)

```

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [April 26, 2017, 7:36pm UTC](https://discourse.julialang.org/t/function-with-memory/3391/5 "2017-04-26T19:36:40Z")

</div>

There’s no performance gain with trying to do in-place updating of immutable types. Instead, there’s actually a performance loss. Purely using immutables is fastest, it’s just that in many cases this cannot be done (like in cases with array of arbitrary length).

---

<div class="post-metadata">

### Author: ![robertdj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertdj/32/103_2.png) [@robertdj](https://discourse.julialang.org/u/robertdj)
#### Post date: [April 26, 2017, 7:39pm UTC](https://discourse.julialang.org/t/function-with-memory/3391/6 "2017-04-26T19:39:53Z")

</div>

That’s an excellent point – thanks!
