# Question: Memory allocation in function

**URL:** <https://discourse.julialang.org/t/question-memory-allocation-in-function/18562>\
**Category:** New to Julia\
**Created:** [December 11, 2018, 3:54pm UTC](https://discourse.julialang.org/t/question-memory-allocation-in-function/18562 "2018-12-11T15:54:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![timmm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timmm/32/6760_2.png) [@timmm](https://discourse.julialang.org/u/timmm)\
**Post date:** [December 11, 2018, 3:54pm UTC](https://discourse.julialang.org/t/question-memory-allocation-in-function/18562/1 "2018-12-11T15:54:42Z")

</div>

Hi all,

I just want to check I have understood this right.

Example:

```julia
function Foo!(X)
X=X./2
return(X)
end

function TestFooAllocs()
A=[1 2 3];
@time A=Foo!(A);
end

```

Output:

```julia
0.000002 seconds (1 allocation: 112 bytes)
1×3 Array{Float64,2}:
0.5 1.0 1.5

```

In this example the single allocation is when I create A? I.e. when I manipulate this inside the function ‘foo’ is this assigned a new block of memory or not?

Hope my question makes sense.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [December 11, 2018, 4:05pm UTC](https://discourse.julialang.org/t/question-memory-allocation-in-function/18562/2 "2018-12-11T16:05:11Z")

</div>

`X./2` creates a new array, which is then bound to `X`. You can modify the original array with `X.=X./2`.

---

<div class="post-metadata">

**Author:** ![timmm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timmm/32/6760_2.png) [@timmm](https://discourse.julialang.org/u/timmm)\
**Post date:** [December 11, 2018, 4:12pm UTC](https://discourse.julialang.org/t/question-memory-allocation-in-function/18562/3 "2018-12-11T16:12:24Z")

</div>

Nice, working REPL example below.

```julia
function Foo!(X)
X.=X./2
return(X)
end

function FooLoop!(X)
for i=1:length(X)
	X[i]= X[i]/2
end
return(X)
end

function TestFooAllocs()
A=[1. 2. 3.];
@time A=Foo!(A);
end

function TestFooLoopAllocs()
A=[1. 2. 3.];
@time A=FooLoop!(A);
end

TestFooAllocs()
TestFooLoopAllocs()

```

REPL outputs:

```julia
0.000000 seconds
1×3 Array{Float64,2}:
0.5 1.0 1.5

0.000000 seconds
1×3 Array{Float64,2}:
0.5 1.0 1.5

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [December 11, 2018, 4:33pm UTC](https://discourse.julialang.org/t/question-memory-allocation-in-function/18562/4 "2018-12-11T16:33:11Z")

</div>

PS: also have a look at StaticArrays.jl. For small arrays those often work really well without requiring the gymnastics of in-place modifications.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 11, 2018, 5:56pm UTC](https://discourse.julialang.org/t/question-memory-allocation-in-function/18562/5 "2018-12-11T17:56:36Z")

</div>

> [@timmm](#):
>
> `return(X)`

It is not idiomatic to treat `return` as a function, since it is a keyword. You should rather write

```julia
return X

```

For benchmarking I recommend to use BenchmarkTools.jl, that way you also get more accurate memory numbers.
