# Indexed variable inside function and for loop

**URL:** <https://discourse.julialang.org/t/indexed-variable-inside-function-and-for-loop/20914>\
**Category:** New to Julia\
**Created:** [February 17, 2019, 9:27pm UTC](https://discourse.julialang.org/t/indexed-variable-inside-function-and-for-loop/20914 "2019-02-17T21:27:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vatfancio](https://avatars.discourse-cdn.com/v4/letter/v/8e7dd6/32.png) [@vatfancio](https://discourse.julialang.org/u/vatfancio)\
**Post date:** [February 17, 2019, 9:27pm UTC](https://discourse.julialang.org/t/indexed-variable-inside-function-and-for-loop/20914/1 "2019-02-17T21:27:17Z")

</div>

Hi everyone. I have a simple doubt about whether is possible to have an index number inside a function in a for loop. The ilustrative example below makes my doubt clearer.

I have an affine function `f(x) = 2x - a` and I want to find the root for distinct values of a. In this case, is it possible to use the `fzero` function inside a loop?

I understand why the error occurs but I do not know how to fix it. I have a similar code in _Fortran_ and I am trying to convert it to Julia.

Thanks a lot!

```julia

a = [1 3 5]

function main(x)
    2*x - a[i]
end # function

guess = 5
for i in 1:3
    y = fzero(main, guess)
    println(y)
end # for

UndefVarError: j not defined

```

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [February 17, 2019, 9:42pm UTC](https://discourse.julialang.org/t/indexed-variable-inside-function-and-for-loop/20914/2 "2019-02-17T21:42:02Z")

</div>

If I understand your problem correctly (please submit fully reproducible code to make it easier for us!), a common way to deal with it in Julia is to have the method take the index, and then bind the index to the function to create a single-parameter function:

```julia
a = [1 3 5]

function main(x, i)
    2*x - a[i]
end

guess = 5
for i in 1:3
    y = fzero(x -> main(x,i), guess)
    println(y)
end

```

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [February 17, 2019, 9:47pm UTC](https://discourse.julialang.org/t/indexed-variable-inside-function-and-for-loop/20914/3 "2019-02-17T21:47:54Z")

</div>

Another possibility is doing the following:

```julia
a = [1 3 5]

function main(x, b)
    2*x - b
end

guess = 5
for i in 1:3
    b = a[i]
    y = fzero(x -> main(x,b), guess)
    println(y)
end

```

This will probably run faster, unless you declare `a` constant with `const a = [1 3 5]` in your original code, then I guess both will have the same speed.

But the key concept you are looking for here is that of [anonymous functions](https://docs.julialang.org/en/v1/manual/functions/#man-anonymous-functions-1).

EDIT: Modifying according to what bennedich pointed out.

---

<div class="post-metadata">

**Author:** ![vatfancio](https://avatars.discourse-cdn.com/v4/letter/v/8e7dd6/32.png) [@vatfancio](https://discourse.julialang.org/u/vatfancio)\
**Post date:** [February 17, 2019, 10:02pm UTC](https://discourse.julialang.org/t/indexed-variable-inside-function-and-for-loop/20914/4 "2019-02-17T22:02:52Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![vatfancio](https://avatars.discourse-cdn.com/v4/letter/v/8e7dd6/32.png) [@vatfancio](https://discourse.julialang.org/u/vatfancio)\
**Post date:** [February 17, 2019, 10:17pm UTC](https://discourse.julialang.org/t/indexed-variable-inside-function-and-for-loop/20914/5 "2019-02-17T22:17:04Z")

</div>

Thanks.

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [February 17, 2019, 10:46pm UTC](https://discourse.julialang.org/t/indexed-variable-inside-function-and-for-loop/20914/6 "2019-02-17T22:46:59Z")

</div>

> [@favba](#):
>
> This will probably run faster

I think it’d actually be the same, since the indexing would still take place for each function call. You’d have to move it outside the function for the speedup to occur:

```julia
for i in 1:3
    b = a[i]
    y = fzero(x -> main(x,b), guess)
    println(y)
end

```
