Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function...

32
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

Transcript of Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function...

Page 1: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

Lecture 4: Defining Functions

(Ch. 3.4-3.11)

CS 1110Introduction 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

Page 2: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

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

functionname

argument

Page 3: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

Anatomy of a Function Definition

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

3

Function Header

name parameters

DocstringSpecification

Statements to execute when calledalso called Function Body

The vertical line indicates indentation

Use vertical lines when you write Python on exams so we can see indentation

Page 4: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

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

Page 5: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

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

Page 6: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

Executing the script simple_math.py

6C:/> 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

Page 7: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

• Number of statement in thefunction body to execute next

• Starts with 1

Draw parametersas 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 thisway.

Page 8: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

Example: get_feet in height.py module

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

8

def get_feet(ht_in_inches):return ht_in_inches // 121

Page 9: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

Example: get_feet(68)

1. Draw a frame for the call2. 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

Page 10: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

Example: get_feet(68)

10

get_feet 1

1

PHASE 2: Execute function bodyReturn statement creates a special variable for result

RETURN 5

def get_feet(ht_in_inches):return ht_in_inches // 12

ht_in_inches 68

Page 11: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

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

Page 12: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

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

Page 13: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

Example: get_feet(68)

13

1

PHASE 3: Erase call frame

ERASE WHOLE FRAMEBut don’t actually erase on an exam

def get_feet(ht_in_inches):return ht_in_inches // 12

Page 14: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

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 // 12return feet

12

get_feet 1

68ht_in_inches

Page 15: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

get_feet 1 2

Local Variables (2)

• Call frames can make “local” variables>>> import height>>> height.get_feet(68)

15

12

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

68ht_in_inches

5feet

Page 16: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

Local Variables (3)

• Call frames can make “local” variables>>> import height>>> height.get_feet(68)

16

12

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

get_feet 1 2

68ht_in_inches

5feet

RETURN 5

Page 17: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

Local Variables (4)

• Call frames can make “local” variables>>> import height>>> height.get_feet(68)

17

12

ERASE WHOLE FRAME

Variables are gone! This function is over.

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

Page 18: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

Exercise Time

Function Definition

def foo(a,b):x = ay = breturn x*y+y

Function Call

>>> foo(3,4)

18

What does the frame look like

at the start?

12

3

Page 19: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

Which One is Closest to Your Answer?

A: B:

19

C: D:foo 1

3a 4b

3x

foo 1

3a 4b

x y

foo 1

3a 4b

foo 1

3a 4b

ax

Page 20: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

And the answer is…

A: B:

20

C: D:foo 1

3a 4b

3x

foo 1

3a 4b

x y

foo 1

3a 4b

foo 1

3a 4b

ax �

Page 21: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

Exercise Time

Function Definition

def foo(a,b):x = ay = breturn x*y+y

Function Call

>>> foo(3,4)B:

21

12

3

foo 1

3a 4b

What is the next step?

Page 22: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

Which One is Closest to Your Answer?

A: B:

22

C: D:foo 2

3a 4b

3x

foo 2

3a 4b

3x y

foo 2

3a 4b

foo 1

3a 4b

3x

Page 23: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

And the answer is…

A: B:

23

C: D:foo 2

3a 4b

3x

foo 2

3a 4b

3x y

foo 2

3a 4b

foo 1

3a 4b

3x

Page 24: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

Exercise Time

Function Definition

def foo(a,b):x = ay = breturn x*y+y

Function Call

>>> foo(3,4)

24

foo 2

3a 4b

3x

What is the next step?

12

3

Page 25: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

Exercise Time

Function Definition

def foo(a,b):x = ay = breturn x*y+y

Function Call

>>> foo(3,4)

25

foo 3

3a 4b

3x 4y

What is the next step?

12

3

Page 26: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

Which One is Closest to Your Answer?

A: B:

26

C: D:

foo 3 foo3a 4b

3x 4y

RETURN 16

3

foo3a 4b

3x 4y

RETURN 16 ERASE THE FRAME

RETURN 16

Page 27: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

And the answer is…

A: B:

27

C: D:

foo 3 foo3a 4b

3x 4y

RETURN 16

3

foo3a 4b

3x 4y

RETURN 16 ERASE THE FRAME

RETURN 16

Page 28: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

Exercise Time

Function Definition

def foo(a,b):x = ay = breturn x*y+y

Function Call

>>> foo(3,4)

28

foo3a 4b

3x 4y

RETURN 16

What is the next step?

12

3

Page 29: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

Exercise Time

Function Definition

def foo(a,b):x = ay = breturn x*y+y

Function Call

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

29

ERASE THE FRAME

12

3

Page 30: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

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

68ht_in_inches

Global Space

12INCHES_PER_FT

30

INCHES_PER_FT = 12…

def get_feet(ht_in_inches):feet = ht_in_inches // INCHES_PER_FTreturn feet

get_feet(68)

get_feet

5feet12

Page 31: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

What about this??

• What if you choose a local variable inside a function that happens to also be a global variable?

get_feet 1

68ht_in_inches

Global Space

12INCHES_PER_FT

31

INCHES_PER_FT = 12feet = “plural of foot”…

def get_feet(ht_in_inches):feet = ht_in_inches // INCHES_PER_FTreturn feet

get_feet(68)

get_feet

12

“plural of foot”feet

Page 32: Lecture 4: Defining Functions - Cornell University · From last time: Function Calls •Function expressions have the form fun(x,y,…) •Examples(math functions that work in Python):

Look, but don’t touch!

Can’t change global variables“Assignment to a global” makes a new local variable!

get_feet 1 2

68ht_in_inches

Global Space

12INCHES_PER_FT

32

INCHES_PER_FT = 12feet = “plural of foot”…

def get_feet(ht_in_inches):feet = ht_in_inches // INCHES_PER_FTreturn feet

get_feet(68)

get_feet

12

“plural of foot”feet

5feet