SlideShare a Scribd company logo
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 register if you are coming
§ Need to know how much food to have
§ Link will be posted on Ed Discussions
9/1/22 Defining Functions 2
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 register if you are coming
§ Need to know how much food to have
§ Link will be posted on Ed Discussions
9/1/22 Defining Functions 3
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 under Assessments
§ Read and take quiz in CMS
9/1/22 Defining Functions 4
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
§ VS Code to make a module
§ Python to use the module
9/1/22 Defining Functions 5
Two different
programs
We Write Programs to Do Things
• Functions are the key doers
9/1/22 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
We Write Programs to Do Things
• Functions are the key doers
9/1/22 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
We Write Programs to Do Things
• Functions are the key doers
9/1/22 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
Function
Header Function
Body
(indented)
We Write Programs to Do Things
• Functions are the key doers
9/1/22 Defining Functions 9
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
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
9/1/22 Defining Functions 10
Function Header
name parameters
Docstring
Specification
Statements to
execute when called
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
9/1/22 Defining Functions 11
Function Header
name parameters
Docstring
Specification
Statements to
execute when called
The vertical line
indicates indentation
Use vertical lines when you write Python
on exams so we can see indentation
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
9/1/22 Defining Functions 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)
9/1/22 Defining Functions 13
?
x
What is in the box?
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)
9/1/22 Defining Functions 14
?
x
What is in the box?
A: 2
B: 3
C: 16
D: Nothing!
E: I do not know
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)
9/1/22 Defining Functions 15
?
x
What is 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/22 Defining Functions 16
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/22 Defining Functions 17
def to_centigrade(x):
return 5*(x-32)/9.0
to_centigrade 2
x
Initial call frame
(before exec body)
next line 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/22 Defining Functions 18
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
RETURN 10.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/22 Defining Functions 19
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
RETURN 10.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/22 Defining Functions 20
def to_centigrade(x):
return 5*(x-32)/9.0
ERASE
W
H
O
LE
FRAM
E
But don’t actually
erase on an exam
2
1
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)
9/1/22 Defining Functions 21
1
a 2
b
swap 3
1
a 2
b
Global Variables
Call Frame
5
4
3
2
1
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)
9/1/22 Defining Functions 22
1
a 2
b
swap 4
1
a 2
b
Global Variables
Call Frame
1
tmp
5
4
3
2
1
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)
9/1/22 Defining Functions 23
1
a 2
b
swap 5
1
a 2
b
Global Variables
Call Frame
1
tmp
2
x
5
4
3
2
1
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)
9/1/22 Defining Functions 24
1
a 2
b
swap
1
a 2
b
Global Variables
Call Frame
1
tmp
2 1
x x
5
4
3
2
1
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)
9/1/22 Defining Functions 25
1
a 2
b
Global Variables
Call Frame
ERASE THE FRAME
5
4
3
2
1
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)
9/1/22 Defining Functions 26
5
6
7
What does the
frame look like
at the start?
4
3
2
1
Which One is Closest to Your Answer?
A: B:
9/1/22 Defining Functions 27
C: D:
foo 5
3
a
foo 5
3
a 4
b
x y
foo 4
3
a 4
b
foo 5
3
a 4
b
Which One is Closest to Your Answer?
A: B:
9/1/22 Defining Functions 28
C: D:
foo 5
3
a
foo 5
3
a 4
b
x y
foo 4
3
a 4
b
foo 5
3
a 4
b
E:
¯_(ツ)_/¯
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:
9/1/22 Defining Functions 29
5
6
7
4
3
2
1
foo 5
3
a 4
b
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:
9/1/22 Defining Functions 30
5
6
7
4
3
2
1
foo 5
3
a 4
b
What is the next step?
Which One is Closest to Your Answer?
A: B:
9/1/22 Defining Functions 31
C: D:
foo 6
3
a 4
b
3
x
foo 6
3
a 4
b
3
x y
foo 6
3
a 4
b
foo 5
3
a 4
b
3
x
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:
9/1/22 Defining Functions 32
foo 6
3
a 4
b
3
x
5
6
7
4
3
2
1
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:
9/1/22 Defining Functions 33
foo 6
3
a 4
b
3
x
What is the next step?
5
6
7
4
3
2
1
Which One is Closest to Your Answer?
A: B:
9/1/22 Defining Functions 34
C: D:
foo 7
3
a 4
b
3
x 4
y
foo
3
a 4
b
3
x 4
y
RETURN
7
foo
3
a 4
b
3
x 4
y
RETURN 16
ERASE THE FRAME
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:
9/1/22 Defining Functions 35
foo 7
3
a 4
b
3
x 4
y
5
6
7
4
3
2
1
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:
9/1/22 Defining Functions 36
foo 7
3
a 4
b
3
x 4
y
What is the next step?
5
6
7
4
3
2
1
Which One is Closest to Your Answer?
A: B:
9/1/22 Defining Functions 37
C: D:
foo 7 foo
3
a 4
b
3
x 4
y
RETURN 16
7
foo
3
a 4
b
3
x 4
y
RETURN 16
ERASE THE FRAME
RETURN 16
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:
9/1/22 Defining Functions 38
foo
3
a 4
b
3
x 4
y
RETURN 16
5
6
7
4
3
2
1
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:
9/1/22 Defining Functions 39
foo
3
a 4
b
3
x 4
y
RETURN 16
What is the next step?
5
6
7
4
3
2
1
Which One is Closest to Your Answer?
A: B:
9/1/22 Defining Functions 40
C: D:
16
x
foo
foo
16
x
RETURN 16
ERASE THE FRAME
ERASE THE FRAME
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:
9/1/22 Defining Functions 41
16
x
ERASE THE FRAME
5
6
7
4
3
2
1
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:
9/1/22 Defining Functions 42
16
x
ERASE THE FRAME
Variable in
global space
5
6
7
4
3
2
1
Visualizing Frames: The Python Tutor
9/1/22 Defining Functions 43
Visualizing Frames: The Python Tutor
9/1/22 Defining Functions 44
Global
Space
Call Frame
Visualizing Frames: The Python Tutor
9/1/22 Defining Functions 45
Global
Space
Call Frame
Variables from
second lecture
go in here
Visualizing Frames: The Python Tutor
9/1/22 Defining Functions 46
Missing line
numbers!
Visualizing Frames: The Python Tutor
9/1/22 Defining Functions 47
Missing line
numbers!
Line number
marked here
(sort-of)
Next Time: Text Processing
9/1/22 Defining Functions 48

