chapters 5-7 notes.docx - A function is a group of...

This preview shows page 1 - 3 out of 12 pages.

A function is a group of statements that exist within a program for the purpose of performing a specific task. Instead of one long sequence, can be written as several small functions performing a specific part of the task. (165-66) divide and conquer Modularized program: program written with each task in its own function Benefits of Modularizing: Simpler Code, Code Reuse, Better Testing, Faster Development, Easier Facilitation of Teamwork (167) Void function: executes the statements that it contains and then terminates it Value-returning Function: executes the statement that it contains and then returns the value back to the statement that called it (example: input, int, float) Function Definition: the code for a function. Execute the function by writing a statement that calls it. Variable name rules (168) Use verbs in function names to indicate what the function is doing Function Definition Layout (168) First line, function header: marks beginning of function definition. Begins with “def” followed by name of function, parenthesis, and a colon Next line begins block: a set of statements that belong together in a group. Preformed anytime the function is executed, indented b/c tells where it begins and ends Call a function to execute it; jumps to function and executes block. When done, goes back to where function was called and continues execution at that point. (it “returns”) (169) Main function calls other functions as needed; contains program’s mainline logic: overall logic of program(170) Calling function examples (170-172) Can use tab and spacebar to indent but not both Top Down Design to break down algorithms into functions (174-175) Structure Chart: uses boxes that represent each function of program, connected as to show functions called by each function. Defining and Calling Functions examples (175) Pause execution until user presses enter Local Variable: created inside a function and cannot be accessed by statements that are outside the function. Different functions can have local variables with the same names b/c functions cannot see each other’s local variables. (179)
Image of page 1

Subscribe to view the full document.

Scope: part of the program in which the variable may be accessed; visible only to statements in the variable’s scope An argument is any piece of data that is passed into a function when the function is called. A parameter is a variable that receives an argument that is passed into a function. (181) Parameter variable’s scope is the function in which the parameter is used. All statements inside function can access, but no statement outside can.(183) Function w/ multiple arguments (185) Parameter List: inside () in function header Changes made to parameter variable will not affect the argument(example) (187) Pass by Value: when a function cannot change the value of an argument that was passed to it.
Image of page 2
Image of page 3
  • Spring '17

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern