{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Week_5_Functions_2 - CMPT120 J Ye Functions(II Week 5...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CMPT120 J. Ye Functions (II) ( Week 5 ) Function Parameters and Arguments Function Return Values Variable Scope CMPT120 J. Ye 1 Function Components (last week) Function header keyword def function name (which should indicate the function’s purpose) Function parameter list (inputs to the function), if required colon Function body The statements to be executed when the function is called including a docstring on the first line of the function body Must be indented (like Python loops) Optional return statement (output from the function) 2
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CMPT120 J. Ye A function may have arguments (input to the function) and return value (output from the function) Note: here the term “output” (from a function) has nothing to do with the “output” from print statement. 3 The Conceptual Model of a Function (last week) FUNCTION f arguments (input) return value (output) Function Definitions (Declarations) Let’s now examine (Python) functions in more detail A function definition (declaration) specifies the code that will be executed when the function is invoked (called) When a function is invoked, the flow of control jumps to the function and executes its code When complete, the flow returns to the place where the function was invoked and continues See next slide for an example 4
Image of page 2
CMPT120 J. Ye If function A calls function B, then function A is the caller and function B is the callee If both the caller and callee functions are defined in the same .py file, e.g. compute and myFunction in the figure, when myFunction
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

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