Abstraction and Functions Professor Robin Burke
Outline Function Definitions Functions Function libraries syntax return value multiple parameters local variables Function libraries
Function definition syntax function FahrToCelsius (tempInFahr) { return (5/9) * (tempInFahr – 32); } declaration start function name parameter names start of body function body end of body
Scope With functions Local Global levels of interpretation what happens inside of a function Global what happens outside of the function
Renamed example 4 animal cow OldMacVerse sound moo global document.write (...) animal cow OldMacVerse sound moo OldMacVerse (critter, noise); ("duck", "quack"); global "testmac.html" critter cow noise moo
Libraries We can define a group of functions Separate file usually related Separate file .js extension Load using script tag <script type="text/javascript" src="random.js" /> Call functions from page
Example random.js pick4.html convert.js ctof2.html ftok.html
Debugging examples
Computational problem-solving Initial conditions what do we know? what is the input? Output what will the solution look like? Resources what pieces already exist?
Problem decomposition Identify steps that will lead to a solution Decompose each step into steps small enough that they can be implemented Multiple stages of decomposition may be necessary
Pseudocode Programming language is too specific requires that you make low-level decisions Useful to describe steps of computation Pseudocode describe computation without actually writing the program
Example A page that convert Fahrenheit to Celcius
Decomposition 1 Initial conditions Output Resources Temp F input by user Output appropriate HTML with Celsius temperature Resources conversion formula
Decomposition 2 Steps Not quite pseudocode get input calculate conversion output HTML Not quite pseudocode not operations that Javascript can do can be decomposed further
Decomposition 3 get input calculate conversion output HTML prompt user for temp in F calculate conversion temp in C is 5/9 temp in F - 32 output HTML write temp F to page write temp C to page
Pseudocode Could be realized in multiple programming languages prompt user for temp in F temp in C is 5/9 temp in F - 32 write temp F to page write temp C to page Could be realized in multiple programming languages although only a few can be used as web scripting languages
New problem Get height and width from user Display a table (single cell) of this size with the dimensions inside
Next class Events Reading Ch. 9