1 / 19

Writing Functions

Writing Functions. The Big Idea. What if this was how we did powers?. Information. F unction header specifies everything but job of function A contract to do work. Review. A function declaration : Prototype Statements that do work of the function. A Function. A function:

fisseha
Download Presentation

Writing 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. Writing Functions

  2. The Big Idea • What if this was how we did powers?

  3. Information • Functionheader specifies everything but job of function • A contract to do work

  4. Review • A functiondeclaration : • Prototype • Statements that do work of the function

  5. A Function • A function: -Called max -Takes two inputs (ints) -Gives back an int

  6. Using Max • Using Max:

  7. Full Program • Must declare afunction beforeit is used

  8. Function Name • Function name • Normal identifier rules • no spaces, can't start with number, etc… • Use camelCase • Clearly identify job • Readability > terseness • Focus on verbs

  9. Body • Body of a function – where magic happens • Always a block of code { } • Has own scope • Variables declared inside only available inside

  10. Parameters • Parameters : information function needs • Declare with: type name, type name, type name… • OK to have no parametersintgetHourOfDay()…

  11. Parameters • Parameters are special variables • Only exist within this function • Initialized with value caller passes in:abs(10)  number = 10

  12. Function type • returnstatement used to pass answer back • Must return right type of data

  13. Function type • returnstatement used to pass answer back • OK to have more than one return • Every path must return

  14. Void Functions • Void : nothing • Void function type returns nothing • Just does some work • Returns at }

  15. Call Stacks • Each function works in a separate stack frame in memory

  16. Rules For Functions • Only variable available are: • Parameters • New variables you declare in function

  17. Rules For Functions • Do NOT use cin/cout

  18. Rules For Functions • Do NOT use cin/cout • Unless job of function is to print or get input

  19. Comparison Return a grade: Print a grade:

More Related