0% found this document useful (0 votes)
2 views31 pages

(Lecture 2) Functions, Memory Model, Function Design

The document provides an overview of defining functions in Python, including how to create custom functions like converting Celsius to Fahrenheit. It discusses the importance of function structure, local variables, and the memory model during function calls. Additionally, it outlines guidelines for designing new functions, emphasizing the need for planning and testing.

Uploaded by

rmstn365
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views31 pages

(Lecture 2) Functions, Memory Model, Function Design

The document provides an overview of defining functions in Python, including how to create custom functions like converting Celsius to Fahrenheit. It discusses the importance of function structure, local variables, and the memory model during function calls. Additionally, it outlines guidelines for designing new functions, emphasizing the need for planning and testing.

Uploaded by

rmstn365
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Review

• Abstraction

• Expression and evaluation


• Operator and operand
• Value and type
• Name and variable

• Assignment

• Memory model
Computing Bootcamp SNU Graduate School of Data Science 1
Computing Bootcamp

Functions

Lecture 2-1

Hyung-Sin Kim

SNU Graduate School of Data Science


Computing Bootcamp SNU Graduate School of Data Science 2
Defining Our Own Functions
• Python does provide useful built-in functions (e.g., max, min…), but they
are not enough

• For example, assume that we want to covert celsius degree to fahrenheit


degree
• convert_to_fahrenheit(10)
• We want to get 50 (Equation: fahrenheit = celsius * 9/5 + 32)
• But we get an error when typing it, which means that there is no function named
“convert_to_fahrenheit”

Computing Bootcamp SNU Graduate School of Data Science 3


Defining Our Own Functions
• Let’s make our own function “convert_to_fahrenheit”
• def convert_to_fahrenheit(celsius):
• return celsius * 9 / 5 + 32

• Function body is indented! Without the indentation, you will see an error
• Indentation must be the same block of codes

• Let’s do this again


• convert_to_fahrenheit(10)
• And change the argument freely!

Computing Bootcamp SNU Graduate School of Data Science 4


Defining Our Own Functions – Exercise
• Define “convert_to_celsius(fahrenheit),” which converts fahrenheit
degree to celsius degree

• Can someone screen-capture and share on the chat?

Computing Bootcamp SNU Graduate School of Data Science 5


Defining Our Own Functions – Generalization
• def <<function_name>> (<<parameters>>):
function header
• <<function_body>>

• Parameters are variables

• Most functions have a return statement at the end of the function body
• return <<expression>>
• It evaluates the expression, produces a value, which is the result of the function
call

Computing Bootcamp SNU Graduate School of Data Science 6


Local Variables
• Implement “convert_to_fahrenheit” in a different way
• def convert_to_fahrenheit(celsius):
• a=9/5
• b = 32
• return celsius * a + b

• Local variables: Variables created within a function


• Parameters are also local variables
• There are erased when the function returns (cannot be used outside of the function)

Computing Bootcamp SNU Graduate School of Data Science 7


Namespace
• When Python executes a function call, it creates a namespace in which to
store local variables for that call

• If a variable name in the namespace is same as a variable in another


namespace, Python just considers the current namespace!

Computing Bootcamp SNU Graduate School of Data Science 8


Computing Bootcamp

Memory Models for Function


Calls
Lecture 2-2

Hyung-Sin Kim

SNU Graduate School of Data Science


Computing Bootcamp SNU Graduate School of Data Science 9
So… what happens when you call a function?

Computing Bootcamp SNU Graduate School of Data Science 10


Execute a Function Call
• max(3+8, 5)
• Step 1: Evaluate the arguments left to right.

• Step 2: Create a namespace to hold the function call’s local variables, including
the parameters.

• Step 3: Pass the resulting argument values into the function by assigning them to
the parameters

• Step 4: Execute the function body. When a return statement is executed, the
function terminates and the value of the expression in the return statement is used
as the value of the function call

Computing Bootcamp SNU Graduate School of Data Science 11


Memory Model for Function Call
• def doubling(x):
frames objects
• return 2*x
Frames for
Memory objects
namspaces
• x=5
• x = doubling(x+5)

Computing Bootcamp SNU Graduate School of Data Science 12


Memory Model for Function Call
• def doubling(x):
frames objects
• return 2*x
• global

• x=5
• x = doubling(x+5)

Computing Bootcamp SNU Graduate School of Data Science 13


Memory Model for Function Call
• def doubling(x):
frames objects
• return 2*x
• global func doubling(…)
doubling
• x=5
• x = doubling(x+5)

Computing Bootcamp SNU Graduate School of Data Science 14


Memory Model for Function Call
• def doubling(x):
frames objects
• return 2*x
• global func doubling(…)
doubling
• x=5 x
5
• x = doubling(x+5)

