Notes on FUNCTIONS Let's try to clarify the difference between function (a division of your program code) and function call (causing a function to execute and compute something) physical real-world example if you are a painter, you can paint a room any color, not just one color someone gives you a can of yellow paint, you paint the room yellow someone gives you a can of beige paint, you paint the room beige someone gives you a can of blue paint, you paint the room blue we do not have to hire a blue painter to paint the living room, and a different yellow painter to paint the family room we hire one painter, and give him/her different cans for different rooms the painter "executes" the paint function in each room, and the can of paint is the "parameter" we pass to make the "computation" vary from room to room function is the same way... it does some general activity (as defined in the function body) and you pass to it the specific data it will act upon (when you call the function). so we define a function to load an array with random numbers we call it, and pass it array "first", and "first" gets loaded we call it, and pass it array "second", and "second" gets loaded so we define a function to compute 2 raised to a power we call it, and pass it 5 and it produces 2^5 and returns 32 we call it, and pass it 7 and it produces 2^5 and returns 128 we define a function once (we write the code that tells the parameter names and order, the local variable inside the function, the code for the function body, and what the function returns, if anything) function pow2 ( exponent ) { var i; var result=1; for (i=1; i<=exponent; i++) { result=2*result; } return result; } We can call a function (make a function execute and compute something) as many times as we need. We pass the data to it that we want to have worked on quant = pow2(4); alert(quant); var n = 6; quant = pow2(n); alert(quant); alert(pow2(10)); quant = 17 + pow2(5); alert(quant); Functions provide several advantages in writing programs. First, by calling a function in different places, we do not have to have repeated copies of the body of the function code in our program in different places... we prevent errors that come from replicating code Here is the code above without writing a function "pow2" var i; var quant; exponent = 4; quant=1; for (i=1; i<=exponent; i++) { quant=2*quant; } alert(quant); exponent = 6; quant=1; for (i=1; i<=exponent; i++) { quant=2*quant; } alert(quant); exponent = 10; quant=1; for (i=1; i<=exponent; i++) { quant=2*quant; } alert(quant); exponent = 5; quant=1; for (i=1; i<=exponent; i++) { quant=2*quant; } quant = 17 + quant; alert(quant); Second, we get the advantage of abstraction... we see the name of the function only at the call, and do not have to look at all the details of the body of the function code, so we have a higher-level understanding of the computation where the function is called Third, using functions allows us to write our programs in modular fashion, breaking a large problem down into interacting smaller problems, solving each smaller problem one at a time, and using "stubs" for our functions until they are fleshed out fully.