Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Explain scoping confusion to a programming beginner

$
0
0

The scoping rules feel unintuitive, and I feel more confused after reading numerous Discourse and StackExchange responses, such as Stefan’s explanation.

I come from MATLAB which has really straightforward and intuitive scoping rules, can someone explain why I should get different results between the two following scenarios? (I include them in a file at the REPL.)

bFound = false
for n = 1:10
    if n > 5
        bFound = true
        println("bFound = ", bFound)
        break
    end
end
println("bFound = ", bFound)

which yields

bFound = true
bFound = false

as opposed to

function Test()
    bFound = false
    for n = 1:10
        if n > 5
            bFound = true
            println("bFound = ", bFound)
            break
        end
    end
    println("bFound = ", bFound)
end

Test()

which yields

bFound = true
bFound = true

I just don’t understand the inner workings of programming languages enough to know why the best choice in the big picture of things results in obtaining different results here.

4 posts - 4 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles