# Getting return value From Function with loop

**URL:** <https://discourse.julialang.org/t/getting-return-value-from-function-with-loop/99714>\
**Category:** New to Julia\
**Created:** [June 1, 2023, 9:33am UTC](https://discourse.julialang.org/t/getting-return-value-from-function-with-loop/99714 "2023-06-01T09:33:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephen1959](https://avatars.discourse-cdn.com/v4/letter/s/bbe5ce/32.png) [@Stephen1959](https://discourse.julialang.org/u/Stephen1959)\
**Post date:** [June 1, 2023, 9:33am UTC](https://discourse.julialang.org/t/getting-return-value-from-function-with-loop/99714/1 "2023-06-01T09:33:25Z")

</div>

Greetings. This ius my first post. Below is code that calculates the value of a scrabble word.  
It works well if I use the println() but cannot get it to return the value in total varable. Thanh you  
function score(str)  
letterValue = 0  
total = 0

dict = Dict(‘a’ =\> 1, ‘e’ =\> 1, ‘i’ =\> 1, ‘o’ =\> 1, ‘u’ =\> 1, ‘l’ =\> 1, ‘n’ =\> 1,‘r’ =\> 1,‘s’ =\> 1,  
‘t’ =\> 1,‘d’ =\> 2,‘g’ =\> 2,‘b’ =\> 3,‘c’ =\> 3,‘m’ =\> 3,‘p’ =\> 3, ‘f’ =\> 4,‘h’ =\> 4,‘v’ =\> 4,‘w’ =\> 4,  
‘y’ =\> 4,‘k’ =\> 5, ‘j’ =\> 8, ‘x’ =\> 8,‘q’ =\> 10, ‘z’ =\> 10)

for character in str  
letterValue = dict[character]  
total += letterValue  
end  
end

Using Julia 1.9 and VS Code

---

<div class="post-metadata">

**Author:** ![vsoler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vsoler/32/30667_2.png) [@vsoler](https://discourse.julialang.org/u/vsoler)\
**Post date:** [June 1, 2023, 9:44am UTC](https://discourse.julialang.org/t/getting-return-value-from-function-with-loop/99714/2 "2023-06-01T09:44:34Z")

</div>

```julia
function score(str)
    letterValue = 0
    total = 0

    dict = Dict('a' => 1, 'e' => 1, 'i' => 1, 'o' => 1, 'u' => 1, 'l' => 1, 'n' => 1, 'r' => 1, 's' => 1,
        't' => 1, 'd' => 2, 'g' => 2, 'b' => 3, 'c' => 3, 'm' => 3, 'p' => 3, 'f' => 4, 'h' => 4, 'v' => 4, 'w' => 4,
        'y' => 4, 'k' => 5, 'j' => 8, 'x' => 8, 'q' => 10, 'z' => 10)

    for character in str
        letterValue = dict[character]
        total += letterValue
    end
    total
end

```

Just add “total” before the last “end” as shown, and it will work.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [June 1, 2023, 9:49am UTC](https://discourse.julialang.org/t/getting-return-value-from-function-with-loop/99714/3 "2023-06-01T09:49:34Z")

</div>

To be a little more explicit, this is a shortcut for `return total`, which means that when you call the function `result = score(str)`, the `result` variable will contain what you named `total` inside the function

---

<div class="post-metadata">

**Author:** ![Stephen1959](https://avatars.discourse-cdn.com/v4/letter/s/bbe5ce/32.png) [@Stephen1959](https://discourse.julialang.org/u/Stephen1959)\
**Post date:** [June 1, 2023, 9:59am UTC](https://discourse.julialang.org/t/getting-return-value-from-function-with-loop/99714/4 "2023-06-01T09:59:29Z")

</div>

THANK YOU Tried that using score(“apple”) but still no result comes out

---

<div class="post-metadata">

**Author:** ![Stephen1959](https://avatars.discourse-cdn.com/v4/letter/s/bbe5ce/32.png) [@Stephen1959](https://discourse.julialang.org/u/Stephen1959)\
**Post date:** [June 1, 2023, 10:03am UTC](https://discourse.julialang.org/t/getting-return-value-from-function-with-loop/99714/5 "2023-06-01T10:03:09Z")

</div>

[tephen1959](https://discourse.julialang.org/u/Stephen1959)

![](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vsoler/25/30667_2.png "vsoler") vsoler

[3m](https://discourse.julialang.org/t/getting-return-value-from-function-with-loop/99714/4)

THANK YOU Tried that using score(“apple”) but still no result comes out

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 1, 2023, 10:05am UTC](https://discourse.julialang.org/t/getting-return-value-from-function-with-loop/99714/6 "2023-06-01T10:05:50Z")

</div>

Are you sure? Here’s a fresh REPL session:

```julia
julia> function score(str)
           letterValue = 0
           total = 0

           dict = Dict('a' => 1, 'e' => 1, 'i' => 1, 'o' => 1, 'u' => 1, 'l' => 1, 'n' => 1, 'r' => 1, 's' => 1,
               't' => 1, 'd' => 2, 'g' => 2, 'b' => 3, 'c' => 3, 'm' => 3, 'p' => 3, 'f' => 4, 'h' => 4, 'v' => 4, 'w' => 4,
               'y' => 4, 'k' => 5, 'j' => 8, 'x' => 8, 'q' => 10, 'z' => 10)

           for character in str
               letterValue = dict[character]
               total += letterValue
           end
           total
       end
score (generic function with 1 method)

julia> score("apple")
9

julia> result = score("apple")
9

julia> result
9

```

---

<div class="post-metadata">

**Author:** ![Stephen1959](https://avatars.discourse-cdn.com/v4/letter/s/bbe5ce/32.png) [@Stephen1959](https://discourse.julialang.org/u/Stephen1959)\
**Post date:** [June 1, 2023, 10:39am UTC](https://discourse.julialang.org/t/getting-return-value-from-function-with-loop/99714/7 "2023-06-01T10:39:23Z")

</div>

Thank you to you all. Got it sorted out with your help

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [June 1, 2023, 12:20pm UTC](https://discourse.julialang.org/t/getting-return-value-from-function-with-loop/99714/8 "2023-06-01T12:20:21Z")

</div>

Was this a problem from [here](https://exercism.org/tracks/julia/exercises/scrabble-score)?
