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/2018sp 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] Things to Do Before


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/2018sp

slide-2
SLIDE 2

Things to Do Before Next Class

Readings:

§ Sections 8.1, 8.2, 8.4, 8.5, first paragraph of 8.9

Labs:

  • Go to Lab! (Lab 2 is this week)
  • Get Credit for Lab 1:

§ can be checked off during Tuesday's consulting hours 4:30-9:30 in the ACCEL lab § cannot be checked off after 3:45pm Wednesday § check online if you received credit:

http://www.cs.cornell.edu/courses/cs1110/ 2018sp/labs/index.php

2

slide-3
SLIDE 3

Check out Piazza Post @21

3

https://piazza.com/class/jckqwmqflaz6i?cid=21

slide-4
SLIDE 4

4

slide-5
SLIDE 5

5

slide-6
SLIDE 6
slide-7
SLIDE 7

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)

7

function name argument

slide-8
SLIDE 8

From last time: Modules

  • Modules provide extra functions, variables

§ Access them with the import command

  • Example: module math

>>> import math >>> math.cos(2.0)

  • 0.4161468365471424

>>> math.pi 3.141592653589793

8

slide-9
SLIDE 9

From last time: Modules

Module Text

# my_module.py """This is a simple module. It shows how modules work""" x = 1+2 x = 3*x

Interactive Python

>>> import my_module >>> my_module.x 9

9

  • We discussed how to make module variables
  • Have not covered how to make functions
slide-10
SLIDE 10

simple_math.py

>>> import simple_math >>> simple_math.increment(1) 2 >>> simple_math. increment(2) 3

10

slide-11
SLIDE 11

Anatomy of a Function Definition

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

11

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-12
SLIDE 12

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)

12

slide-13
SLIDE 13

Function Calls vs. Definitions

13

Function Call Function Definition

  • Command to do the function

>>> simple_math.increment(23) 24 >>>

  • Defines what function does

def increment(n): return n+1

  • Parameter: variable that is listed within

the parentheses of a function header.

  • Argument: a value to assign to the function

parameter when it is called

declaration of parameter n argument to assign to n

slide-14
SLIDE 14

# simple_math.py """module with two simple math functions""" def increment(n): """Returns: n+1""" return n+1

Using simple_math.py

Module Text Interactive Python

>>> import simple_math

14

Python skips Python skips Python learns the function definition Python skips everything inside the function Repeat for all functions in module

slide-15
SLIDE 15

# simple_math.py """module with two simple math functions""" def increment(n): """Returns: n+1""" return n+1

Using simple_math.py

Module Text Interactive Python

>>> import simple_math >>> simple_math.increment(23)

15

Python knows what this is! Now Python executes the function body

slide-16
SLIDE 16

# simple_math.py """module with two simple math functions""" def increment(n): """Returns: n+1""" return n+1

Using simple_math.py

Module Text Interactive Python

>>> import simple_math >>> simple_math.increment(23)

16

slide-17
SLIDE 17

# simple_math.py """module with two simple math functions""" def increment(n): """Returns: n+1""" return n+1

Using simple_math.py

Module Text Interactive Python

>>> import simple_math >>> simple_math.increment(23) 24

17

slide-18
SLIDE 18
  • 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

18

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-19
SLIDE 19

Example: get_feet

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

19

def get_feet(height_in_inches): return height_in_inches // INCHES_PER_FOOT 1

slide-20
SLIDE 20

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

20

get_feet

1 next line to execute 1

PHASE 1: Set up call frame

height_in_inches 68 def get_feet(height_in_inches): return height_in_inches // INCHES_PER_FOOT

slide-21
SLIDE 21

Example: get_feet(68)

21

get_feet

1 1

PHASE 2: Execute function body

Return statement creates a special variable for result

RETURN 5

def get_feet(height_in_inches): return height_in_inches // INCHES_PER_FOOT height_in_inches 68

slide-22
SLIDE 22

Example: get_feet(68)

22

get_feet

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

PHASE 2: Execute function body

height_in_inches 68

RETURN 5

slide-23
SLIDE 23

get_feet

Example: get_feet(68)

23

1

PHASE 3: Erase call frame

def get_feet(height_in_inches): return height_in_inches // INCHES_PER_FOOT height_in_inches 68

RETURN 5

slide-24
SLIDE 24

Example: get_feet(68)

24

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(height_in_inches): return height_in_inches // INCHES_PER_FOOT

slide-25
SLIDE 25

Local Variables (1)

  • Call frames can make “local” variables

>>> import room_numbers >>> room_numbers.lab_rooms()

25

lab_rooms

1 def lab_rooms(): red_room = 235

  • range_room = 236

1 2

slide-26
SLIDE 26

Local Variables (2)

  • Call frames can make “local” variables

>>> import room_numbers >>> room_numbers.lab_rooms()

26

lab_rooms

2 235 red_room 1 2 def lab_rooms(): red_room = 235

  • range_room = 236
