Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstraction and Functions

Similar presentations


Presentation on theme: "Abstraction and Functions"— Presentation transcript:

1 Abstraction and Functions
Professor Robin Burke

2 Outline Function Definitions Functions Function libraries syntax
return value multiple parameters local variables Function libraries

3 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

4 Scope With functions Local Global levels of interpretation
what happens inside of a function Global what happens outside of the function

5 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

6 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

7 Example random.js pick4.html convert.js ctof2.html ftok.html

8 Debugging examples

9 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?

10 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

11 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

12 Example A page that convert Fahrenheit to Celcius

13 Decomposition 1 Initial conditions Output Resources
Temp F input by user Output appropriate HTML with Celsius temperature Resources conversion formula

14 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

15 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

16 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

17 New problem Get height and width from user
Display a table (single cell) of this size with the dimensions inside

18 Next class Events Reading Ch. 9


Download ppt "Abstraction and Functions"

Similar presentations


Ads by Google