Defining Functions Academic Integrity Quiz Remember : quiz about - - PowerPoint PPT Presentation

defining functions academic integrity quiz
SMART_READER_LITE
LIVE PREVIEW

Defining Functions Academic Integrity Quiz Remember : quiz about - - PowerPoint PPT Presentation

Lecture 4 Defining Functions Academic Integrity Quiz Remember : quiz about the course AI policy Have posted grades for completed quizes Right now, missing ~90 enrolled students If did not receive perfect, take it again If you are


slide-1
SLIDE 1

Defining Functions

Lecture 4

slide-2
SLIDE 2

Academic Integrity Quiz

  • Remember: quiz about the course AI policy

§ Have posted grades for completed quizes § Right now, missing ~90 enrolled students § If did not receive perfect, take it again

  • If you are not aware of the quiz

§ Go to http://www.cs.cornell.edu/courses/cs11110/ § Click Academic Integrity in side bar § Read and take quiz in CMS

8/31/17 Defining Functions 2

slide-3
SLIDE 3

Recall: Modules

  • Modules provide extra functions, variables

§ Example: math provides math.cos(), math.pi § Access them with the import command

  • Python provides a lot of them for us
  • This Lecture: How to make modules

§ Komodo Edit to make a module § Python to use the module

8/31/17 Defining Functions 3

Two different programs

slide-4
SLIDE 4

We Write Programs to Do Things

  • Functions are the key doers

8/31/17 Defining Functions 4

Function Call Function Definition

  • Command to do the function

>>> plus(23) 24 >>>

  • Defines what function does

def plus(n): return n+1

  • Parameter: variable that is listed within

the parentheses of a method header.

  • Argument: a value to assign to the method

parameter when it is called

slide-5
SLIDE 5

We Write Programs to Do Things

  • Functions are the key doers

8/31/17 Defining Functions 5

Function Call Function Definition

  • Command to do the function

>>> plus(23) 24 >>>

  • Defines what function does

def plus(n): return n+1

  • Parameter: variable that is listed within

the parentheses of a method header.

  • Argument: a value to assign to the method

parameter when it is called

Function Header

slide-6
SLIDE 6

We Write Programs to Do Things

  • Functions are the key doers

8/31/17 Defining Functions 6

Function Call Function Definition

  • Command to do the function

>>> plus(23) 24 >>>

  • Defines what function does

def plus(n): return n+1

  • Parameter: variable that is listed within

the parentheses of a method header.

  • Argument: a value to assign to the method

parameter when it is called

Function Header Function Body (indented)

slide-7
SLIDE 7

We Write Programs to Do Things

  • Functions are the key doers

8/31/17 Defining Functions 7

Function Call Function Definition

  • Command to do the function

>>> plus(23) 24 >>>

  • Defines what function does

def plus(n): return n+1

  • Parameter: variable that is listed within

the parentheses of a method header.

  • Argument: a value to assign to the method

parameter when it is called

Function Header Function Body (indented) declaration of parameter n argument to assign to n

slide-8
SLIDE 8

Anatomy of a Function Definition

def plus(n): """Returns the number n+1 Parameter n: number to add to Precondition: n is a number""" x = n+1 return x

8/31/17 Defining Functions 8

Function Header name parameters Docstring Specification Statements to execute when called

slide-9
SLIDE 9

Anatomy of a Function Definition

def plus(n): """Returns the number n+1 Parameter n: number to add to Precondition: n is a number""" x = n+1 return x

8/31/17 Defining Functions 9

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

Use vertical lines when you write Python

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

The return Statement

  • Format: return <expression>

§ Used to evaluate function call (as an expression) § Also stops executing the function! § Any statements after a return are ignored

  • Example: temperature converter function

def to_centigrade(x): """Returns: x converted to centigrade""" return 5*(x-32)/9.0

8/31/17 Defining Functions 10

slide-11
SLIDE 11

A More Complex Example

Function Definition def foo(a,b): """Return something Param a: number Param b: number""" x = a y = b return x*y+y Function Call >>> x = 2 >>> foo(3,4)

8/31/17 Defining Functions 11

? x

What is in the box?

slide-12
SLIDE 12

A More Complex Example