slide-27
SLIDE 27

Local Variables (3)

  • Call frames can make “local” variables

>>> import room_numbers >>> room_numbers.lab_rooms()

27

lab_rooms

1 2 def lab_rooms(): red_room = 235

  • range_room = 236

235 red_room 236

  • range_room

RETURN NONE

slide-28
SLIDE 28

Local Variables (4)

  • Call frames can make “local” variables

>>> import room_numbers >>> room_numbers.lab_rooms()

28

1 2 E R A S E W H O L E F R A M E Variables are gone! This function is useless. def lab_rooms(): red_room = 235

  • range_room = 236
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)

29

What does the frame look like at the start?

1 2 3

slide-30
SLIDE 30

Which One is Closest to Your Answer?

A: B:

30

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-31
SLIDE 31

Exercise Time

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

>>> foo(3,4)

B:

31

1 2 3 foo 1 3 a 4 b

What is the next step?

slide-32
SLIDE 32

Which One is Closest to Your Answer?

A: B:

32

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-33
SLIDE 33

Exercise Time

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

>>> foo(3,4)

C:

33

foo 2 3 a 4 b 3 x

What is the next step?

1 2 3

slide-34
SLIDE 34

Exercise Time

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

>>> foo(3,4)

34

foo 3 3 a 4 b 3 x 4 y

What is the next step?

1 2 3

slide-35
SLIDE 35

Which One is Closest to Your Answer?

A: B:

35

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-36
SLIDE 36

Exercise Time

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

>>> foo(3,4)

C:

36

foo 3 a 4 b 3 x 4 y

RETURN

16

What is the next step?

1 2 3

slide-37
SLIDE 37

Exercise Time

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

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

37

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

slide-38
SLIDE 38

Function Access to Global Space

  • All function definitions

are in some module

  • Call can access global space

for that module

§ math.cos: global for math § height.get_feet uses global for height

  • But cannot change values

§ “Assignment to a global” makes a new local variable! change_a 3.5 a

# scope_example.py """Show how globals work""" a = 4 # global space def change_a(): a = 3.5 # local variable return a Global Space (for scope_example.py)

4 a

38

slide-39
SLIDE 39

Global Space (for scope_example.py)

Function Access to Global Space

  • All function definitions

are in some module

  • Call can access global space

for that module

§ math.cos: global for math § height.get_feet uses global for height

  • But cannot change values

§ Assignment to a global makes a new local variable! get_a 1 4 a

# scope_example.py """Show how globals work""" a = 4 # global space def get_a(): return a # returns global

39

slide-40
SLIDE 40

Call Frames and Global Variables

The specification is a lie:

def swap(a,b): """Swap global a & b""" tmp = a a = b b = tmp

>>> a = 1 >>> b = 2 >>> swap(a,b)

40

1 a 2 b 1 2 3 swap 1 1 a 2 b

Global Variables Call Frame

slide-41
SLIDE 41

Call Frames and Global Variables

The specification is a lie:

def swap(a,b): """Swap global a & b""" tmp = a a = b b = tmp

>>> a = 1 >>> b = 2 >>> swap(a,b)

41

1 a 2 b swap 2 1 a 2 b

Global Variables Call Frame

1 tmp 1 2 3

slide-42
SLIDE 42

Call Frames and Global Variables

The specification is a lie:

def swap(a,b): """Swap global a & b""" tmp = a a = b b = tmp

>>> a = 1 >>> b = 2 >>> swap(a,b)

42

1 a 2 b swap 3 1 a 2 b

Global Variables Call Frame

1 tmp 2 1 2 3

x

slide-43
SLIDE 43

Call Frames and Global Variables

The specification is a lie:

def swap(a,b): """Swap global a & b""" tmp = a a = b b = tmp

>>> a = 1 >>> b = 2 >>> swap(a,b)

43

1 a 2 b swap 1 a 2 b

Global Variables Call Frame

1 tmp 2 1 1 2 3

x x

slide-44
SLIDE 44

Call Frames and Global Variables

The specification is a lie:

def swap(a,b): """Swap global a & b""" tmp = a a = b b = tmp

>>> a = 1 >>> b = 2 >>> swap(a,b)

44

1 a 2 b

Global Variables Call Frame

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

slide-45
SLIDE 45

Call Frames and Global Variables

The specification is a lie:

def swap(a,b): """Swap global a & b""" tmp = a a = b b = tmp

>>> a = 1 >>> b = 2 >>> swap(a,b)

45

1 a 2 b

Global Variables Call Frame

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

THIS FUNCTION DOES NOT SWAP the global a and global b

slide-46
SLIDE 46

Visualizing Frames: The Python Tutor

46

slide-47
SLIDE 47

More Exercises

Module Text

# my_module.py def foo(x): return x+1 x = 1+2 x = 3*x

Interactive Python

>>> import my_module >>> my_module.x …

47

A: 9 B: 10 C: 1 D: Nothing E: Error What does Python give me? CORRECT