# Invalid iteration specification, what does it mean?

**URL:** <https://discourse.julialang.org/t/invalid-iteration-specification-what-does-it-mean/106119>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 12, 2023, 5:04pm UTC](https://discourse.julialang.org/t/invalid-iteration-specification-what-does-it-mean/106119 "2023-11-12T17:04:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pinkpeachman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pinkpeachman/32/203811_2.png) [@pinkpeachman](https://discourse.julialang.org/u/pinkpeachman)\
**Post date:** [November 12, 2023, 5:04pm UTC](https://discourse.julialang.org/t/invalid-iteration-specification-what-does-it-mean/106119/1 "2023-11-12T17:04:46Z")

</div>

I would like to write a function in a .jl file, that can recursively produce polynomials for me. However I got the following error message: invalid iteration specification.

```plaintext
> function P(n)
    Pn(x)=1 ##if n==0 Pn(x)=1
    global i=0 ##declare i for the for loop
    if n!=0
        Pn_1(x)=1
        Pn(x)=x
        seged(x)=x ##declare the function for the for loop
        for global i in 1:n-1
            seged(x)=(((2*i+1)*x*Pn(x)-(i)*Pn_1(x))/(i+1)) ##the formula for the recursive function
            Pn_1(x)= P(x)
            Pn(x)=seged(x)
        end
    end
    return Pn(x)
end

```

Also I have come from Octave and this code run without errors back there.

---

<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 12, 2023, 5:24pm UTC](https://discourse.julialang.org/t/invalid-iteration-specification-what-does-it-mean/106119/2 "2023-11-12T17:24:20Z")

</div>

> [@pinkpeachman](#):
>
> `for global i`

Afaik this is not valid syntax in Julia. Just remove the `global` (why would you need it?)

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [November 12, 2023, 5:29pm UTC](https://discourse.julialang.org/t/invalid-iteration-specification-what-does-it-mean/106119/3 "2023-11-12T17:29:38Z")

</div>

The error is thrown when the text fails to parse to a valid Julia expression.

```julia
julia> :(for global i in iter end)
ERROR: syntax: invalid iteration specification
Stacktrace:
 [1] top-level scope
   @ none:1

```

It’s also redundant, you already had the `global i` declaration earlier. A for loop (or let block) header introduces a new local variable into their scope by default, so if you want to reassign a global variable, you could 1) rename the iteration variable e.g. `for i2 in 1:n-1` and assign `i = i2` in the body, or 2) specify `for outer i in 1:n-1` to borrow `i` from the outer scope.

> [@pinkpeachman](#):
>
> Also I have come from Octave and this code run without errors back there.

Mostly unrelated to the error, but expect more syntax and design differences in totally unrelated languages, this isn’t like Octave cloning MATLAB.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [November 12, 2023, 5:51pm UTC](https://discourse.julialang.org/t/invalid-iteration-specification-what-does-it-mean/106119/4 "2023-11-12T17:51:19Z")

</div>

The closest syntax we have to this is the `outer` keyword. Julia is not a MATLAB or Octave emulator, so you should not expect syntax that works there to just work in Julia.

```julia-repl
julia> i = 0
0

julia> function f()
           global i = 0
           for outer i in 1:10
           end
           return i
       end
f (generic function with 1 method)

julia> f()
10

julia> i
10

```

Our analysis of your code is that you probably do not need the make `i` global or use `outer`. If that is the case, then making it global will result in unnecessarily slow code.
