[CreativeProgramming]Lecture4_Functions in Python
[CreativeProgramming]Lecture4_Functions in Python
Spring 2025
CUL1122 Lecture #04
Functions in Python
Today
❖Functions
▪ Importance of Functions
▪ Parameters and Return Values
▪ Function Calls and Return Values
▪ Scope of Variables
3
Basic Idea of Functions
4 0 100 4
Mathematical Functions vs. Functions in Computer Programs
❖In mathematics, functions are either used from existing ones or defined
anew.
▪ Using existing functions: sine, cosine, etc.
▪ Defining new functions: f(x) = x2 + 2x + 1
❖Functions in computer programs
▪ Functions are written using programming languages.
▪ They encapsulate ideas for computation to solve problems and are assigned a
name.
5
Importance of Functions
6
Reusability of Functions
7
Defining a Python Function
❖The function declaration consists of the function name on the first line,
followed by the function body.
def name(parameters): # Function name is the identifier used to call the function
# Parameters are a list of inputs passed to the function
code # Function body must be indented
code Function # Function body is executed each time the function is called
… Body
return value # ‘return’ ends the function call and sends result back to the caller
# You can omit the ‘return’ if you want to return nothing
: Indentation
8
Arguments and Return Values
bake_cake( tiers
1-tier , flavor
‘Cheese’ )
9
Arguments and Return Values
bake_cake( tiers
4-tier , flavor
‘Choco’ )
10
Function Call and Result Passing
Calling a function
3- with arguments
bake_cake( tiers
3-tier flavor )
, ti ‘Cheese’)
er 3-tier ‘Cheese’
bake_cake
‘Cheese’
11
Function Call and Result Passing
Calling a function
3- with arguments
bake_cake( tiers
3-tier flavor )
, ti ‘Cheese’)
er 3-tier ‘Cheese’
bake_cake
Passing return
values
3
“Cheese”
12
Parameters vs. Arguments
26 Return value
13
Function Execution Order
14
Scope of Variables: Types
❖1) Local Variables
▪ Valid only within the function.
▪ Created upon function call and disappear when the function ends.
❖2) Global Variables
▪ Valid throughout the script (or program).
▪ Created at the start of the program and cease to exist when the program ends.
❖Example:
▪ Global Variable: A person known throughout the country, such as Elon Musk.
▪ A person who is well-known in a specific local area, such as Mayor Oh Se-hoon.
➢He is famous in Korea but may not be recognized outside of the country.
15
Scope of Variables: Access Error
16
Scope of Variables: Priority
❖When global and local variables share the same name, each operates
within its own scope hierarchy.
17
Scope of Variables: Accessing Global Variables
18
Lab 4
Today
20
Exercise #1: Using a Calculator
21
Exercise #2: Converting Temperature
22
Exercise #3: Calculating Factorials
23
Exercise #4: Understanding Variable Scope in Python
❖Create a script that defines both a global variable and a local variable
within a function to explore how variable scope works in Python.
▪ Use the same name, celebrity, for both the global and local variables, assigning
famous names to each.
▪ Test the scope by attempting to access the celebrity variable both inside and
outside the function.
24
Exercise #5: Calculating the Total Price
❖Create a script that defines a function to calculate the drink price based
on the order, calls the function, and displays the result.
▪ Declare the total price as a global variable accessible throughout the script.
25
Exercise #6: Assessing Body Mass Index (BMI)
26
Exercise #7: Playing FizzBuzz
27
수고하셨습니다!
28