Functions & Abstraction
 A function is a package for an algorithm; once written, it can be use over and over.

The Package
Functions have a specific syntax
 function <name> ( <parameter list> ) {
          <function definition>
}
<name> names are identifiers; start w/letter
<parameter list> is the input variables, a list separated by commas
<function definition> is just the program to do the work

A Sample Function
Compute the Body Mass Index when the inputs are in metric
function <name> ( <parameter list> ) {
          <function definition>
}

Writing Functions
Most programming is done by writing functions, so learning the form is key

Declarations
A function is declared by writing down the “package” … the function is used when it is called

Summarizing
Declaration: the function “package,” says what happens when the function runs
Call: the function use, asks for the computation to be run
There is only one function declaration
There can be many calls … functions are reusable
In JS, functions tend to be grouped together but the calls go where they are needed

Gold Function
Suppose we compute “weight in Au”
Worth = (Weight*12)*368.4
Begin with the form ...

Gold Function
Suppose we compute “weight in Au”
Worth = (Weight*12)*368.4

Gold Function
Suppose we compute “weight in Au”
Worth = (Weight*12)*368.4

Gold Function
Suppose we compute “weight in Au”
Worth = (Weight*12)*368.4

Testing Template
No one writes perfect programs the first time … smart programmers check
To test, have a standard page handy

Declare the Function
Put a function declaration in <script>

Try The Function

Function Features
Reviewing properties of functions
Selecting names … don’t use alert()
Parameter variables … don’t need to be declared, but work like local variables

Function Features (cont.)
Scope of Reference … refers to where in the program a variable is “known,” i.e. where its value can be referenced and/or assigned

Function Features (cont.)
Global … declared outside of functions

Function Features (cont.)
Parameters vs Arguments … parameters are the “formal” variables in the function declaration; arguments are the same thing in the call

Summary
Functions are packages for algorithms
They follow a series of rules, that quickly become intuitive
Functions have both a declaration and a call
Functions have both parameters (in the declaration) and arguments (in the call)
Scope refers to the region of a program where a variable is “known”