Section 1 - Instruction

Last time, we learned how to build practical Go functions that take parameters and return values. But have you noticed something interesting about variables inside functions versus variables outside them?

Engagement Message

Where exactly do variables "live" in our Go programs?

Section 2 - Instruction

Variables created inside a function have local scope - they only exist within that function. These local variables are born when the function starts and disappear when it ends.

Engagement Message

What do you think happens to the result variable after the function finishes?

Section 3 - Instruction

Local variables can't be accessed from outside their function. This code would cause an error because result doesn't exist outside the function:

Engagement Message

Why do you think this protection exists?

Section 4 - Instruction

Variables created outside functions have global scope – they can be accessed from anywhere in the program, including inside functions:

Notice that to make a global variable, we use var outside any function. The := shortcut only works inside functions, not at the top level of your code. That's why is used here.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal