lecture 4 - cornell university · 2020. 1. 28. · recall: modules • modules provide extra...

48
Defining Functions Lecture 4

Upload: others

Post on 18-Sep-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Defining Functions

Lecture 4

Page 2: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Academic Integrity Quiz

• Remember: quiz about the course AI policy§ Have posted grades for completed quizes§ Right now, missing ~21 enrolled students§ If did not receive 9/10 (~70 students), take it again

• If you are not aware of the quiz§ Go to http://www.cs.cornell.edu/courses/cs1110/§ Click Academic Integrity under Assessments§ Read and take quiz in CMS

9/10/19 Defining Functions 2

Page 3: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

The ACCEL Drama Continues

• Now hear construction going into October§ This is best case; who knows when done

• We are working on long term plans§ Section 211 permanently moved to Phillips 318§ Sunday Consulting Hours in Upson 216§ Mon-Thur Consulting Hours in Upson 255

• Will update website when finalized§ Choose menu INFO > OFFICE HOURS

9/10/19 Defining Functions 3

Page 4: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

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§ Atom Editor to make a module

§ Python to use the module

9/10/19 Defining Functions 4

Two differentprograms

Page 5: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

We Write Programs to Do Things

• Functions are the key doers

9/10/19 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

Page 6: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

We Write Programs to Do Things

• Functions are the key doers

9/10/19 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

FunctionHeader

Page 7: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

We Write Programs to Do Things

• Functions are the key doers

9/10/19 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

FunctionHeader Function

Body(indented)

Page 8: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

We Write Programs to Do Things

• Functions are the key doers

9/10/19 Defining Functions 8

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

FunctionHeader Function

Body(indented)declaration of

parameter nargument to assign to n

Page 9: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

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+1return x

9/10/19 Defining Functions 9

Function Header

name parameters

DocstringSpecification

Statements to execute when called

Page 10: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

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+1return x

9/10/19 Defining Functions 10

Function Header

name parameters

DocstringSpecification

Statements to execute when called

The vertical line indicates indentation

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

Page 11: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

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 functiondef to_centigrade(x):

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

9/10/19 Defining Functions 11

Page 12: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

A More Complex Example

Function Definition

def foo(a,b):"""Return something

Param a: numberParam b: number"""

x = ay = breturn x*y+y

Function Call

>>> x = 2>>> foo(3,4)

9/10/19 Defining Functions 12

?x

What is in the box?

Page 13: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

A More Complex Example

Function Definition

def foo(a,b):"""Return something

Param a: numberParam b: number"""

x = ay = breturn x*y+y

Function Call

>>> x = 2>>> foo(3,4)

9/10/19 Defining Functions 13

?x

What is in the box?

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

Page 14: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

A More Complex Example

Function Definition

def foo(a,b):"""Return something

Param a: numberParam b: number"""

x = ay = breturn x*y+y

Function Call

>>> x = 2>>> foo(3,4)

9/10/19 Defining Functions 14

?x

What is in the box?

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

CORRECT

Page 15: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

• Number of statement in thefunction body to execute next

• Starts with line no. of body

Draw parametersas variables (named boxes)

Understanding How Functions Work

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

9/10/19 Defining Functions 15

function name

local variables (later in lecture)

parameters

instruction counter

Page 16: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Text (Section 3.10) vs. Class

Textbook This Class

9/10/19 Defining Functions 16

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

Call: to_centigrade(50.0)Definition:

to_centigrade 1

50.0xto_centigrade x –> 50.0

1

Page 17: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Example: to_centigrade(50.0)

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

9/10/19 Defining Functions 17

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

to_centigrade 1

x

Initial call frame(before exec body)

next line to execute1

50.0

Page 18: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Example: to_centigrade(50.0)

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

9/10/19 Defining Functions 18

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

to_centigrade

50.0x

Executing thereturn statement

Return statement creates a special variable for result1

RETURN 10.0

Page 19: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Example: to_centigrade(50.0)

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

9/10/19 Defining Functions 19

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

to_centigrade

50.0x

Executing thereturn statement

The return terminates;no next line to execute1

RETURN 10.0

Page 20: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Example: to_centigrade(50.0)

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

9/10/19 Defining Functions 20

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

ERASE WHOLE FRAME

But don’t actually erase on an exam1

Page 21: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Call Frames vs. Global Variables

The specification is a lie:def swap(a,b):

"""Swap global a & b"""tmp = aa = bb = tmp

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

9/10/19 Defining Functions 21

1a 2b

123 swap 1

1a 2b

Global Variables

Call Frame

Page 22: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Call Frames vs. Global Variables

The specification is a lie:def swap(a,b):

"""Swap global a & b"""tmp = aa = bb = tmp

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

9/10/19 Defining Functions 22

1a 2b

swap 2

1a 2b

Global Variables

Call Frame

1tmp

123

Page 23: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Call Frames vs. Global Variables

The specification is a lie:def swap(a,b):

"""Swap global a & b"""tmp = aa = bb = tmp

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

9/10/19 Defining Functions 23

1a 2b

swap 3

1a 2b

Global Variables

Call Frame

