Professional Documents
Culture Documents
SFU CMPT120 Lecture 4.1-After-Large
SFU CMPT120 Lecture 4.1-After-Large
• Today
• Last Time • Functions
• In-class Exercises • Flow of Execution With Functions
• Multiple Functions
• Calling Functions Inside Functions
• Function Parameters
• Main Function
• Reading
• cspy ch. 5 & 6
1 3 4
2
def addition(): addition(): def addition
def addition:
a = 3 a = 3 a = 3
a = 3
b = 4 b = 4 b = 4
b = 4
c = a * b c = a + b c = a + b
c = a + b
def saybye():
print('I need to go!') A function can be called inside
print('See you later!') another function. Python keep
track of execution flow.
1 2 4
3
CMPT 120, Spring 2023, Mohammad Tayebi 13
Multiple Parameters
def sayhi(pname, pcountry, pcity):
print('Hi', pname + '!') No limitation in the number of
print('What’s up?') parameters you pass to a function.
print('Are you from', pcountry +
'?') Parameters should be separated by
commas.
print('which city?', pcity + '?')
name = 'Anna'
age = sayhi(name)
print(name, 'is', str(age) + '
years old.') Hi Anna!
May I ask how old are you? 12
Anna is 12 years old.
CMPT 120, Spring 2023, Mohammad Tayebi 15
In-class Exercise 7
• What does the
following def somefunction(a,b):
function do? c = a * a + b
c = a / a
d = c + a
1 2 4
3
CMPT 120, Spring 2023, Mohammad Tayebi 16
Using a Main Function
In some programming languages such as C,
def sayhi(pname): C++ and Java, the program can not start
print('Hi', pname + '!') executions from statements sitting alone
outside of a function, and a special function,
called main, is needed to be automatically
invoked for program execution.
def main():
name = 'Anna' In python a main function is not needed but to
have a more structured and readable program it
sayhi(name) is recommended to use such function. Note that,
print('Goodbye!') there is nothing special about the name main.
main() Hi Anna!
Goodbye!
CMPT 120, Spring 2023, Mohammad Tayebi 17
Example - A Function to Compute the
Volume of a Cylinder
def volume_cylinder(radius, height):
pi = 3.14159
volume = pi * radius ** 2 * height
return volume
print(volume_cylinder(2, 3))
37.699079999999995
print(area_circle(2))
12.56636
def area_circle(radius):
pi = 3.14159
area = pi * radius ** 2
print(area)
area_circle(2)
12.56636
def area_circle(radius):
We can define a global variable inside a
global pi function using the global keyword.
pi = 3.14159
area = pi * radius ** 2
Functions can access global variables and
pi = pi + 1 The area_circle modify them. Modifying global variable
print(area) function has some side inside a functions is considered poor
effect on whole program programming practice.
by changing the value of
the variable pi.
area_circle(2) Based on modular programming principles
print(pi) the best practice is to think of a function
as black-box with input and outputs.
12.56636
4.14159
CMPT 120, Spring 2023, Mohammad Tayebi 22
In-class Exercise 8
def somefunction(a,b):
global k
• What is the final k = 10
value of x? c = a + b + k
return c
x = somefunction(1,2)
k = 3
x = somefunction(1,2)
x = x + k
12 18 11
23
1 2 4
3
CMPT 120, Spring 2023, Mohammad Tayebi 23
Modules
# greetings module
• You can store your functions in a def greeting_morning(pname):
print('Good morning,', pname + "!")
module and import it to your code
when needed. def greeting_afternoon(pname):
print('Good afternoon,', pname + "!")
@@@@@@@1
@@@@@@12
@@@@@123
@@@@1234 for i in range(7):
@@@12345 print('@' * (7 - i), end='')
@@123456
@1234567
for j in range(1, i + 2):
print(j, end='')
print()
@
for i in range(7):
@@
print(' ' * (7 - i), end='')
@@@
for j in range(0, i + 1):
@@@@
print('@ ', end='')
@@@@@
print()
@@@@@@
@@@@@@@
# this program computes the area of a cylinder given its radius and height.
radius = float(input('Insert the radius of the cylinder: '))
height = float(input('Insert the height of the cylinder: '))
pi = 3.14
area = 2 * pi * radius * height + 2 * pi * radius * height
print('The cylinder are is:', area)