More Related Content

PDF
The Ring programming language version 1.6 book - Part 23 of 189
PDF
The Ring programming language version 1.9 book - Part 28 of 210
PDF
Python Slides conditioanls and control flow
PDF
The Ring programming language version 1.8 book - Part 26 of 202
PPTX
Understanding Python Programming Language -Functions
PDF
The Ring programming language version 1.9 book - Part 27 of 210
PDF
03-Variables, Expressions and Statements (1).pdf
PDF
The Ring programming language version 1.7 book - Part 24 of 196
The Ring programming language version 1.6 book - Part 23 of 189
The Ring programming language version 1.9 book - Part 28 of 210
Python Slides conditioanls and control flow
The Ring programming language version 1.8 book - Part 26 of 202
Understanding Python Programming Language -Functions
The Ring programming language version 1.9 book - Part 27 of 210
03-Variables, Expressions and Statements (1).pdf
The Ring programming language version 1.7 book - Part 24 of 196

Similar to Python Course Lecture Defining Functions (20)

PPTX
Pythonlearn-02-Expressions123AdvanceLevel.pptx
PPTX
FUNCTIONS, CLASSES AND OBJECTS.pptx
PDF
The Ring programming language version 1.5.1 book - Part 20 of 180
PPTX
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
PPT
functions _
PPT
basics of optimizations presentation s
PPTX
Python Programming | JNTUA | UNIT 2 | Conditionals and Recursion |
PDF
The Ring programming language version 1.8 book - Part 25 of 202
PDF
L14-L16 Functions.pdf
PDF
The Ring programming language version 1.5.4 book - Part 21 of 185
PPTX
lesson 3 operation function gen math.pptx
PDF
The Ring programming language version 1.9 book - Part 98 of 210
PDF
Code Refactoring
PPTX
JNTUK python programming python unit 3.pptx
PPTX
functions
PDF
The Ring programming language version 1.5.1 book - Part 174 of 180
PPTX
Chapter 4
PDF
Sample Exam Questions on Python for revision
PDF
Optimizing the Catalyst Optimizer for Complex Plans
PPT
Savitch ch 04
Pythonlearn-02-Expressions123AdvanceLevel.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptx
The Ring programming language version 1.5.1 book - Part 20 of 180
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
functions _
basics of optimizations presentation s
Python Programming | JNTUA | UNIT 2 | Conditionals and Recursion |
The Ring programming language version 1.8 book - Part 25 of 202
L14-L16 Functions.pdf
The Ring programming language version 1.5.4 book - Part 21 of 185
lesson 3 operation function gen math.pptx
The Ring programming language version 1.9 book - Part 98 of 210
Code Refactoring
JNTUK python programming python unit 3.pptx
functions
The Ring programming language version 1.5.1 book - Part 174 of 180
Chapter 4
Sample Exam Questions on Python for revision
Optimizing the Catalyst Optimizer for Complex Plans
Savitch ch 04
Ad

