Functions & Scope

In this lesson, Codecademy introduced functions, which are pieces of code that can be called multiple times to execute a series of statements. JavaScript functions have a similar syntax to those of PHP or Java, and can contain parameters and return statements. This lesson also covered the concept of scope, which is the idea that some variables are accessible/inaccessible from other parts of the program. A variable with global scope can be accessed by every part of the program, whereas a variable with functional scope is only accessible inside its function block. In the following example, the functions takeOrder, getSubTotal, getTax, and getTotal all perform different functions, but can be used in together to repeat an order and its total cost. This code would result in the output:
Order: thin crust pizza topped with pepperoni
Order: deep dish pizza topped with pineapple
Order: hand tossed pizza topped with garlic
22.5
23.85