Python Course Lecture Defining Functions

MuhammadIfitikhar 11 views 48 slides Aug 29, 2024
Slide 1
Slide 1 of 48
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48

About This Presentation

Python course lecture slides


Slide Content

Defining Functions
Lecture 4

Announcement: Partner Finding Social
•Reminder: Assignments can be done in pairs
§Can work with anyone in class (in any section)
§Can change partners every assignment
•Having a mixer to help find a partner
§4-6pm Tuesday, September 6th
§Upson Lounge (adjacent to Duffield)
•Please registerif you are coming
§Need to know how much food to have
§Link will be posted on Ed Discussions
9/1/22Defining Functions2

Announcement: Partner Finding Social
•Reminder: Assignments can be done in pairs
§Can work with anyone in class (in any section)
§Can change partners every assignment
•Having a mixer to help find a partner
§4-6pm Tuesday, September 6th
§Upson Lounge (adjacent to Duffield)
•Please registerif you are coming
§Need to know how much food to have
§Link will be posted on Ed Discussions
9/1/22Defining Functions3

Academic Integrity Quiz
•Remember: quiz about the course AI policy
§Have posted grades for completed quizes
§Right now, missing ~40 enrolled students
§If did not receive 9/10 (~124 students), take it again
•If you are not aware of the quiz
§Go to http://www.cs.cornell.edu/courses/cs1110/
§Click Academic Integrity underAssessments
§Read and take quiz in CMS
9/1/22Defining Functions4

Recall: Modules
•Modules provide extra functions, variables
§Example: mathprovides math.cos(), math.pi
§Access them with the importcommand
•Python provides a lot of them for us
•This Lecture: How to make modules
§VS Code to makea module
§Python to usethe module
9/1/22Defining Functions5
Two different
programs

We Write Programs to Do Things
•Functions are the key doers
9/1/22Defining Functions6
Function CallFunction Definition
•Command to do the function
>>> plus(23)
24
>>>
•Defines what function does
defplus(n):
returnn+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

We Write Programs to Do Things
•Functions are the key doers
9/1/22Defining Functions7
Function CallFunction Definition
•Command to do the function
>>> plus(23)
24
>>>
•Defines what function does
defplus(n):
returnn+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

We Write Programs to Do Things
•Functions are the key doers
9/1/22Defining Functions8
Function CallFunction Definition
•Command to do the function
>>> plus(23)
24
>>>
•Defines what function does
defplus(n):
returnn+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
HeaderFunction
Body
(indented)

We Write Programs to Do Things
•Functions are the key doers
9/1/22Defining Functions9
Function CallFunction Definition
•Command to do the function
>>> plus(23)
24
>>>
•Defines what function does
defplus(n):
returnn+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
HeaderFunction
Body
(indented)declaration of
parameter n
argument to
assign to n

Anatomy of a Function Definition
defplus(n):
"""Returns the number n+1
Parameter n: number to add to
Precondition: n is a number"""
x = n+1
return x
9/1/22Defining Functions10
Function Header
nameparameters
Docstring
Specification
Statements to
execute when called

Anatomy of a Function Definition
defplus(n):
"""Returns the number n+1
Parameter n: number to add to
Precondition: n is a number"""
x = n+1
return x
9/1/22Defining Functions11
Function Header
nameparameters
Docstring
Specification
Statements to
execute when called
The vertical line
indicates indentation
Use vertical lines when you write Python
on examsso we can see indentation

The returnStatement
•Format:return <expression>
§Used to evaluate function call (as an expression)
§Also stops executing the function!
§Any statements after a returnare ignored
•Example: temperature converter function
defto_centigrade(x):
"""Returns: x converted to centigrade"""
return5*(x-32)/9.0
9/1/22Defining Functions12

A More Complex Example
Function Definition
deffoo(a,b):
"""Return something
Parama: number
Paramb: number"""
x = a
y = b
returnx*y+y
Function Call
>>> x = 2
>>> foo(3,4)
9/1/22Defining Functions13
?x
Whatis in the box?

A More Complex Example
Function Definition
deffoo(a,b):
"""Return something
Parama: number
Paramb: number"""
x = a
y = b
returnx*y+y
Function Call
>>> x = 2
>>> foo(3,4)
9/1/22Defining Functions14
?x
Whatis in the box?
A: 2
B: 3
C: 16
D: Nothing!
E: I do not know