More from MuhammadIfitikhar (9)

PPTX
Lecture-00: Azure IoT Pi Day Slides.pptx
PPTX
Lecture-6: Azure and Iot hub lecture.pptx
PDF
Python course slides topic objects in python
PDF
Python Lecture slides topic Algorithm design
PDF
Python course lecture slides specifications and testing
PDF
Python Lecture Slides topic strings handling
PDF
Python Course Functions and Modules Slides
PDF
Python Lecture Slides Variables and Assignments
PDF
Course Overview Python Basics Course Slides
Lecture-00: Azure IoT Pi Day Slides.pptx
Lecture-6: Azure and Iot hub lecture.pptx
Python course slides topic objects in python
Python Lecture slides topic Algorithm design
Python course lecture slides specifications and testing
Python Lecture Slides topic strings handling
Python Course Functions and Modules Slides
Python Lecture Slides Variables and Assignments
Course Overview Python Basics Course Slides
Ad

Recently uploaded (20)

PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
medical staffing services at VALiNTRY
PDF
System and Network Administraation Chapter 3
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
Nekopoi APK 2025 free lastest update
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
L1 - Introduction to python Backend.pptx
PPTX
Introduction to Artificial Intelligence
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
assetexplorer- product-overview - presentation
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Digital Systems & Binary Numbers (comprehensive )
medical staffing services at VALiNTRY
System and Network Administraation Chapter 3
Which alternative to Crystal Reports is best for small or large businesses.pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Nekopoi APK 2025 free lastest update
How to Choose the Right IT Partner for Your Business in Malaysia
L1 - Introduction to python Backend.pptx
Introduction to Artificial Intelligence
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Design an Analysis of Algorithms II-SECS-1021-03
PTS Company Brochure 2025 (1).pdf.......
wealthsignaloriginal-com-DS-text-... (1).pdf
assetexplorer- product-overview - presentation
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool

