Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Collection of statements that can be invoked as a unit  Can take parameters  Can be used multiple times  Can call without knowing what they do or.

Similar presentations


Presentation on theme: " Collection of statements that can be invoked as a unit  Can take parameters  Can be used multiple times  Can call without knowing what they do or."— Presentation transcript:

1

2  Collection of statements that can be invoked as a unit  Can take parameters  Can be used multiple times  Can call without knowing what they do or how

3

4 HTML

5

6

7

8 JAVASCRIPT

9 HTMLJAVASCRIPT

10 HTMLJAVASCRIPT

11 HTMLJAVASCRIPT

12 HTMLJAVASCRIPT

13 Parameters Return Function Building Up Function Capabilities

14 Move the onclick work into a function

15 Function

16 function name() { stmt; }

17 Let the function work on any data

18 Parameters Function

19 function name(parm) { stmt; }

20  Name is a placeholder  Can be used anywhere in a function  Can be used as many times as you want  Can not change it  MUST supply value when call  Can be different every time

21  Just a special type of variable  Something that you hand to the function  Q: Many users: how do you name?  A: Give it its OWN names to use locally  Q: How do you match up?  A: By POSITION

22 HTML JAVASCRIPT (FUNCTION.JS) function doit (a,b) { var c = a*b); alert(“product is ”+c); }

23 HTMLJS mypet(‘cow’); mypet(‘dog’); function mypet(pet) { alert(‘my pet: ‘+pet); }

24  Order matters  Need different names

25 HTMLJS mypet(‘Mutt’,’Jeff’); mypet(‘Jeff’,’Mutt’); function taller(big,small) { alert (big+’ is taller than ‘+ small); }

26 Let the result be placed anywhere

27 Parameters Return Function

28  Use the function as a value  form.field.value = function(parm1, parm2);  difference = subtract(minuhend,subtrahend);  Contrast with  alert(string);  append(form.field.value,’end’);

29 return (value);  Want to get information BACK to HTML  With a return, the function has a VALUE  Can be used anywhere you can use a constant or variable  Alert  Assignment statement

30 HTML JAVASCRIPT (FUNCTION.JS) function doit (a,b) { var c = a*b); return(c); }

31

32  Need to define  Inputs  Outputs  What to do

33  These are the parameters  Order matters  Need a way to reference them  Position 1, position 2, …  Cubby holes  Better to use meaningful names  Each name is just a pointer to the cubby hole

34  Use a RETURN statement  A write-once cubby hole  Only way to access is the RETURN statement  Once you set it, the function is ended  Can have a simple value or more (e.g., concatenating strings)

35  Series of statements: the recipe  Assignment statements  Function calls  Can use  Literals (5, “ “)  parameters  Specially defined locations ( variables )

36

37  Like CSS, can be inline or a separate file  Like CSS, an external file is considered better practice  Like CSS, link to it from the head section

38 Myscript.js JavaScript option in komodo note that it’s an empty file but it can help you do NOT include the script tag

39 HTML JAVASCRIPT (FUNCTION.JS) function doit () { alert(“Hi!”); }


Download ppt " Collection of statements that can be invoked as a unit  Can take parameters  Can be used multiple times  Can call without knowing what they do or."

Similar presentations


Ads by Google