# Why doesn't this return the intended value

**URL:** <https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916>\
**Category:** New to Julia\
**Created:** [April 23, 2022, 9:23pm UTC](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916 "2022-04-23T21:23:22Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexanderChen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexanderchen/32/22007_2.png) [@AlexanderChen](https://discourse.julialang.org/u/AlexanderChen)\
**Post date:** [April 23, 2022, 9:23pm UTC](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916/1 "2022-04-23T21:23:22Z")

</div>

Hi,

Background: this is the last part of my implementation of the huffman compression. The good news is,  
it seems to work the `print(decoded_string)` give back the string it decoded before. But it does not return it. it returns `nothing` the value of `decoded_string` is` ""` initially.

other parameter are:  
`encoded_string`: is a string with 1’s and 0’s.  
`key_encoding_array`: an array of tuples with the structure `Vector{Tuple{String, String,Int64}}`

```julia
function rebuilding_message(encoded_string,key_encoding_array::Vector{Tuple{String, String,Int64}},decoded_string)

    if isempty(encoded_string)
       print(decoded_string)    
       return decoded_string
    else
       for i in key_encoding_array
            if length(encoded_string) >= i[3] && i[2] == encoded_string[1:i[3]]
               rebuilding_message(SubString(encoded_string,(i[3]+1)),key_encoding_array,string(decoded_string,i[1]))
            end
        end
    end
end

```

thanks in advance!

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [April 23, 2022, 10:25pm UTC](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916/2 "2022-04-23T22:25:26Z")

</div>

It’s because the `else` branch doesn’t return anything.

---

<div class="post-metadata">

**Author:** ![AlexanderChen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexanderchen/32/22007_2.png) [@AlexanderChen](https://discourse.julialang.org/u/AlexanderChen)\
**Post date:** [April 24, 2022, 6:26am UTC](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916/3 "2022-04-24T06:26:08Z")

</div>

Sorry, i don’t understand what you mean. The else branch does not return but it does contain a recursively call to the same function. The basecase of the recursion should have a return and it does. Can you otherwise show me what you mean?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [April 24, 2022, 6:55am UTC](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916/4 "2022-04-24T06:55:16Z")

</div>

> [@AlexanderChen](#):
>
> call to the same function.

you still need to return:

```julia

julia> function f(a)
           if a == 0
               return 0
           else
               for i = 1:1
                   f(a-1)
               end
           end
       end
f (generic function with 1 method)

julia> f(2)

julia> function f(a)
           if a == 0
               return 0
           else
               for i = 1:1
                   return f(a-1)
               end
           end
       end
f (generic function with 1 method)

julia> f(2)
0

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [April 24, 2022, 9:28am UTC](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916/5 "2022-04-24T09:28:42Z")

</div>

Isn’t this an example of a tail recursive function? Perhaps OP comes from a language where this is automatically optimized. There is an interesting discussion: [tail call elimination · Issue #4964 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/4964)

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [April 24, 2022, 3:37pm UTC](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916/6 "2022-04-24T15:37:34Z")

</div>

Unless `rebuilding_message` modifies its arguments, which doesn’t seem to be true here, you want to return something from the `else` branch. Same as

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

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 24, 2022, 4:17pm UTC](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916/7 "2022-04-24T16:17:02Z")

</div>

uhh, no? This is not really a good example, did you try it without the `return` in the `else` clause? It works the exact same way. If the `else` pertains to an `if` that is the last expression in a method and the last line of the `else` has any value then that value is returned, as in any other method in Julia.

---

<div class="post-metadata">

**Author:** ![AlexanderChen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexanderchen/32/22007_2.png) [@AlexanderChen](https://discourse.julialang.org/u/AlexanderChen)\
**Post date:** [April 25, 2022, 2:28pm UTC](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916/8 "2022-04-25T14:28:38Z")

</div>

I have put a return in the else statement. No change in result. I have adjusted some stuff but no solution yet.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [April 25, 2022, 2:32pm UTC](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916/9 "2022-04-25T14:32:32Z")

</div>

What happens if key is not in the array? Then the loop finishes with nothing, no?

---

<div class="post-metadata">

**Author:** ![AlexanderChen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexanderchen/32/22007_2.png) [@AlexanderChen](https://discourse.julialang.org/u/AlexanderChen)\
**Post date:** [April 25, 2022, 2:36pm UTC](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916/10 "2022-04-25T14:36:10Z")

</div>

correct, hasn’t happened as far as I can tell. there is a print(decoded\_string) just before the return. This starts out as “” and but does print out the correct text. I do think the construction of if statement - for loop - if statement seems to not work properly.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 25, 2022, 2:44pm UTC](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916/11 "2022-04-25T14:44:16Z")

</div>

Something like this

```julia
function rebuilding_message(encoded_string,key_encoding_array::Vector{Tuple{String, String,Int64}},decoded_string)
    @show encoded_string, decoded_string
    if !isempty(encoded_string)
       for i in key_encoding_array
            if length(encoded_string) >= i[3] && i[2] == encoded_string[1:i[3]]
               decoded_string = rebuilding_message(SubString(encoded_string,(i[3]+1)),key_encoding_array,string(decoded_string,i[1]))
            end
        end
    end
    return decoded_string
end

@show rebuilding_message("abc", [("a", "a", 1), ("b", "b", 1), ("c", "c", 1)], "")

```

seems to be what you intended?

---

<div class="post-metadata">

**Author:** ![AlexanderChen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexanderchen/32/22007_2.png) [@AlexanderChen](https://discourse.julialang.org/u/AlexanderChen)\
**Post date:** [April 25, 2022, 6:26pm UTC](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916/12 "2022-04-25T18:26:56Z")

</div>

@goerch : no not really. The `decoded_string` start of as empty` ""`. And with each recursion a character is added to the `decoded_string` and the `encoded_string` becomes smaller. Up to the point that the `encoded_string` is empty and the `decoded_string` is full.

i didn’t know that @show command though…that help with the debugging!

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 25, 2022, 6:30pm UTC](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916/13 "2022-04-25T18:30:16Z")

</div>

> [@AlexanderChen](#):
>
> @goerch : no not really. The `decoded_string` start of as empty ` ""` . And with each recursion a character is added to the `decoded_string` and the `encoded_string` becomes smaller. Up to the point that the `encoded_string` is empty and the `decoded_string` is full.

and is returned. That is what I believe to see in my REPL

```julia
(encoded_string, decoded_string) = ("abc", "")
(encoded_string, decoded_string) = ("bc", "a")
(encoded_string, decoded_string) = ("c", "ab")
(encoded_string, decoded_string) = ("", "abc")
rebuilding_message("abc", [("a", "a", 1), ("b", "b", 1), ("c", "c", 1)], "") = "abc"

```

?

Edit: tried a maybe better example

```julia
@show rebuilding_message("abc", [("d", "a", 1), ("e", "b", 1), ("f", "c", 1)], "")

```

and get

```julia
(encoded_string, decoded_string) = ("abc", "")
(encoded_string, decoded_string) = ("bc", "d")
(encoded_string, decoded_string) = ("c", "de")
(encoded_string, decoded_string) = ("", "def")
rebuilding_message("abc", [("d", "a", 1), ("e", "b", 1), ("f", "c", 1)], "") = "def"

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [April 25, 2022, 6:39pm UTC](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916/14 "2022-04-25T18:39:11Z")

</div>

> [@AlexanderChen](#):
>
> Sorry, i don’t understand what you mean. The else branch does not return but it does contain a recursively call to the same function. The basecase of the recursion should have a return and it does. Can you otherwise show me what you mean?

That only returns to the previous stackframe, which ends up in the `for` loop of the previous call.

* * *

Loop blocks by default return `nothing` because it’s not well defined which of the loop iteration values should be returned. Thus you need to save whichever result you want to return and then `return` that _after_ the loop is done.

---

<div class="post-metadata">

**Author:** ![AlexanderChen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexanderchen/32/22007_2.png) [@AlexanderChen](https://discourse.julialang.org/u/AlexanderChen)\
**Post date:** [April 25, 2022, 7:17pm UTC](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916/15 "2022-04-25T19:17:08Z")

</div>

@Sukera I get what you mean but something doesn’t make sense to me.I did a slight rewrite based on what @goerch. replaced the print with @show.

My problem is that if I do this I get the right result. How is that possible? @show gives me the correct string in the correct sequence back but adding a return somehow gives nothing. The explanation I have heard thus far make perfect sense if it wasn’t for the correct string first being given.

```julia
function rebuilding_message(encoded_string,key_encoding_array::Vector{Tuple{String, String,Int64}},decoded_string)
    if isempty(encoded_string)
        @show decoded_string
        return decoded_string
     else
        for i in key_encoding_array
             if length(encoded_string) >= i[3] && i[2] == encoded_string[1:i[3]]
                rebuilding_message(SubString(encoded_string,(i[3]+1)),key_encoding_array,string(decoded_string,i[1]))
             end
         end
     end
 end

```

---

<div class="post-metadata">

**Author:** ![AlexanderChen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexanderchen/32/22007_2.png) [@AlexanderChen](https://discourse.julialang.org/u/AlexanderChen)\
**Post date:** [April 25, 2022, 7:19pm UTC](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916/16 "2022-04-25T19:19:41Z")

</div>

@goerch I now get a string back but its complete jumbled. not reversed or something fixable. it’s basically now encrypted.🙂

Though it seems to work for your example.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 25, 2022, 7:23pm UTC](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916/17 "2022-04-25T19:23:12Z")

</div>

Hi @AlexanderChen, what I tried to show you is how easy it should be to complete the M(inimal)W(orking)E(example) for others to check. Simply fill in the parameters of the call of `rebuilding_message` and we should find a way to make this work.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [April 25, 2022, 7:29pm UTC](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916/18 "2022-04-25T19:29:19Z")

</div>

> [@AlexanderChen](#):
>
> My problem is that if I do this I get the right result. How is that possible? @show gives me the correct string in the correct sequence back but adding a return somehow gives nothing. The explanation I have heard thus far make perfect sense if it wasn’t for the correct string first being given.

Having computed the correct string and returning it from the call stack are two different things - you’re printing it to the console, but _not_ returning it from the function in the `else` branch. You’re not assigning the result if `rebuilding_message` in the recursion to anything, so its return value is ignored.

Try this instead:

```julia
function rebuilding_message(encoded_string,key_encoding_array::Vector{Tuple{String, String,Int64}},decoded_string)
    if isempty(encoded_string)
        return decoded_string
     else
        for i in key_encoding_array
             if length(encoded_string) >= i[3] && i[2] == encoded_string[1:i[3]]
                # actually return the value we get from the recursion further up the call stack
                return rebuilding_message(SubString(encoded_string,(i[3]+1)),key_encoding_array,string(decoded_string,i[1]))
             end
         end
     end
 end

```

---

<div class="post-metadata">

**Author:** ![AlexanderChen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexanderchen/32/22007_2.png) [@AlexanderChen](https://discourse.julialang.org/u/AlexanderChen)\
**Post date:** [April 25, 2022, 7:33pm UTC](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916/19 "2022-04-25T19:33:43Z")

</div>

@Sukera I already tried this yesterday and did it now again. result is both identical namely `nothing`

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [April 25, 2022, 8:07pm UTC](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916/20 "2022-04-25T20:07:28Z")

</div>

Then I suspect there’s a bug in your logic. The only way `nothing` can be returned from there is if the `if` condition in the loop is never true, i.e. the recursion isn’t run in some call. Semantically it’s the same as this:

```julia
function rebuilding_message(...)
     if isempty(encoded_string)
        return decoded_string
     else
        for i in key_encoding_array
             if length(encoded_string) >= i[3] && i[2] == encoded_string[1:i[3]]
                # recursive call, only hit when the if is true
                return rebuilding_message(...)
             end
        end

        return nothing
     end
 end

```

Like I said above, `for` loops don’t implicitly return any value (i.e. the branch then returns `nothing`). Does that make more sense to you?

[Next page](https://discourse.julialang.org/t/why-doesnt-this-return-the-intended-value/79916.md?page=2)
