Last time, we learned about default parameters to make functions more flexible. But have you noticed something interesting about variables inside functions versus variables outside them?
Engagement Message
Where exactly do variables "live" in our programs?
Variables created inside a function have local scope. They only exist within that function's curly braces {}
. These local variables are "born" when the function is called and disappear when it ends.
Engagement Message
What do you think happens to the result
after the function finishes?
Local variables can't be accessed from outside their function. This code would cause an error because result
is not defined in the outer scope:
Engagement Message
Why do you think this privacy is a good feature?
Variables created outside of any function have global scope. They can be accessed from anywhere in the program, including inside other functions.
Engagement Message
Can you see how username
is available everywhere?
