Functions
                            executes code chunks when called
                             http://www.w3schools.c...
surprise!
            you already have been using them
            since your first line of code
            document.write...
defining a function

                               function foo() {
                                 // some code
        ...
defining a function
                            function keyword


                                         function foo() ...
defining a function
                            function keyword   function name


                                        ...
defining a function
                            function keyword   function name   parenthesis


                          ...
defining a function
                            function keyword   function name   parenthesis


                          ...
defining a function
                            function keyword   function name         parenthesis


                    ...
braces always mean
                 start and stop
                            (if, else if, else, for, for...in, while, f...
calling a function
                            // define function foo
                            // for later use
       ...
functions are called using
                            parenthesis and the function name



                              ...
calling a function
                            // define function foo
                            // for later use
       ...
the code inside functions only
      executes when the function is called
                            var number_1 = 5;
  ...
functions can return values
                            // define the function
                            function name()...
the instant a return statement is found,
             the function returns and stops execution




Thursday, October 1, 20...
return doesn’t have to
                         return a value
                            function demoReturn() {
       ...
functions can have
                               a parameter
       // define the function
       function name(name) {
 ...
functions can have
                               a parameter
                                      name is a parameter
  ...
functions can have
                               a parameter
                                      name is a parameter
  ...
functions can have
                            multiple parameters
                            // define the function
    ...
philosophical tidbit
                     parameter - name of value to be used in function
                     argument -...
camelCaseFunctionNames



Thursday, October 1, 2009
naming conventions

             // variables use underscores
             var full_name = 'John Nunemaker';

            ...
function can be made
                           up of any code
             function demoWithLoop(loops) {
               ...
functions, ifs, loops
                            function demoWithLoop(loops) {
                              if (loops >...
functions, ifs, loops, return
                            function demoWithLoop(loops) {
                              if ...
assignment06
                            http://teaching.johnnunemaker.com/capp-30550/sessions/functions/




Thursday, Oc...
Upcoming SlideShare
Loading in …5
×

Functions Intro

1,010
-1

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,010
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
21
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Functions Intro

  1. 1. Functions executes code chunks when called http://www.w3schools.com/js/js_functions.asp Thursday, October 1, 2009
  2. 2. surprise! you already have been using them since your first line of code document.write, alert, prompt, confirm, parseInt, etc. Thursday, October 1, 2009
  3. 3. defining a function function foo() { // some code } Thursday, October 1, 2009
  4. 4. defining a function function keyword function foo() { // some code } Thursday, October 1, 2009
  5. 5. defining a function function keyword function name function foo() { // some code } Thursday, October 1, 2009
  6. 6. defining a function function keyword function name parenthesis function foo() { // some code } Thursday, October 1, 2009
  7. 7. defining a function function keyword function name parenthesis function foo() { // some code } start of function Thursday, October 1, 2009
  8. 8. defining a function function keyword function name parenthesis function foo() { // some code } start of function end of function Thursday, October 1, 2009
  9. 9. braces always mean start and stop (if, else if, else, for, for...in, while, functions, objects Thursday, October 1, 2009
  10. 10. calling a function // define function foo // for later use function foo() { // some code } // call the function and // run the code inside it foo(); Thursday, October 1, 2009
  11. 11. functions are called using parenthesis and the function name () Thursday, October 1, 2009
  12. 12. calling a function // define function foo // for later use function foo() { // some code } // does not execute function foo; // executes function foo(); Thursday, October 1, 2009
  13. 13. the code inside functions only executes when the function is called var number_1 = 5; var number_2 = 10; var sum = number_1 + number_2; function foo() { alert('This will never alert.'); } alert(sum); // only sum will be alerted // the string inside foo function does // not because foo was never called Thursday, October 1, 2009
  14. 14. functions can return values // define the function function name() { return 'John Nunemaker'; } // call the function var name = name(); // name variable is now 'John Nunemaker' Thursday, October 1, 2009
  15. 15. the instant a return statement is found, the function returns and stops execution Thursday, October 1, 2009
  16. 16. return doesn’t have to return a value function demoReturn() { return; // this will never be called alert('hi'); } // will not alert ‘hi’ demoReturn(); Thursday, October 1, 2009
  17. 17. functions can have a parameter // define the function function name(name) { return 'The name was: ' + name; } // call the function var message = name('Ron Burgandy'); // message variable is now 'The name was: Ron Burgandy' // call the function again with different name message = name('Veronica Corningstone'); // message variable is now 'The name was: Veronica Corningstone' Thursday, October 1, 2009
  18. 18. functions can have a parameter name is a parameter // define the function function name(name) { return 'The name was: ' + name; } // call the function var message = name('Ron Burgandy'); // message variable is now 'The name was: Ron Burgandy' // call the function again with different name message = name('Veronica Corningstone'); // message variable is now 'The name was: Veronica Corningstone' Thursday, October 1, 2009
  19. 19. functions can have a parameter name is a parameter // define the function function name(name) { return 'The name was: ' + name; } ‘Ron Burgandy’ is an argument // call the function var message = name('Ron Burgandy'); // message variable is now 'The name was: Ron Burgandy' // call the function again with different name message = name('Veronica Corningstone'); // message variable is now 'The name was: Veronica Corningstone' ‘Veronica Corningstone’ is an argument Thursday, October 1, 2009
  20. 20. functions can have multiple parameters // define the function function fullName(first_name, last_name) { return first_name + ' ' + last_name; } var wes = fullName('Wes', 'Mantooth'); // wes variable is now 'Wes Mantooth' var brian = fullName('Brian', 'Fantana'); // brian variable is now 'Brian Fantana' Thursday, October 1, 2009
  21. 21. philosophical tidbit parameter - name of value to be used in function argument - value passed in when function called Thursday, October 1, 2009
  22. 22. camelCaseFunctionNames Thursday, October 1, 2009
  23. 23. naming conventions // variables use underscores var full_name = 'John Nunemaker'; // functions use camel case with lower first function fullName() { return 'John Nunemaker'; } Thursday, October 1, 2009
  24. 24. function can be made up of any code function demoWithLoop(loops) { for (var i=0; i < loops; i++) { alert("Why'd you have to wait..."); } } // two alerts demoWithLoop(2); Thursday, October 1, 2009
  25. 25. functions, ifs, loops function demoWithLoop(loops) { if (loops > 10) { alert('DO NOT WANT!'); } else { for (var i=0; i < loops; i++) { alert("Why'd you have to wait..."); } } } // alert DO NOT WANT! demoWithLoop(11); // 4 alerts of Why'd you have to wait... demoWithLoop(4); Thursday, October 1, 2009
  26. 26. functions, ifs, loops, return function demoWithLoop(loops) { if (loops > 10) { alert('DO NOT WANT!'); return; } for (var i=0; i < loops; i++) { alert("Why'd you have to wait..."); } } // alert DO NOT WANT! demoWithLoop(11); // 4 alerts of Why'd you have to wait... demoWithLoop(4); Thursday, October 1, 2009
  27. 27. assignment06 http://teaching.johnnunemaker.com/capp-30550/sessions/functions/ Thursday, October 1, 2009
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×