11/11/2024
User –defined Function in Python
Pemograman Komputer 1
Writing your own function
The body of a
function is
indented
2
1
11/11/2024
Example of a user-defined function
def defining a function
called hello_word,
followed by ():
The function action is to
print Hello World
Parts of the function:
The signature has four
parts:
(1) def,
(2) the name of the
function (multiply),
(3) input parameter (x
and y) and :
The body is the code
lines that are indented
2
11/11/2024
Return
A function can be ended with return
Return statement means a value will be returned when the
function is called
3
11/11/2024
Body indentation, wrong way of doing it
Correct
Calling user defined function
Incorrect
4
11/11/2024
Even and odd function-user defined
What would be the output of these lines?
10
5
11/11/2024
User-defined function without parameter
11
User-defined function dengan default
paramater Jika tidak diberikan parameter maka defaultnya
adalah hitam
Warna default hitam diganti dengan merah
Hasil eksekusi baris ke-empat
Hasil eksekusi baris ke-lima
12
6
11/11/2024
Exercise
13
User-defined function with user input
14
7
11/11/2024
Exercise
1. Buatlah sebuah program untuk menghitung luas segitiga dengan parameter tinggi dan alas
2. Ubah program di bawah ini menjadi fungsi dengan parameter totalHours dan regularHours
15