# Simple fibonacci function

**URL:** <https://discourse.julialang.org/t/simple-fibonacci-function/24368>\
**Category:** New to Julia\
**Created:** [May 19, 2019, 1:46am UTC](https://discourse.julialang.org/t/simple-fibonacci-function/24368 "2019-05-19T01:46:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![samusz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samusz/32/7972_2.png) [@samusz](https://discourse.julialang.org/u/samusz)\
**Post date:** [May 19, 2019, 1:46am UTC](https://discourse.julialang.org/t/simple-fibonacci-function/24368/1 "2019-05-19T01:46:59Z")

</div>

```julia
julia> function fib(n::Int)
       n<0 && error("n must be non negative")
       n==1 && return 1
       fib(n-1) + fib(n-2)
       end
fib (generic function with 1 method)

julia> fib(2)
ERROR: n must be non negative
Stacktrace:
 [1] error(::String) at .\error.jl:33
 [2] fib(::Int64) at .\REPL[13]:2
 [3] fib(::Int64) at .\REPL[13]:4 (repeats 2 times)
 [4] top-level scope at none:0

```

I don’t get it !  
What am I missing ?

Thanks !

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [May 19, 2019, 1:49am UTC](https://discourse.julialang.org/t/simple-fibonacci-function/24368/2 "2019-05-19T01:49:40Z")

</div>

You are missing the basecase for `n==0`.

```julia
n = 2
fib(n-2 =0 )
fib(0-1) 

```

```julia
function fib(n::Int)
              n<0 && error("n must be non negative")
              n==0 && return 0
              n==1 && return 1
              fib(n-1) + fib(n-2)
              end

```

---

<div class="post-metadata">

**Author:** ![samusz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samusz/32/7972_2.png) [@samusz](https://discourse.julialang.org/u/samusz)\
**Post date:** [May 19, 2019, 6:22am UTC](https://discourse.julialang.org/t/simple-fibonacci-function/24368/3 "2019-05-19T06:22:59Z")

</div>

Thanks !  
Still, I find it weird the error() message was triggered.  
This got me thinking my coding of this line was wrong and that it was acting as \< or = :  
I had  
julia\> fib(0)  
ERROR: n must be non negative  
Stacktrace:  
[1] error(::String) at .\error.jl:33  
[2] fib(::Int64) at .\REPL[37]:2  
[3] fib(::Int64) at .\REPL[37]:4  
[4] top-level scope at none:0

julia\>

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 19, 2019, 6:32am UTC](https://discourse.julialang.org/t/simple-fibonacci-function/24368/4 "2019-05-19T06:32:52Z")

</div>

f(0) goes to line three and calls f(-1)

---

<div class="post-metadata">

**Author:** ![samusz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samusz/32/7972_2.png) [@samusz](https://discourse.julialang.org/u/samusz)\
**Post date:** [May 19, 2019, 11:37am UTC](https://discourse.julialang.org/t/simple-fibonacci-function/24368/5 "2019-05-19T11:37:00Z")

</div>

Yes, I get that.  
I meat to say that what confused me was reading the stack trace :

using the error() made the “n must be non negative” show up in the stack trace  
and stop the infinite loop …

With

```julia
function fib(n::Int)
      n<0 && println("n must be non negative")
      n==1 && return 1
      fib(n-1) + fib(n-2)
      end

```

it would have been much clearer as the infinite loop shows up.  
I didn’t knew error() would hide this problem.

So should I got back to using println() then and only use when I am sure this kind of issue doesn’t happen ?

I meat to improve my code and it came back at me 😅

_Note for other newb to prog/Julia_: lI got the error() idea form the Julia [manual short circuit evaluation](https://docs.julialang.org/en/v1/manual/control-flow/#Short-Circuit-Evaluation-1) part

```julia
function fact(n::Int)
           n >= 0 || error("n must be non-negative")
           n == 0 && return 1
           n * fact(n-1)
       end

```

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [May 20, 2019, 11:33pm UTC](https://discourse.julialang.org/t/simple-fibonacci-function/24368/6 "2019-05-20T23:33:06Z")

</div>

`error` throws an `ErrorException`. If you only wanted to log you can use `@error`
