Week_5_Functions_2

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

Info iconThis 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
Background image of page 1

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

View Full DocumentRight 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
Background 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
Background image of page 3

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

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

This note was uploaded on 08/28/2011 for the course CMPT 125 taught by Professor Diana during the Spring '08 term at Simon Fraser.

Page1 / 9

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

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online