# The compiler goes in segmentation fault with a fibonacci facuntion call (greater that 1)

**URL:** <https://discourse.julialang.org/t/the-compiler-goes-in-segmentation-fault-with-a-fibonacci-facuntion-call-greater-that-1/43136>\
**Category:** New to Julia\
**Created:** [July 15, 2020, 4:26pm UTC](https://discourse.julialang.org/t/the-compiler-goes-in-segmentation-fault-with-a-fibonacci-facuntion-call-greater-that-1/43136 "2020-07-15T16:26:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![phykos](https://avatars.discourse-cdn.com/v4/letter/p/c67d28/32.png) [@phykos](https://discourse.julialang.org/u/phykos)\
**Post date:** [July 15, 2020, 4:26pm UTC](https://discourse.julialang.org/t/the-compiler-goes-in-segmentation-fault-with-a-fibonacci-facuntion-call-greater-that-1/43136/1 "2020-07-15T16:26:37Z")

</div>

my code is:

```julia
function fib(n::Integer)
    if n <= 1
        return n
    end
    
    return fib(n - 1) + fib(n + 2)
end

for i = 0:40
    println(fib(i))
end

```

and the output is:

```julia
0
1
[2] 5540 segmentation fault (core dumped) julia fib.jl

```

why this?  
it’s a fault of my code or of the compiler?  
am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 15, 2020, 4:29pm UTC](https://discourse.julialang.org/t/the-compiler-goes-in-segmentation-fault-with-a-fibonacci-facuntion-call-greater-that-1/43136/2 "2020-07-15T16:29:04Z")

</div>

You probably meant `fib(n - 2)` (not `n+2`). You wrote an infinite loop.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 15, 2020, 4:29pm UTC](https://discourse.julialang.org/t/the-compiler-goes-in-segmentation-fault-with-a-fibonacci-facuntion-call-greater-that-1/43136/3 "2020-07-15T16:29:35Z")

</div>

> [@phykos](#):
>
> `return fib(n - 1) + fib(n + 2)`

This should be `fib(n - 2)`, not `fib(n + 2)`.

This bug in your code causes infinite recursion, which should lead to a `StackOverFlowError`, not a core dump. That’s what I get on my machine:

```julia
julia> for i = 0:40
           println(fib(i))
       end
0
1
ERROR: StackOverflowError:
Stacktrace:
 [1] fib(::Int64) at ./REPL[1]:6 (repeats 79984 times)

```

What version of Julia are you using?

---

<div class="post-metadata">

**Author:** ![phykos](https://avatars.discourse-cdn.com/v4/letter/p/c67d28/32.png) [@phykos](https://discourse.julialang.org/u/phykos)\
**Post date:** [July 15, 2020, 4:48pm UTC](https://discourse.julialang.org/t/the-compiler-goes-in-segmentation-fault-with-a-fibonacci-facuntion-call-greater-that-1/43136/4 "2020-07-15T16:48:09Z")

</div>

oof, you’re right
