3/31/2016 FWP lecture 4 http://www.cs.dartmouth.edu/~fwp/slides04/slides04db.html?m=all&s=0&f=0 1/35 Fundamentals of Web Programming Lecture 4: functions and abstraction Devin Balkcom [email protected]office: Sudikoff 206 http://www.cs.dartmouth.edu/~fwp
35
Embed
Fundamentals of Web Programmingfwp/slides04/slides04db.pdf · 3/31/2016 FWP lecture 4 ... Fundamentals of Web Programming Lecture 4: ... Functional programming ...
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Review: loopsAllow control of the program counter.Condition is tested to decide whether to iterate again.Condition must change from true to false to avoid infinite loop.Loops can be nested.Design either outer or inner loop first.Usually use a for loop for counting.
Review: defining your own functionsFunctions allow packaging of small pieces of code. Abstraction!Program counter: execute the function and then return.
Function header declares variables. (But no var keyword.)On function call, values are copied into variables.
►
// Draw a square with upper left x, y and side length svar square = function(x, y, s) { line(x, y, x + s, y); line(x + s, y, x + s, y + s); line(x + s, y + s, x, y + s); line(x, y + s, x, y);};
The function executes, then makes its value available. A function callis an expression that is computed by running the function.
►
var y = Math.sqrt(25); // store the return value from sqrt in xprint(y);
print( Math.sqrt(4) ); // use sqrt return value as actual parameter
// use sqrt return value to test value of i in a for loop:print("Integer factors of 100: ");for( var i = 1; i < Math.sqrt(100); i++ ) { if (100 % i === 0 ) { print(" " + i + " " + 100 / i); }}
1. returns the value following return (if any) to the calling code.2. immediately stops execution of the body of the function andresumes execution at the point of call.
►
var computeFour = function() { print("Your wish is my command! I compute four!"); return 24 / 4 - 2;};
Function framesWhen a function is called, the intepreter creates a frame for thatfunction, an area of memory where it stores its local variables.When a function returns, the frame is destroyed. The localvariables are destroyed irrevocably and are no longer in scope.
Global variables are declared outside any function, in the globalframe.
Thinking about functionsLocal variables are an example of the principle of least privilege.Modules should have access only to what they need to do a job.
When designing a function:
What values go in? (parameters)What temporary variables are used? (local variables)What is the single output of the function? (return)
Exercise: alertFind a blank exercise window. Use the alert function and thesetTimout function to schedule an alert to be displayed at the end ofclass. (alert takes a parameter, so you might need to wrap it inanother function that does not for use with setTimout.)
Callback chainsYou can use callbacks to execute complex behavior even after yourprogram stops.
For example, how can we set up a callback to count down from 10 to1 and print blastoff, with one count per second? I can think of at leasttwo approaches.
if(counter > 0) { print(counter); // set a new timeout callback setTimeout(countdown, 1000); counter--; } else { // we're done, don't ask for another callback print("Blast off!"); } };
print("Prepare for countdown:");// create a counter variable in the outer scope,// so that itis available to all calls of the countdown functionvar counter = 10; setTimeout(countdown, 1000);