A global is visible from anywhere in the translation unit, so any function can read or modify it. Bugs from globals are notoriously hard to track down because the "modifying" code can be physically far from the "reading" code, you can't tell where a value comes from without searching the whole file. Globals also make testing hard because functions become dependent on hidden state, so a test of f() might pass or fail based on what other functions ran before it. They make concurrency hard because two threads touching the same global require synchronization that's invisible at the call site. The cumulative effect is that a function's behavior stops being a function of its inputs alone, it depends on the entire history of the program. Local variables and explicit parameters force that dependency to show up in the code.
Programming Fundamentals · Interview question
Why is having many global variables a code smell?
A strong answer
What a weak answer sounds like
You know the answer. Do you know what gets you dinged?
Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.
From the lesson
Functions
Naming a chunk of logic so you can call it from many places. Parameters in, return value out, scope rules: the building block of every program.