1 / 20

Wed/Fri Week 2

Wed/Fri Week 2. Functions! What are they? What do they look like in JavaScript? What are they good for? How do I use them? Some examples… Mini-Lab 1!!!. What is a function?. A ‘machine’ that does something:. Function. What is a function?. It has a place for input…. Input. Function.

zasha
Download Presentation

Wed/Fri Week 2

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. Wed/Fri Week 2 • Functions! • What are they? • What do they look like in JavaScript? • What are they good for? • How do I use them? • Some examples… • Mini-Lab 1!!!

  2. What is a function? • A ‘machine’ that does something: Function

  3. What is a function? • It has a place for input… Input Function

  4. What is a function? • …and a place for output… Input Function Output

  5. What is a function? • …and a ‘crank’ to ‘do the work’. Input Function Output

  6. What is a function? • So, to use our function machine, we put something in…. Input Function Output

  7. What is a function? • …turn the crank… Input Function Output

  8. What is a function? • …and something will come out! Input Function Output

  9. A Quick Example… • Let’s say we had a function machine that would square a number… SquareNum

  10. A Quick Example… • To use it, we would put a number in… 4 SquareNum

  11. A Quick Example… • …turn the crank… SquareNum

  12. A Quick Example… • …and the right answer will pop out! SquareNum 16

  13. Functions in JavaScript • Every function must have a name. • Inputs are called parameters or arguments • Some functions might not take any parameters. • Outputs are called return values • Some functions might not have a return value.

  14. Functions in JavaScript function SquareNum ( x ) { return (x * x); } Function name Parameter Return value

  15. Functions in JavaScript • A generic function definition: //comments go here – describe what it does function functionName(param1, param2, …) { …body of function… return (value); //more comments }

  16. Functions in JavaScript • Defining a function is not enough – we must call it to make it do something! • Defining a function is analogous to building a function machine. • Calling the function is analogous to turning the crank on the already-built machine. • To call a function, we just use its name! • Must also pass appropriate parameters.

  17. Functions in JavaScript • Example function call: …do something… SquareNum(5); …do some more stuff…

  18. Important Notes • Function calls must have the same number of parameters as the definition expects. • Parentheses are required, even if there are no parameters (this goes for definitions and calls!) • Function definitions don’t do anything. You have to call the function to make it do something. • Definitions go in the <HEAD> of your page, calls can be in the <HEAD> or in the <BODY>

  19. Why Use Functions? • Group statements together into a single logical unit: • Example: No built in sort in JavaScript. We are able write a Sort function, though! • Event handlers: • Call a function every time user clicks a button.

  20. Why Use Functions? • Dividing job into smaller pieces: • Makes it easier to write and test. • For jobs that we’ll do repeatedly: • Define the function once, then we can call it as much as we want! • Some examples • Mini-Lab 1!!!

More Related