Why does it matter where I place return in above code?

What you might be missing is that every call to GCD eventually returns. At that point, execution continues – the next line of code after the call will be executed.

In the first version of your code, if GCD is called, the next line is an else block (which is skipped), and the function returns nothing.

In the second version, after GCD returns, a message is printed unconditionally. Maybe this code will make the difference clear:

function GCD(a, b, r)
                   n::Int64 = max(a, b)
                   k::Int64 = min(a, b)
                   z = n-k
                   println("z is $z, n is $n, k is $k, Round $r")

                   if ((z>=1) | (k<=1))
                       println("GTD not yet Found!")
                       GCD(z, k, r+1)
                   end


                   println("GTD is $k (round $r)") # modified to print the round
                   return k, r
               end

producing

julia> GCD(32,12,0)
z is 20, n is 32, k is 12, Round 0
GTD not yet Found!
z is 8, n is 20, k is 12, Round 1
GTD not yet Found!
z is 4, n is 12, k is 8, Round 2
GTD not yet Found!
z is 4, n is 8, k is 4, Round 3
GTD not yet Found!
z is 0, n is 4, k is 4, Round 4
GTD is 4 (round 4)
GTD is 4 (round 3)
GTD is 8 (round 2)
GTD is 12 (round 1)
GTD is 12 (round 0)
(12, 0)

The rounds go from 4 to 0 because the GCD functions are returning from the end of the stack to the start. The final (12,0) is the return value of the first call to GCD.

1 Like