1tmp

2

123

x

Page 24: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Call Frames vs. Global Variables

The specification is a lie:def swap(a,b):

"""Swap global a & b"""tmp = aa = bb = tmp

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

9/10/19 Defining Functions 24

1a 2b

swap

1a 2b

Global Variables

Call Frame

1tmp

2 1

123

x x

Page 25: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Call Frames vs. Global Variables

The specification is a lie:def swap(a,b):

"""Swap global a & b"""tmp = aa = bb = tmp

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

9/10/19 Defining Functions 25

1a 2b

Global Variables

Call Frame123 ERASE THE FRAME

Page 26: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Exercise Time

Function Definition

def foo(a,b):"""Return something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)

9/10/19 Defining Functions 26

12

3

What does the frame look like

at the start?

Page 27: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Which One is Closest to Your Answer?

A: B:

9/10/19 Defining Functions 27

C: D:foo 1

3a 4b

3x

foo 1

3a 4b

x y

foo 0

3a 4b

foo 1

3a 4b

Page 28: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Which One is Closest to Your Answer?

A: B:

9/10/19 Defining Functions 28

C: D:foo 1

3a 4b

3x

foo 1

3a 4b

x y

foo 0

3a 4b

foo 1

3a 4b

E:

¯\_(ツ)_/¯

Page 29: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Exercise Time

Function Definition

def foo(a,b):"""Return something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)B:

9/10/19 Defining Functions 29

12

3

foo 1

3a 4b

Page 30: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Exercise Time

Function Definition

def foo(a,b):"""Return something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)B:

9/10/19 Defining Functions 30

12

3

foo 1

3a 4b

What is the next step?

Page 31: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Which One is Closest to Your Answer?

A: B:

9/10/19 Defining Functions 31

C: D:foo 2

3a 4b

3x

foo 2

3a 4b

3x y

foo 2

3a 4b

foo 1

3a 4b

3x

Page 32: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Exercise Time

Function Definition

def foo(a,b):"""Return something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)C:

9/10/19 Defining Functions 32

12

3

foo 2

3a 4b

3x

Page 33: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Exercise Time

Function Definition

def foo(a,b):"""Return something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)C:

9/10/19 Defining Functions 33

12

3

foo 2

3a 4b

3x

What is the next step?

Page 34: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Which One is Closest to Your Answer?

A: B:

9/10/19 Defining Functions 34

C: D:

foo 3

3a 4b

3x 4y

foo

3a 4b

3x 4y

RETURN

3

foo

3a 4b

3x 4y

RETURN 16

ERASE THE FRAME

Page 35: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Exercise Time

Function Definition

def foo(a,b):"""Return something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)A:

9/10/19 Defining Functions 35

12

3

foo 3

3a 4b

3x 4y

Page 36: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Exercise Time

Function Definition

def foo(a,b):"""Return something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)A:

9/10/19 Defining Functions 36

12

3

foo 3

3a 4b

3x 4y

What is the next step?

Page 37: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Which One is Closest to Your Answer?

A: B:

9/10/19 Defining Functions 37

C: D:

foo 3 foo

3a 4b

3x 4y

RETURN 16

3

foo

3a 4b

3x 4y

RETURN 16

ERASE THE FRAME

RETURN 16

Page 38: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Exercise Time

Function Definition

def foo(a,b):"""Return something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)C:

9/10/19 Defining Functions 38

12

3

foo

3a 4b

3x 4y

RETURN 16

Page 39: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Exercise Time

Function Definition

def foo(a,b):"""Return something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)C:

9/10/19 Defining Functions 39

12

3

foo

3a 4b

3x 4y

RETURN 16

What is the next step?

Page 40: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Which One is Closest to Your Answer?

A: B:

9/10/19 Defining Functions 40

C: D:16x

foo

foo

16x

RETURN 16

ERASE THE FRAME

ERASE THE FRAME

Page 41: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Exercise Time

Function Definition

def foo(a,b):"""Return something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)D:

9/10/19 Defining Functions 41

12

3

16xERASE THE FRAME

Page 42: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Exercise Time

Function Definition

def foo(a,b):"""Return something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)D:

9/10/19 Defining Functions 42

12

3

16xERASE THE FRAME

Variable in global space

Page 43: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Visualizing Frames: The Python Tutor

9/10/19 Defining Functions 43

Page 44: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Visualizing Frames: The Python Tutor

9/10/19 Defining Functions 44

Global Space

Call Frame

Page 45: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Visualizing Frames: The Python Tutor

9/10/19 Defining Functions 45

Global Space

Call Frame

Variables from second lecture

go in here

Page 46: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Visualizing Frames: The Python Tutor

9/10/19 Defining Functions 46

Missing line numbers!

Page 47: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Visualizing Frames: The Python Tutor

9/10/19 Defining Functions 47

Missing line numbers!

Line numbermarked here

(sort-of)

Page 48: Lecture 4 - Cornell University · 2020. 1. 28. · Recall: Modules • Modules provide extra functions, variables § Example: math provides math.cos(), math.pi § Access them with

Next Time: Text Processing

9/10/19 Defining Functions 48