Lecture 4: Defining Functions (Ch. 3.4-3.11) CS 1110 Introduction - - PowerPoint PPT Presentation

lecture 4 defining functions
SMART_READER_LITE
LIVE PREVIEW

Lecture 4: Defining Functions (Ch. 3.4-3.11) CS 1110 Introduction - - PowerPoint PPT Presentation

http://www.cs.cornell.edu/courses/cs1110/2019sp Lecture 4: Defining Functions (Ch. 3.4-3.11) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White] From last time:


slide-1
SLIDE 1

Lecture 4: Defining Functions

(Ch. 3.4-3.11) CS 1110 Introduction to Computing Using Python

[E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]

http://www.cs.cornell.edu/courses/cs1110/2019sp

slide-2
SLIDE 2

From last time: Function Calls

  • Function expressions have the form fun(x,y,…)
  • Examples (math functions that work in Python):

§ round(2.34) § max(a+3,24)

Let’s define our own functions!

2

function name argument

slide-3
SLIDE 3

Anatomy of a Function Definition

def increment(n): """Returns: the value of n+1""" return n+1

3

Function Header name parameters Docstring Specification Statements to execute when called also called Function Body The vertical line indicates indentation

Use vertical lines when you write Python

  • n exams so we can see indentation
slide-4
SLIDE 4

The return Statement

  • Passes a value from the function to the caller
  • Format: return <expression>
  • Any statements after return are ignored
  • Optional (if absent, special value None will be

sent back)

4

slide-5
SLIDE 5

Function Definitions vs. Calls

5

Function definition

  • Defines what function does
  • Declaration of parameter n
  • Parameter: the variable that is listed

within the parentheses of a function header. Function call

  • Command to do the function
  • Argument to assign to n
  • Argument: a value to assign to the

function parameter when it is called def increment(n): return n+1 increment(2) simple_math.py

slide-6
SLIDE 6

Executing the script simple_math.py

6

C:/> python simple_math.py

# simple_math.py """script that defines and calls one simple math function""” def increment(n): """Returns: n+1""" return n+1 increment(2) Python skips Python skips Python learns about the function Python skips everything inside the function Python executes this statement

Show in python tutor

(put an error inside fn) Now Python executes the function body

slide-7
SLIDE 7
  • Number of statement in the

function body to execute next

  • Starts with 1

Draw parameters as variables (named boxes)

Understanding How Functions Work

  • We will draw pictures to show what is in memory
  • Function Frame: Representation of function call

7

function name local variables (later in lecture) parameters instruction counter Note: slightly different than in the book (3.9) Please do it this way.

slide-8
SLIDE 8

Example: get_feet in height.py module

>>> import height >>> height.get_feet(68)

8

def get_feet(ht_in_inches): return ht_in_inches // 12 1

slide-9
SLIDE 9

Example: get_feet(68)

  • 1. Draw a frame for the call
  • 2. Assign the argument value

to the parameter (in frame)

  • 3. Indicate next line to execute

9

get_feet

1 next line to execute 1

PHASE 1: Set up call frame

ht_in_inches 68 def get_feet(ht_in_inches): return ht_in_inches // 12

slide-10
SLIDE 10

Example: get_feet(68)

10

get_feet

1 1

PHASE 2: Execute function body

Return statement creates a special variable for result

RETURN 5

def get_feet(ht_in_inches): return ht_in_inches // 12 ht_in_inches 68

slide-11
SLIDE 11

Example: get_feet(68)

11

get_feet

1 1 The return terminates; no next line to execute def get_feet(ht_in_inches): return ht_in_inches // 12

PHASE 2: Execute function body

ht_in_inches 68

RETURN 5

slide-12
SLIDE 12

get_feet

Example: get_feet(68)

12

1

PHASE 3: Erase call frame

def get_feet(ht_in_inches): return ht_in_inches // 12 ht_in_inches 68

RETURN 5

slide-13
SLIDE 13

Example: get_feet(68)

13

1

PHASE 3: Erase call frame

E R A S E W H O L E F R A M E

But don’t actually erase on an exam

def get_feet(ht_in_inches): return ht_in_inches // 12

slide-14
SLIDE 14

Local Variables (1)

  • Call frames can make “local” variables

>>> import height >>> height.get_feet(68)

14

def get_feet(ht_in_inches): feet = ht_in_inches // 12 return feet 1 2

get_feet

1 68 ht_in_inches

slide-15
SLIDE 15

get_feet

1 2

Local Variables (2)

  • Call frames can make “local” variables

>>> import height >>> height.get_feet(68)

15

1 2 def get_feet(ht_in_inches): feet = ht_in_inches // 12 return feet 68 ht_in_inches 5 feet

slide-16
SLIDE 16

Local Variables (3)

  • Call frames can make “local” variables