Python Course Lecture Defining Functions

  • 2. 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 register if you are coming § Need to know how much food to have § Link will be posted on Ed Discussions 9/1/22 Defining Functions 2
  • 3. 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 register if you are coming § Need to know how much food to have § Link will be posted on Ed Discussions 9/1/22 Defining Functions 3
  • 4. 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 under Assessments § Read and take quiz in CMS 9/1/22 Defining Functions 4
  • 5. 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 § VS Code to make a module § Python to use the module 9/1/22 Defining Functions 5 Two different programs
  • 6. We Write Programs to Do Things • Functions are the key doers 9/1/22 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
  • 7. We Write Programs to Do Things • Functions are the key doers 9/1/22 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
  • 8. We Write Programs to Do Things • Functions are the key doers 9/1/22 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 Function Header Function Body (indented)
  • 9. We Write Programs to Do Things • Functions are the key doers 9/1/22 Defining Functions 9 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
  • 10. 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 9/1/22 Defining Functions 10 Function Header name parameters Docstring Specification Statements to execute when called
  • 11. 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 9/1/22 Defining Functions 11 Function Header name parameters Docstring Specification Statements to execute when called The vertical line indicates indentation Use vertical lines when you write Python on exams so we can see indentation
  • 12. 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 9/1/22 Defining Functions 12
  • 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) 9/1/22 Defining Functions 13 ? x What is in the box?
  • 14. 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) 9/1/22 Defining Functions 14 ? x What is in the box? A: 2 B: 3 C: 16 D: Nothing! E: I do not know
  • 15. 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) 9/1/22 Defining Functions 15 ? x What is in the box? A: 2 B: 3 C: 16 D: Nothing! E: I do not know CORRECT
  • 16. • 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/22 Defining Functions 16 function name local variables (later in lecture) parameters instruction counter
  • 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 9/1/22 Defining Functions 17 def to_centigrade(x): return 5*(x-32)/9.0 to_centigrade 2 x Initial call frame (before exec body) next line to execute 50.0 2 1
  • 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 9/1/22 Defining Functions 18 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 RETURN 10.0 2 1
  • 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 9/1/22 Defining Functions 19 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 RETURN 10.0 2 1
  • 20. 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/22 Defining Functions 20 def to_centigrade(x): return 5*(x-32)/9.0 ERASE W H O LE FRAM E But don’t actually erase on an exam 2 1
  • 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) 9/1/22 Defining Functions 21 1 a 2 b swap 3 1 a 2 b Global Variables Call Frame 5 4 3 2 1
  • 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) 9/1/22 Defining Functions 22 1 a 2 b swap 4 1 a 2 b Global Variables Call Frame 1 tmp 5 4 3 2 1
  • 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) 9/1/22 Defining Functions 23 1 a 2 b swap 5 1 a 2 b Global Variables Call Frame 1 tmp 2 x 5 4 3 2 1
  • 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) 9/1/22 Defining Functions 24 1 a 2 b swap 1 a 2 b Global Variables Call Frame 1 tmp 2 1 x x 5 4 3 2 1
  • 25. 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) 9/1/22 Defining Functions 25 1 a 2 b Global Variables Call Frame ERASE THE FRAME 5 4 3 2 1
  • 26. 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) 9/1/22 Defining Functions 26 5 6 7 What does the frame look like at the start? 4 3 2 1
  • 27. Which One is Closest to Your Answer? A: B: 9/1/22 Defining Functions 27 C: D: foo 5 3 a foo 5 3 a 4 b x y foo 4 3 a 4 b foo 5 3 a 4 b
  • 28. Which One is Closest to Your Answer? A: B: 9/1/22 Defining Functions 28 C: D: foo 5 3 a foo 5 3 a 4 b x y foo 4 3 a 4 b foo 5 3 a 4 b E: ¯_(ツ)_/¯
  • 29. 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: 9/1/22 Defining Functions 29 5 6 7 4 3 2 1 foo 5 3 a 4 b
  • 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: 9/1/22 Defining Functions 30 5 6 7 4 3 2 1 foo 5 3 a 4 b What is the next step?
  • 31. Which One is Closest to Your Answer? A: B: 9/1/22 Defining Functions 31 C: D: foo 6 3 a 4 b 3 x foo 6 3 a 4 b 3 x y foo 6 3 a 4 b foo 5 3 a 4 b 3 x
  • 32. 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: 9/1/22 Defining Functions 32 foo 6 3 a 4 b 3 x 5 6 7 4 3 2 1
  • 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: 9/1/22 Defining Functions 33 foo 6 3 a 4 b 3 x What is the next step? 5 6 7 4 3 2 1
  • 34. Which One is Closest to Your Answer? A: B: 9/1/22 Defining Functions 34 C: D: foo 7 3 a 4 b 3 x 4 y foo 3 a 4 b 3 x 4 y RETURN 7 foo 3 a 4 b 3 x 4 y RETURN 16 ERASE THE FRAME
  • 35. 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: 9/1/22 Defining Functions 35 foo 7 3 a 4 b 3 x 4 y 5 6 7 4 3 2 1
  • 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: 9/1/22 Defining Functions 36 foo 7 3 a 4 b 3 x 4 y What is the next step? 5 6 7 4 3 2 1
  • 37. Which One is Closest to Your Answer? A: B: 9/1/22 Defining Functions 37 C: D: foo 7 foo 3 a 4 b 3 x 4 y RETURN 16 7 foo 3 a 4 b 3 x 4 y RETURN 16 ERASE THE FRAME RETURN 16
  • 38. 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: 9/1/22 Defining Functions 38 foo 3 a 4 b 3 x 4 y RETURN 16 5 6 7 4 3 2 1
  • 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: 9/1/22 Defining Functions 39 foo 3 a 4 b 3 x 4 y RETURN 16 What is the next step? 5 6 7 4 3 2 1
  • 40. Which One is Closest to Your Answer? A: B: 9/1/22 Defining Functions 40 C: D: 16 x foo foo 16 x RETURN 16 ERASE THE FRAME ERASE THE FRAME
  • 41. 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: 9/1/22 Defining Functions 41 16 x ERASE THE FRAME 5 6 7 4 3 2 1
  • 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: 9/1/22 Defining Functions 42 16 x ERASE THE FRAME Variable in global space 5 6 7 4 3 2 1
  • 43. Visualizing Frames: The Python Tutor 9/1/22 Defining Functions 43
  • 44. Visualizing Frames: The Python Tutor 9/1/22 Defining Functions 44 Global Space Call Frame
  • 45. Visualizing Frames: The Python Tutor 9/1/22 Defining Functions 45 Global Space Call Frame Variables from second lecture go in here
  • 46. Visualizing Frames: The Python Tutor 9/1/22 Defining Functions 46 Missing line numbers!
  • 47. Visualizing Frames: The Python Tutor 9/1/22 Defining Functions 47 Missing line numbers! Line number marked here (sort-of)
  • 48. Next Time: Text Processing 9/1/22 Defining Functions 48