- reusable bits of code
- called or invoked using the function’s name and parentheses, e.g. functionName();
- parameters or arguments can be passed in (as per pic above)
- i.e. visibility of variables
- Global variables are declared outside of the function block and can be ‘seen’ by the rest of the code
- omitting the var keyword when declaring a variable automatically makes it global – this can cause unintentional errors!
- Local variables are those declared within the function, including the function parameters, and are not accessible outwith the function
- you can create local and global variables with the same name; the local one will take precedence. Again – this can cause many errors for the unwary!
- use the return keyword to send a value back out of a function
- the return value of a function can be assigned to a variable, e.g. var solution = functionName(8);