1 / 18

Abstraction and Functions

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

peggygreen
Download Presentation

Abstraction and Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Abstraction andFunctions Professor Robin Burke

  2. Outline • Function Definitions • syntax • Functions • 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 • levels of interpretation • Local • what happens inside of a function • Global • what happens outside of the function

  5. Renamed example 4 document.write (...) cow animal OldMacVerse sound moo OldMacVerse (critter, noise); OldMacVerse ("duck", "quack"); global "testmac.html" critter cow noise moo

  6. 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

  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 • Temp F input by user • Output • appropriate HTML • with Celsius temperature • Resources • conversion formula

  14. Decomposition 2 • Steps • get input • calculate conversion • output HTML • Not quite pseudocode • not operations that Javascript can do • can be decomposed further

  15. 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

  16. 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

  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

More Related