Level 1
1 day < 1 min
LeaderboardGuest · Log in
Key point 2 of 8 · Programming Fundamentals
Estimated reading time: 4 min

Scope, Collections, and I/O

Scope determines where in a program a given variable can be seen and used. A local variable is one declared inside a function or a similarly bounded block of code, and it exists only for as long as that function is executing; once the function finishes, the variable disappears, and no code outside that function can refer to it. A global variable, by contrast, is declared outside of any function, at the top level of a program, and can be read and modified from anywhere in the code. Global variables can seem convenient because they save the trouble of passing values explicitly between functions, but this convenience comes at a real cost: when many different parts of a program can modify the same shared state, it becomes far harder to reason about how a value changes over the program's execution, and bugs caused by one function unexpectedly altering a global variable that another function depends on are notoriously difficult to track down. Most modern programming guidance therefore favors keeping variables as local as possible and passing data between functions explicitly, through parameters and return values, reserving global state for the rare cases that genuinely need it.

Food for thought

Why do you think "Scope, Collections, and I/O" was significant in this period?