Computing Bootcamp SNU Graduate School of Data Science 15


Memory Model for Function Call
• def doubling(x):
frames objects
• return 2*x
• global func doubling(…)
doubling
• x=5 x
5
• x = doubling(x+5)

Computing Bootcamp SNU Graduate School of Data Science 16


Memory Model for Function Call
• def doubling(x):
frames objects
• return 2*x
• global func doubling(…)
doubling
• x=5 x
5
• x = doubling(x+5)
• Evaluate argument and get a value (10) 10
• Using x in the global namespace

Computing Bootcamp SNU Graduate School of Data Science 17


Memory Model for Function Call
• def doubling(x):
frames objects
• return 2*x
• global func doubling(…)
doubling
• x=5 x
5
• x = doubling(x+5)
• Evaluate argument and get a value (10) doubling 10
• Using x in the global namespace
• Creating a namespace

Computing Bootcamp SNU Graduate School of Data Science 18


Memory Model for Function Call
• def doubling(x):
frames objects
• return 2*x
• global func doubling(…)
doubling
• x=5 x
5
• x = doubling(x+5)
• Evaluate argument and get a value (10) doubling 10
x
• Using x in the global namespace
• Creating a namespace
• Assign the argument value to the function parameter

Computing Bootcamp SNU Graduate School of Data Science 19


Memory Model for Function Call
• def doubling(x):
frames objects
• return 2*x
• global func doubling(…)
doubling
• x=5 x
5
• x = doubling(x+5)
• Evaluate argument and get a value (10) doubling 10
x
• Using x in the global namespace
• Creating a namespace 20

• Assign the argument value to the function parameter


• Get the return value of the function

Computing Bootcamp SNU Graduate School of Data Science 20


Memory Model for Function Call
• def doubling(x):
frames objects
• return 2*x
• global func doubling(…)
doubling
• x=5 x
5
• x = doubling(x+5)
• Evaluate argument and get a value (10) doubling 10
x
• Using x in the global namespace
Return value
• Creating a namespace 20

• Assign the argument value to the function parameter


• Get the return value of the function

Computing Bootcamp SNU Graduate School of Data Science 21


Memory Model for Function Call
• def doubling(x):
frames objects
• return 2*x
• global func doubling(…)
doubling
• x=5 x
5
• x = doubling(x+5)
• Evaluate argument and get a value (10) doubling 10
x
• Using x in the global namespace
Return value
• Creating a namespace 20

• Assign the argument value to the function parameter


• Get the return value of the function
• Terminate the function and assign the result to x

Computing Bootcamp SNU Graduate School of Data Science 22


Computing Bootcamp SNU Graduate School of Data Science 23
Computing Bootcamp

Function Design

Lecture 2-3

Hyung-Sin Kim

SNU Graduate School of Data Science


Computing Bootcamp SNU Graduate School of Data Science 24
To write your own functions…

Computing Bootcamp SNU Graduate School of Data Science 25


Guidelines for Designing New Functions
• Recall what coding is…
• Coding is writing an algorithm using programming language
• Meaning that you must have an algorithm (a plan) in advance!

• Writing a good essay/paper requires planning


• Topic, background material, outline, body

• Writing a good function does need a plan!

Computing Bootcamp SNU Graduate School of Data Science 26


Guidelines for Designing New Functions
• Whenever writing a new function, you need to answer the following
questions
• Name: What do you name the function?
• Param: What are the parameters, and what types of information do they refer to?
• Body: What calculations are you doing with that information?
• Return: What information does the function return?
• Test: Does it work like you expect it to?

Computing Bootcamp SNU Graduate School of Data Science 27


Example of Function Design
• We want to write a function for calculating difference between two days

• Step 1: Determine function name according to what it does


• days_difference
• Step 2: Determine parameters and return value
• Parameters: day1 (int), day2 (int)
• Return value: difference between the two days
• Step 3: Make some test cases (your expectation)
• days_difference(200, 224) 24
• days_difference(27, 27) 0
• days_difference(18, 30) 12

Computing Bootcamp SNU Graduate School of Data Science 28


Example of Function Design
• Step 4: Write the function header
• def days_difference(day1: int, day2: int) -> int:
• Step 5: Write a short description
• # Return the number of days between day1 and day2, which are both in the range
1-365 (thus indicating the day of the year)
• This is a very important step, both for you and your co-workers!
• Step 6: Write the function body
• return day2 – day1
• Step 7: Test
• Confirm if your function works well for the test cases you made

Computing Bootcamp SNU Graduate School of Data Science 29


Summary
• Function structure (header and body)

• Namespace and local variable

• What happens when you call a function

• Guidelines for writing a new function

Computing Bootcamp SNU Graduate School of Data Science 30


Thanks!

Computing Bootcamp SNU Graduate School of Data Science 31

You might also like