>>> import height >>> height.get_feet(68)

16

1 2 def get_feet(ht_in_inches): feet = ht_in_inches // 12 return feet

get_feet

1 2 68 ht_in_inches 5 feet

RETURN

5

slide-17
SLIDE 17

Local Variables (4)

  • Call frames can make “local” variables

>>> import height >>> height.get_feet(68)

17

1 2 E R A S E W H O L E F R A M E Variables are gone! This function is over. def get_feet(ht_in_inches): feet = ht_in_inches // 12 return feet

slide-18
SLIDE 18

Exercise Time

Function Definition def foo(a,b): x = a y = b return x*y+y Function Call

>>> foo(3,4)

18

What does the frame look like at the start?

1 2 3

slide-19
SLIDE 19

Which One is Closest to Your Answer?

A: B:

19

C: D:

foo 1 3 a 4 b 3 x foo 1 3 a 4 b x y foo 1 3 a 4 b foo 1 3 a 4 b a x

slide-20
SLIDE 20

And the answer is…

A: B:

20

C: D:

foo 1 3 a 4 b 3 x foo 1 3 a 4 b x y foo 1 3 a 4 b foo 1 3 a 4 b a x

slide-21
SLIDE 21

Exercise Time

Function Definition def foo(a,b): x = a y = b return x*y+y Function Call

>>> foo(3,4)

B:

21

1 2 3 foo 1 3 a 4 b

What is the next step?

slide-22
SLIDE 22

Which One is Closest to Your Answer?

A: B:

22

C: D:

foo 2 3 a 4 b 3 x foo 2 3 a 4 b 3 x y foo 2 3 a 4 b foo 1 3 a 4 b 3 x

slide-23
SLIDE 23

And the answer is…

A: B:

23

C: D:

foo 2 3 a 4 b 3 x foo 2 3 a 4 b 3 x y foo 2 3 a 4 b foo 1 3 a 4 b 3 x

slide-24
SLIDE 24

Exercise Time

Function Definition def foo(a,b): x = a y = b return x*y+y Function Call

>>> foo(3,4)

24

foo 2 3 a 4 b 3 x

What is the next step?

1 2 3

slide-25
SLIDE 25

Exercise Time

Function Definition def foo(a,b): x = a y = b return x*y+y Function Call

>>> foo(3,4)

25

foo 3 3 a 4 b 3 x 4 y

What is the next step?

1 2 3

slide-26
SLIDE 26

Which One is Closest to Your Answer?

A: B:

26

C: D:

foo 3 foo 3 a 4 b 3 x 4 y

RETURN

16 3 foo 3 a 4 b 3 x 4 y

RETURN

16 E R A S E T H E F R A M E

RETURN

16

slide-27
SLIDE 27

And the answer is…

A: B:

27

C: D:

foo 3 foo 3 a 4 b 3 x 4 y

RETURN

16 3 foo 3 a 4 b 3 x 4 y

RETURN

16 E R A S E T H E F R A M E

RETURN

16

slide-28
SLIDE 28

Exercise Time

Function Definition def foo(a,b): x = a y = b return x*y+y Function Call

>>> foo(3,4)

28

foo 3 a 4 b 3 x 4 y

RETURN

16

What is the next step?

1 2 3

slide-29
SLIDE 29

Exercise Time

Function Definition def foo(a,b): x = a y = b return x*y+y Function Call

>>> foo(3,4) >>> 16

29

E R A S E T H E F R A M E 1 2 3

slide-30
SLIDE 30

Function Access to Global Space

  • Top-most location in

memory called global space

  • Functions can access

anything in that global space

get_feet 1 2 68 ht_in_inches

Global Space

12 INCHES_PER_FT

30

INCHES_PER_FT = 12 … def get_feet(ht_in_inches): feet = ht_in_inches // INCHES_PER_FT return feet get_feet(68) get_feet 5 feet 1 2

slide-31
SLIDE 31

What about this??

  • What if you choose a local

variable inside a function that happens to also be a global variable?

get_feet 1 68 ht_in_inches

Global Space

12 INCHES_PER_FT

31

INCHES_PER_FT = 12 feet = “plural of foot” … def get_feet(ht_in_inches): feet = ht_in_inches // INCHES_PER_FT return feet get_feet(68) get_feet 1 2

“plural of foot”

feet

slide-32
SLIDE 32

Look, but don’t touch!

Can’t change global variables

“Assignment to a global” makes a new local variable! get_feet 1 2 68 ht_in_inches

Global Space

12 INCHES_PER_FT

32

INCHES_PER_FT = 12 feet = “plural of foot” … def get_feet(ht_in_inches): feet = ht_in_inches // INCHES_PER_FT return feet get_feet(68) get_feet 1 2

“plural of foot”

feet 5 feet