A More Complex Example
Function Definition
deffoo(a,b):
"""Return something
Parama: number
Paramb: number"""
x = a
y = b
returnx*y+y
Function Call
>>> x = 2
>>> foo(3,4)
9/1/22Defining Functions15
?x
Whatis in the box?
A: 2
B: 3
C: 16
D: Nothing!
E: I do not know
CORRECT

•Number of statement in the
function body to execute next
•Starts with line no. of body
Draw parameters
as variables
(named boxes)
Understanding How Functions Work
•Function Frame: Representation of function call
•A conceptual model of Python
9/1/22Defining Functions16
function name
local variables (later in lecture)
parameters
instruction counter

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
9/1/22Defining Functions17
defto_centigrade(x):
return 5*(x-32)/9.0
to_centigrade2
x
Initial call frame
(before exec body)
nextline to execute
50.0
2
1

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
9/1/22Defining Functions18
defto_centigrade(x):
return 5*(x-32)/9.0
to_centigrade
50.0x
Executing the
return statement
Return statement creates a
special variable for result
RETURN10.0
2
1

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
9/1/22Defining Functions19
defto_centigrade(x):
return 5*(x-32)/9.0
to_centigrade
50.0x
Executing the
return statement
Thereturn terminates;
no next line to execute
RETURN10.0
2
1

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
9/1/22Defining Functions20
defto_centigrade(x):
return 5*(x-32)/9.0
ERASE WHOLE FRAME
But don’t actually
erase on an exam2
1

Call Frames vs. Global Variables
The specification is a lie:
defswap(a,b):
"""Swap global a & b"""
tmp= a
a = b
b = tmp
>>> a = 1
>>> b = 2
>>> swap(a,b)
9/1/22Defining Functions21
1a2b
swap3
1a2b
Global Variables
Call Frame
5
4
3
2
1

Call Frames vs. Global Variables
The specification is a lie:
defswap(a,b):
"""Swap global a & b"""
tmp= a
a = b
b = tmp
>>> a = 1
>>> b = 2
>>> swap(a,b)
9/1/22Defining Functions22
1a2b
swap4
1a2b
Global Variables
Call Frame
1tmp
5
4
3
2
1

Call Frames vs. Global Variables
The specification is a lie:
defswap(a,b):
"""Swap global a & b"""
tmp= a
a = b
b = tmp
>>> a = 1
>>> b = 2
>>> swap(a,b)
9/1/22Defining Functions23
1a2b
swap5
1a2b
Global Variables
Call Frame
1tmp
2x
5
4
3
2
1

Call Frames vs. Global Variables
The specification is a lie:
defswap(a,b):
"""Swap global a & b"""
tmp= a
a = b
b = tmp
>>> a = 1
>>> b = 2
>>> swap(a,b)
9/1/22Defining Functions24
1a2b
swap
1a2b
Global Variables
Call Frame
1tmp
21xx
5
4
3
2
1

Call Frames vs. Global Variables
The specification is a lie:
defswap(a,b):
"""Swap global a & b"""
tmp= a
a = b
b = tmp
>>> a = 1
>>> b = 2
>>> swap(a,b)
9/1/22Defining Functions25
1a2b
Global Variables
Call FrameERASE THE FRAME
5
4
3
2
1

Exercise Time
Function Definition
deffoo(a,b):
"""Return something
Paramx: a number
Paramy: a number"""
x = a
y = b
returnx*y+y
Function Call
>>> x = foo(3,4)
9/1/22Defining Functions26
5
6
7
Whatdoes the
frame look like
at the start?
4
3
2
1

Which One is Closest to Your Answer?
A:B:
9/1/22Defining Functions27
C:D:foo5
3a
foo5
3a4b
xy
foo4
3a4b
foo5
3a4b

Which One is Closest to Your Answer?
A:B:
9/1/22Defining Functions28
C:D:foo5
3a
foo5
3a4b
xy
foo4
3a4b
foo5
3a4b
E:
¯\_(ツ)_/¯

Exercise Time
Function Definition
deffoo(a,b):
"""Return something
Paramx: a number
Paramy: a number"""
x = a
y = b
returnx*y+y
Function Call
>>> x = foo(3,4)
B:
9/1/22Defining Functions29
5
6
7
4
3
2
1
foo5
3a4b

