180 likes | 192 Views
Abstraction and Functions. Professor Robin Burke. Outline. Function Definitions syntax Functions return value multiple parameters local variables Function libraries. Function definition syntax. function FahrToCelsius (tempInFahr) { return (5/9) * (tempInFahr – 32); }. declaration
E N D
Abstraction andFunctions Professor Robin Burke
Outline • Function Definitions • syntax • Functions • 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 • levels of interpretation • Local • what happens inside of a function • Global • what happens outside of the function
Renamed example 4 document.write (...) cow animal OldMacVerse sound moo OldMacVerse (critter, noise); OldMacVerse ("duck", "quack"); global "testmac.html" critter cow noise moo
Libraries • We can define a group of functions • 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
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 • Temp F input by user • Output • appropriate HTML • with Celsius temperature • Resources • conversion formula
Decomposition 2 • Steps • get input • calculate conversion • output HTML • Not quite pseudocode • not operations that Javascript can do • can be decomposed further
Decomposition 3 • get input • 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 • 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