Function Definition def foo(a,b): """Return something Param a: number Param b: number""" x = a y = b return x*y+y Function Call >>> x = 2 >>> foo(3,4)

8/31/17 Defining Functions 12

? x

What is in the box?

A: 2 B: 3 C: 16 D: Nothing! E: I do not know

slide-13
SLIDE 13

A More Complex Example

Function Definition def foo(a,b): """Return something Param a: number Param b: number""" x = a y = b return x*y+y Function Call >>> x = 2 >>> foo(3,4)

8/31/17 Defining Functions 13

? x

What is in the box?

A: 2 B: 3 C: 16 D: Nothing! E: I do not know CORRECT

slide-14
SLIDE 14
  • Number of statement in the

function body to execute next

  • Starts with 1

Draw parameters as variables (named boxes)

Understanding How Functions Work

  • Function Frame: Representation of function call
  • A conceptual model of Python

8/31/17 Defining Functions 14

function name local variables (later in lecture) parameters instruction counter

slide-15
SLIDE 15

Text (Section 3.10) vs. Class

Textbook This Class

8/31/17 Defining Functions 15

def to_centigrade(x): return 5*(x-32)/9.0

Call: to_centigrade(50.0) Definition:

to_centigrade 1 50.0 x to_centigrade

x –> 50.0

slide-16
SLIDE 16

Example: to_centigrade(50.0)

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

to the parameter (in frame)

  • 3. Execute the function body

§ Look for variables in the frame § If not there, look for global variables with that name

  • 4. Erase the frame for the call

8/31/17 Defining Functions 16

def to_centigrade(x): return 5*(x-32)/9.0 to_centigrade 1 x Initial call frame (before exec body) next line to execute 1 50.0

slide-17
SLIDE 17

Example: to_centigrade(50.0)

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

to the parameter (in frame)

  • 3. Execute the function body

§ Look for variables in the frame § If not there, look for global variables with that name

  • 4. Erase the frame for the call

8/31/17 Defining Functions 17

def to_centigrade(x): return 5*(x-32)/9.0 to_centigrade 50.0 x Executing the return statement Return statement creates a special variable for result 1

RETURN 10.0

slide-18
SLIDE 18

Example: to_centigrade(50.0)

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

to the parameter (in frame)

  • 3. Execute the function body

§ Look for variables in the frame § If not there, look for global variables with that name

  • 4. Erase the frame for the call

8/31/17 Defining Functions 18

def to_centigrade(x): return 5*(x-32)/9.0 to_centigrade 50.0 x Executing the return statement The return terminates; no next line to execute 1

RETURN 10.0

slide-19
SLIDE 19

Example: to_centigrade(50.0)

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

to the parameter (in frame)

  • 3. Execute the function body

§ Look for variables in the frame § If not there, look for global variables with that name

  • 4. Erase the frame for the call

8/31/17 Defining Functions 19

def to_centigrade(x): return 5*(x-32)/9.0 But don’t actually erase on an exam 1

slide-20
SLIDE 20

Call Frames vs. 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)

8/31/17 Defining Functions 20

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

Global Variables Call Frame

slide-21
SLIDE 21

Call Frames vs. 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)

8/31/17 Defining Functions 21

1 a 2 b swap 2 1 a 2 b

Global Variables Call Frame

1 tmp 1 2 3

slide-22
SLIDE 22

Call Frames vs. 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)

8/31/17 Defining Functions 22

1 a 2 b swap 3 1 a 2 b

Global Variables Call Frame

1 tmp 2 1 2 3

x

slide-23
SLIDE 23

Call Frames vs. 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)

8/31/17 Defining Functions 23

1 a 2 b swap 1 a 2 b

Global Variables Call Frame

1 tmp 2 1 1 2 3

x x

slide-24
SLIDE 24

Call Frames vs. 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)

8/31/17 Defining Functions 24

1 a 2 b

Global Variables Call Frame

1 2 3

slide-25
SLIDE 25