Exercise Time
Function Definition
deffoo(a,b):
"""Return something
Paramx: a number
Paramy: a number"""
x = a
y = b
returnx*y+y
Function Call
>>> x = foo(3,4)
B:
9/1/22Defining Functions30
5
6
7
4
3
2
1
foo5
3a4b
Whatis the nextstep?

Which One is Closest to Your Answer?
A:B:
9/1/22Defining Functions31
C:D:foo6
3a4b
3x
foo6
3a4b
3xy
foo6
3a4b
foo5
3a4b
3x

Exercise Time
Function Definition
deffoo(a,b):
"""Return something
Paramx: a number
Paramy: a number"""
x = a
y = b
returnx*y+y
Function Call
>>> x = foo(3,4)
C:
9/1/22Defining Functions32
foo6
3a4b
3x5
6
7
4
3
2
1

Exercise Time
Function Definition
deffoo(a,b):
"""Return something
Paramx: a number
Paramy: a number"""
x = a
y = b
returnx*y+y
Function Call
>>> x = foo(3,4)
C:
9/1/22Defining Functions33
foo6
3a4b
3x
Whatis the nextstep?
5
6
7
4
3
2
1

Which One is Closest to Your Answer?
A:B:
9/1/22Defining Functions34
C:D:
foo7
3a4b
3x4y
foo
3a4b
3x4y
RETURN
7
foo
3a4b
3x4y
RETURN16
ERASE THE FRAME

Exercise Time
Function Definition
deffoo(a,b):
"""Return something
Paramx: a number
Paramy: a number"""
x = a
y = b
returnx*y+y
Function Call
>>> x = foo(3,4)
A:
9/1/22Defining Functions35
foo7
3a4b
3x4y5
6
7
4
3
2
1

Exercise Time
Function Definition
deffoo(a,b):
"""Return something
Paramx: a number
Paramy: a number"""
x = a
y = b
returnx*y+y
Function Call
>>> x = foo(3,4)
A:
9/1/22Defining Functions36
foo7
3a4b
3x4y
Whatis the nextstep?
5
6
7
4
3
2
1

Which One is Closest to Your Answer?
A:B:
9/1/22Defining Functions37
C:D:
foo7foo
3a4b
3x4y
RETURN16
7
foo
3a4b
3x4y
RETURN16
ERASE THE FRAME
RETURN16

Exercise Time
Function Definition
deffoo(a,b):
"""Return something
Paramx: a number
Paramy: a number"""
x = a
y = b
returnx*y+y
Function Call
>>> x = foo(3,4)
C:
9/1/22Defining Functions38
foo
3a4b
3x4y
RETURN16
5
6
7
4
3
2
1

Exercise Time
Function Definition
deffoo(a,b):
"""Return something
Paramx: a number
Paramy: a number"""
x = a
y = b
returnx*y+y
Function Call
>>> x = foo(3,4)
C:
9/1/22Defining Functions39
foo
3a4b
3x4y
RETURN16
Whatis the nextstep?
5
6
7
4
3
2
1

Which One is Closest to Your Answer?
A:B:
9/1/22Defining Functions40
C:D:16x
foo
foo
16x
RETURN16
ERASE THE FRAME
ERASE THE FRAME

Exercise Time
Function Definition
deffoo(a,b):
"""Return something
Paramx: a number
Paramy: a number"""
x = a
y = b
returnx*y+y
Function Call
>>> x = foo(3,4)
D:
9/1/22Defining Functions41
16x
ERASE THE FRAME
5
6
7
4
3
2
1

Exercise Time
Function Definition
deffoo(a,b):
"""Return something
Paramx: a number
Paramy: a number"""
x = a
y = b
returnx*y+y
Function Call
>>> x = foo(3,4)
D:
9/1/22Defining Functions42
16x
ERASE THE FRAME
Variablein
global space
5
6
7
4
3
2
1

Visualizing Frames: The Python Tutor
9/1/22Defining Functions43

Visualizing Frames: The Python Tutor
9/1/22Defining Functions44
Global
Space
Call Frame

Visualizing Frames: The Python Tutor
9/1/22Defining Functions45
Global
Space
Call Frame
Variablesfrom
second lecture
go in here

Visualizing Frames: The Python Tutor
9/1/22Defining Functions46
Missingline
numbers!

Visualizing Frames: The Python Tutor
9/1/22Defining Functions47
Missingline
numbers!
Line number
marked here
(sort-of)

Next Time: Text Processing
9/1/22Defining Functions48
Tags