University of Science, VNU – HCM
Faculty of Physics and Engineering
Department of Physics and Computer Science
Python
Programming
MSc. Vo Hoang Thuy Tien
Function
Week 4
3
Function
1. Definition
A python function is a is a reusable block of code that performs a specific task.
Functions help in organizing code, improving reusability, and making programs more
modular.
Example:
def add(a, b):
sum = a + b
return sum
result = add(5, 3)
print(result) # Output: 8
4
Function
1. Definition
Example: Syntax:
def add(a, b): DEFINE FUNCTION add(num1, num2)
sum = a + b sum = num1 + num2
return sum RETURN sum
result = add(5, 3) SET result = CALL add(5,3)
print(result) # Output: 8 PRINT ‘the result is’, result
5
Function
1. Definition
Example: Syntax:
def greet(): DEFINE FUNCTION greet()
print("Hello, world!") PRINT "Hello, world!"
greet() # Output: Hello, world! CALL greet() # Output: Hello, world!
6
Function
Example: Calculate BMI (Body Mass Index) and Determine Category
BEGIN
𝑤𝑒𝑖𝑔ℎ𝑡
𝐵𝑀𝐼 = INPUT weight, height
ℎ𝑒𝑖𝑔ℎ𝑡 ∗ ℎ𝑒𝑖𝑔ℎ𝑡
SET bmi = CALL calculate_bmi(weight, height)
IF bmi < 18.5 then
BMI Label PRINT "Gầy"
< 18.5 Gầy ELSE IF bmi < 24.9 then
18.5<= BMI < 24.9 Bình thường PRINT "Bình thường"
24.9 <= BMI < 29.9 Thừa cân ELSE IF bmi < 29.9 then
29.9 <= BMI Béo phì PRINT "Thừa cân"
ELSE
PRINT "Béo phì"
ENDIF
END
7
Function
Example: Calculate BMI (Body Mass Index) and Determine Category
BEGIN
𝑤𝑒𝑖𝑔ℎ𝑡
𝐵𝑀𝐼 = INPUT weight, height
ℎ𝑒𝑖𝑔ℎ𝑡 ∗ ℎ𝑒𝑖𝑔ℎ𝑡
SET bmi = CALL calculate_bmi(weight, height)
IF bmi < 18.5 then
DEFINE FUNCTION calculate_bmi(weight, height) PRINT "Gầy"
bmi = weight / (height * height) ELSE IF bmi < 24.9 then
RETURN bmi RETURN "Bình thường"
END FUNCTION ELSE IF bmi < 29.9 then
RETURN "Thừa cân"
ELSE
RETURN "Béo phì"
ENDIF
END
8
Function
Exercise 1: Draw flowchart to Determine Category
BEGIN
INPUT weight, height
SET bmi = CALL calculate_bmi(weight, height)
SET category = CALL bmi_category(bmi)
END
9
Function
Exercise 2: Simulate a Basic ATM System
Suppose the balance = 100000. The amount deposit or withdraw will be accessed by user
1. Check balance Function check_balance
2. Deposit Function deposit
3. Withdraw Function withdraw
11
Function
Exercise 2: Simulate a Basic ATM System
Suppose the balance = 100000. The amount deposit or withdraw will be accessed by user
BEGIN
ATM SET balance = 100000
PRINT('1. check balance')
1. Check balance PRINT('2. deposit')
2. Deposit PRINT('3. withdraw')
3. Withdraw PRINT('4. exit')
INPUT choice
4. Exit
…
END
12
Function
Step 1: Begin Step 5: Process User Choice
Step 2: Define Functions Step 5.1: If the user selects option "1" (Check Balance):
Step 2.1: Define check_balance(balance) function: Call check_balance(balance).
Print the current balance. Return to Step 4.1
Step 2.2: Define deposit(balance, deposit_amount) function: Step 5.2: If the user selects option "2" (Deposit):
Add deposit_amount to balance. Ask for deposit_amount.
Print a message confirming the deposit. Convert input to an integer.
Return the updated balance. Call deposit(balance, deposit_amount).
Step 2.3: Define withdraw(balance, withdraw_amount) function: Store the returned balance.
Check if withdraw_amount is greater than balance: Return to Step 4.1
If true, print an error message. step 5.3: If the user selects option "3" (Withdraw):
If false, subtract withdraw_amount from balance. Ask for withdraw_amount.
Print a message confirming the withdrawal. Convert input to an integer.
Return the updated balance. Call withdraw(balance, withdraw_amount).
Step 3: Initialize Variables Store the returned balance.
Step 3.1: Set balance = 100000. Return to Step 4.1
Step 4: Display Menu and Process User Input Step 5.4 If the user selects option "4" (Exit):
Step 4.1: Start a loop that runs indefinitely: Break the loop and terminate the program.
Display menu options: Step 5.5: If the user enters an invalid option:
1. Check Balance Print an error message.
2. Deposit Loop back to show the menu again Step 4.1.
3. Withdraw Step 6: End
4. Exit
Step 4.2: Get user input for choice. 13
Function
BEGIN
SET balance = 100000
PRINT('1. check balance')
PRINT('2. deposit')
PRINT('3. withdraw')
PRINT('4. exit')
INPUT choice
WHILE True
IF choice == 1 then
CALL check_balance(balance)
ELSE IF choice == 2 then
SET balance = CALL deposit(balance, deposit_amount)
ELSE IF choice == 3 then
SET balance = CALL withdraw(balance, withdraw_amount)
ELSE IF choice == 4 then
BREAK
ELSE
PRINT ‘invalid choice, please enter again’
END IF
END WHILE
END
17
String manipulation
Exercise 2: Write pseudo code and draw flow chart (using function) calculate the factorial of n.
19
String manipulation
Exercise 3: Write pseudo code and draw flow chart (using function) calculate the frequency of
one character in a string.
20