Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstraction and Functions Professor Robin Burke. 2 Outline Quiz JavaScript review Abstraction Function Definitions purpose syntax Functions return value.

Similar presentations


Presentation on theme: "Abstraction and Functions Professor Robin Burke. 2 Outline Quiz JavaScript review Abstraction Function Definitions purpose syntax Functions return value."— Presentation transcript:

1 Abstraction and Functions Professor Robin Burke

2 2 Outline Quiz JavaScript review Abstraction Function Definitions purpose syntax Functions return value multiple parameters local variables Function libraries

3 3 Quiz 10 minutes

4 4 Quiz Answers

5 5 JavaScript so far statements assignment function calls data types numeric string boolean expressions variables operators function calls

6 6 Example tempInFahr = prompt("Enter the temperature (in Fahrenheit):", "32"); tempInFahr = parseFloat(tempInFahr); tempInCelsius = (5/9) * (tempInFahr - 32); document.write("You entered " + tempInFahr + " degrees Fahrenheit. "); document.write("That's equivalent to " + tempInCelsius + " degrees Celsius.");

7 7 Abstraction JavaScript has built-in functions document.write Math.sqrt Benefit of abstraction we don't have to write these we don't have to know how they work Fundamental idea in computer science

8 8 User-defined functions Our own abstractions FahrToCelsius RollDie Benefits code once, use often fix/enhance in one place add structure to large programs groups and names chunks of computation

9 9 Function call syntax prompt ( "Enter a number", "0") return value is a string containing the user input function nameparameter #1 parameter #2 parameter list

10 10 Function definition syntax function FahrToCelsius (tempInFahr) { return (5/9) * (tempInFahr – 32); } declaration start function nameparameter names function body start of body end of body

11 11 return statement Math.sqrt returns a value document.write does not If a return statement exists function will return a value what value? If not no value

12 12 Documentation Functions are self-contained meant to be used elsewhere eventually by others Documentation very important Book convention function FahrToCelsius (tempInFahr) // Assumes: tempInFahr is a temperature in Fahrenheit // Returns: the equivalent temperature in Celsius { return (5/9) * (tempInFahr – 32); }

13 13 Example CelsiusToFahr Old MacDonald 1 Old MacDonald 2

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

15 15 Scope example 1 animalcow soundundefined global "testmac.html" animal = prompt ( "Enter the...); sound = prompt ("What sound...);

16 16 Scope example 2 animalcow soundmoo global "testmac.html" animal = prompt ( "Enter the...); sound = prompt ("What sound...);

17 17 Scope example 3 animalcow soundmoo global "testmac.html" OldMacVerse (animal, sound); OldMacVerse ("duck", "quack");

18 18 Scope example 4 animalcow soundmoo global "testmac.html" OldMacVerse (animal, sound); OldMacVerse ("duck", "quack"); OldMacVerse animal cow soundmoo document.write (...)

19 19 Scope example 5 animalcow soundmoo global "testmac.html" OldMacVerse (animal, sound); OldMacVerse ("duck", "quack"); OldMacVerse animal cow soundmoo document.write (...) Old MacD..outputStringdocument.write??

20 20 Scope example 6 animalcow soundmoo global "testmac.html" OldMacVerse (animal, sound); OldMacVerse ("duck", "quack");

21 21 Scope example 7 animalcow soundmoo global "testmac.html" OldMacVerse (animal, sound); OldMacVerse ("duck", "quack"); OldMacVerse animal duck soundquack document.write (...)

22 22 Scope example 8 animalcow soundmoo global "testmac.html" OldMacVerse (animal, sound); OldMacVerse ("duck", "quack"); OldMacVerse animal duck soundquack document.write (...) Old MacD..outputStringdocument.write??

23 23 Scope example 9 animalcow soundmoo global "testmac.html" OldMacVerse (animal, sound); OldMacVerse ("duck", "quack");

24 24 Variable names Affect labels on boxes May affect what values are accessible

25 25 Renamed example 1 crittercow noiseundefined global "testmac.html" critter = prompt ( "Enter the...); noise = prompt ("What sound...);

26 26 Renamed example 2 crittercow noisemoo global "testmac.html" critter = prompt ( "Enter the...); noise = prompt ("What sound...);

27 27 Renamed example 3 crittercow noisemoo global "testmac.html" OldMacVerse (critter, noise); OldMacVerse ("duck", "quack");

28 28 Renamed example 4 crittercow noisemoo global "testmac.html" OldMacVerse (critter, noise); OldMacVerse ("duck", "quack"); OldMacVerse animal cow soundmoo document.write (...)

29 29 What if? we change the value of sound at the end of OldMacVerse sound = "snort"; we return a value from OldMacVerse typed nothing into the prompt canceled the prompt

30 30 Local variable example taxes.html

31 31 Libraries We can define a group of functions usually related Separate file.js extension Load using script tag Call functions from page

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

33 33 Debugging examples

34 34 Next class Algorithms Reading Ch. 8 Homework #4 due


Download ppt "Abstraction and Functions Professor Robin Burke. 2 Outline Quiz JavaScript review Abstraction Function Definitions purpose syntax Functions return value."

Similar presentations


Ads by Google