a guest May 15th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- Scope is the way in which variables interact with each other. If we misunderstand the scope of our code, we risk our program acting in a manner we didn’t intend it to. A variable set at the global scope level is one that will be recognized throughout the program, variables are said to be global if they have been declared outside of a function. A variable is said to be a local/block variable if it has been declared within a function. The local variable is intended for use within that function only and will not be used again in the program unless called upon, however a global variable will still be in use.
- Global variables are to be avoided because of the unintended side-effects they have on a program. A side-effect will reach outside its scope and alter values in the parent scope – which are unintended side-effects. This leads the code to becoming ‘indeterminate’ whereby a function will return a different value at a different time – leading to bugs and difficulty in handling errors. Functions that don’t have any side effects are known as ‘pure functions’ – they are determinate and will return the same value all the time. For these very reasons it’s important that we aim to keep all functions ‘pure’ and steer clear of global variable use.
RAW Paste Data