9- Function - revisi 1 (1) (1)
9- Function - revisi 1 (1) (1)
Function
Remember our first Hello World program?
list-social-media-story-template_53876-
114936.jpg
list-social-media-story-template_53876-
114936.jpg
This code relies on functions from module math
print(ceil(1.23))
print(pow(5,3))
print(factorial(4))
list-social-media-story-template_53876-
114936.jpg
Programming without functions
# compute factorial of 3
result = 1
for i in range(1,4):
result *= i
print("Factorial of", 3, "is", result)
# compute factorial of 4
result = 1
for i in range(1,5):
result *= i
print("Factorial of", 4, "is", result)
Functions: What and why
A function is a set of instructions you can call to carry out a specific task
Benefits of functions
➜ Avoid repeating code
➜ Improved reliability
➜ Improved modularity
Let's create a factorial function
def usd_to_idr(amount):
amount_in_idr = amount * 15000.0
return amount_in_idr
DIY: Let's create a currency converter function (assume 1 USD = 15000 IDR)
def usd_to_idr(amount):
amount_in_idr = amount * 15000.0
return amount_in_idr
DIY: Let's create a currency converter function (assume 1 USD = 15000 IDR)
def usd_to_idr(amount):
"""Convert from the USD currency to IDR currency.""" docstring
amount_in_idr = amount * 15000.0
return amount_in_idr
help(usd_to_idr)
DIY: Let's create a currency converter function (assume 1 USD = 15000 IDR)
def usd_to_idr(amount):
"""Convert from the USD currency to IDR currency.""" docstring
amount_in_idr = amount * 15000.0
return amount_in_idr
help(usd_to_idr)
A function with no parameters and no return
def hi():
print("Hi!")
What goes wrong?
hi()
def hi():
print("Hi!")
What goes wrong?
def hi():
print("Hi!")
hi("Indonesia")
What goes wrong?
def hi():
print("Hi!")
hi("Indonesia")
Modify this function to say hi to some name!
def hi(name):
print("Hi, " + name + "!")
Modify this function to say hi to some name!
def hi(name):
print("Hi, " + name + "!")
[1] print(hi("Python"))
Modify this function to say hi to some name!
def hi(name):
print("Hi, " + name + "!")
[1] print(hi("Python"))
Quiz time: Distance between two points
import math
x1 = float(input())
y1 = float(input())
x2 = float(input())
y2 = float(input())
print(distance(x1, y1, x2, y2))
A function can have multiple return statements
print(power(float(input()), int(input())))
Default values
• Not all variables are accessible from any part of our program!
• Variable scope: The part of a program where a variable
is accessible.
Scope matters
x = "Hello, Python!"
def fun():
x = 5
print(f"Inside 'fun', x has the value {x}")
fun()
print(f"Outside 'fun', x has the value {x}")
Scope matters
x = "Hello, Python!"
def fun():
x = 5
print(f"Inside 'fun', x has the value {x}")
fun()
print(f"Outside 'fun', x has the value {x}")
From main to fun
x = 5
def fun(y):
print(y)
fun(x)
From main to fun
x = 5
def fun(y):
print(y)
fun(x)
main fun
x 5 y
From main to fun
x = 5
def fun(y):
y = 7
print(y)
fun(x)
print(x)
From main to fun
x = 5
def fun(y):
y = 7
print(y)
fun(x)
print(x)
main
x 5
fun
7 y
What's wrong with this code?
def hi_fitri():
name = "Fitri"
print("Hi, " + name + "!")
hi_fitri()
print(name)
What's wrong with this code?
def hi_fitri():
name = "Fitri"
print("Hi, " + name + "!")
hi_fitri()
print(name)
Quiz time: What's the output?
a = 0
if a == 0:
b = 1
def my_function(c):
d = 3
print(c)
print(d)
[1] my_function(7)
[2] print(a)
[3] print(b)
[4] print(c)
[5] print(d)
Quiz time: What's the output?
a = 0
if a == 0:
b = 1
def my_function(c):
d = 3
print(c)
print(d)
[1] my_function(7)
[2] print(a)
[3] print(b)
[4] print(c)
[5] print(d)
Global variables
main
main
"Oh, no :(" s
Fungsi Rekursif
Recursion
• it is a function that calls itself
def function():
x = 10
function()
countdown(5)
2. Sub-problem kemudian dipecah hingga menjadi sangat sederhana sehingga dapat diselesaikan tanpa
pembagian lebih lanjut (Base Case).
def factorial_recursive(n):
# Base case: 1! = 1 or n == 0
if (n == 1) or (n == 0):
return 1
factorial_recursive(10)
FIBONACCI SEQUENCE
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181
Apa pola nya?
FIBONACCI SEQUENCE
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181
Kode :
Tugas
Thanks
and keep practicing!