# UndefVarError: ncounter

**URL:** https://discourse.julialang.org/t/undefvarerror-ncounter/49867
**Category:** General Usage
**Tags:** question
**Created:** [November 9, 2020, 8:53pm UTC](https://discourse.julialang.org/t/undefvarerror-ncounter/49867 "2020-11-09T20:53:09Z")
**Posts on this page:** 1
**Showing post:** 2

<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: [November 9, 2020, 9:01pm UTC](https://discourse.julialang.org/t/undefvarerror-ncounter/49867/2 "2020-11-09T21:01:46Z")

</div>

This works:

```julia
       wcount = count("banana",'a')
0

julia> function count(word, ltr)
       ncounter = 0;
       for letter in word
         if letter == 'a'
           ncounter = ncounter + 1
         end
       end
       return ncounter

       end

       wcount = count("banana",'a')
3

```

You should have a read through the scope-section of the manual. Essentially the first `ncounter` created a variable local to the scope of the function, wherease the second one created a global one. Thus it did not find the local one you initialised before.

Also have a look at [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757), in particular on how to quote your code in discourse. Have fun learning Julia!

---

_[View the full topic](https://discourse.julialang.org/t/undefvarerror-ncounter/49867)._