Global Space (for globals.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 § temperature.to_centigrade uses global for temperature

  • But cannot change values

§ Assignment to a global makes a new local variable! § Why we limit to constants

8/31/17 Defining Functions 25

get_a 1 4 a

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

slide-26
SLIDE 26

Global Space (for globals.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 § temperature.to_centigrade uses global for temperature

  • But cannot change values

§ Assignment to a global makes a new local variable! § Why we limit to constants

8/31/17 Defining Functions 26

change_a 3.5 a 4 a

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

slide-27
SLIDE 27

Exercise Time

Function Definition def foo(a,b): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y Function Call

>>> x = foo(3,4)

8/31/17 Defining Functions 27

1 2 3

What does the frame look like at the start?

slide-28
SLIDE 28

Which One is Closest to Your Answer?

A: B:

8/31/17 Defining Functions 28

C: D:

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

slide-29
SLIDE 29

Which One is Closest to Your Answer?

A: B:

8/31/17 Defining Functions 29

C: D:

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

E:

¯\_(ツ)_/¯

slide-30
SLIDE 30

Exercise Time

Function Definition def foo(a,b): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y Function Call

>>> x = foo(3,4)

B:

8/31/17 Defining Functions 30

1 2 3 foo 1 3 a 4 b

slide-31
SLIDE 31

Exercise Time

Function Definition def foo(a,b): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y Function Call

>>> x = foo(3,4)

B:

8/31/17 Defining Functions 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:

8/31/17 Defining Functions 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): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y Function Call

>>> x = foo(3,4)

C:

8/31/17 Defining Functions 33

1 2 3 foo 2 3 a 4 b 3 x

slide-34
SLIDE 34

Exercise Time

Function Definition def foo(a,b): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y Function Call

>>> x = foo(3,4)

C:

8/31/17 Defining Functions 34

1 2 3 foo 2 3 a 4 b 3 x

What is the next step?

slide-35
SLIDE 35

Which One is Closest to Your Answer?

A: B:

8/31/17 Defining Functions 35

C: D:

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

RETURN

3 foo 3 a 4 b 3 x 4 y

RETURN

16

slide-36
SLIDE 36

Exercise Time

Function Definition def foo(a,b): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y Function Call

>>> x = foo(3,4)

A:

8/31/17 Defining Functions 36

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

slide-37
SLIDE 37

Exercise Time

Function Definition def foo(a,b): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y Function Call

>>> x = foo(3,4)

A:

8/31/17 Defining Functions 37

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

What is the next step?

slide-38
SLIDE 38

Which One is Closest to Your Answer?

A: B:

8/31/17 Defining Functions 38

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

RETURN

16

slide-39
SLIDE 39

Exercise Time

Function Definition def foo(a,b): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y Function Call

>>> x = foo(3,4)

C:

8/31/17 Defining Functions 39

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

RETURN

16

slide-40
SLIDE 40

Exercise Time

Function Definition def foo(a,b): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y Function Call

>>> x = foo(3,4)

C:

8/31/17 Defining Functions 40

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

RETURN

16

What is the next step?

slide-41
SLIDE 41

Which One is Closest to Your Answer?

A: B:

8/31/17 Defining Functions 41

C: D:

16 x foo foo 16 x

RETURN

16

slide-42
SLIDE 42

Exercise Time

Function Definition def foo(a,b): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y Function Call

>>> x = foo(3,4)

D:

8/31/17 Defining Functions 42

1 2 3 16 x

slide-43
SLIDE 43

Exercise Time

Function Definition def foo(a,b): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y Function Call

>>> x = foo(3,4)

D:

8/31/17 Defining Functions 43

1 2 3 16 x Variable in global space

slide-44
SLIDE 44

Visualizing Frames: The Python Tutor

8/31/17 Defining Functions 44

slide-45
SLIDE 45

Visualizing Frames: The Python Tutor

8/31/17 Defining Functions 45

Global Space Call Frame

slide-46
SLIDE 46

Visualizing Frames: The Python Tutor

8/31/17 Defining Functions 46

Global Space Call Frame

Variables from second lecture go in here

slide-47
SLIDE 47

Visualizing Frames: The Python Tutor

8/31/17 Defining Functions 47

Missing line numbers!

slide-48
SLIDE 48

Visualizing Frames: The Python Tutor

8/31/17 Defining Functions 48

Missing line numbers! Line number marked here (sort-of)

slide-49
SLIDE 49

Next Time: Concrete Examples

8/31/17 Defining Functions 49