# 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:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![panditabupesh](https://avatars.discourse-cdn.com/v4/letter/p/ecc23a/32.png) [@panditabupesh](https://discourse.julialang.org/u/panditabupesh)
#### Post date: [November 9, 2020, 8:53pm UTC](https://discourse.julialang.org/t/undefvarerror-ncounter/49867/1 "2020-11-09T20:53:09Z")

</div>

I am new to Julia. How do I fix this function?

function count(word, ltr)  
ncounter = 0;  
for letter in word  
if letter == “a”  
global ncounter = ncounter + 1

```
    end
    result = ncounter 
end
return result

```

end

wcount = count(“banana”,‘a’)

julia\> include(“scratchpad.jl”)  
ERROR: LoadError: UndefVarError: ncounter not defined  
Stacktrace:  
[1] count(::String, ::Char) at c:\Projects\PHYBUDGET\pam4\_j\scratchpad.jl:9  
[2] top-level scope at c:\Projects\PHYBUDGET\pam4\_j\scratchpad.jl:14  
[3] include(::String) at .\client.jl:457  
[4] top-level scope at REPL[2]:1  
in expression starting at c:\Projects\PHYBUDGET\pam4\_j\scratchpad.jl:14

---

<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!
