Python - Functions
Reference :
Automate The Boring Stuff With Python
(Practical Programming For Total Beginners)
By Al Sweigart
Python - Ir. Endang Sunandar, [Link] 1
Functions
• Functions is like a mini-program within a program
def hello():
print(“hai..”)
print(“hai..”)
print(“come here..”)
hello()
hello()
hello()
Python - Ir. Endang Sunandar, [Link] 2
Penjelasan script
• The first line is a def statement
• Which defines a function named hello()
• The code in the block that follows the def
statement is the body of the function
• This code is executed when the function is
called, not when the function is first defined
Python - Ir. Endang Sunandar, [Link] 3
• Since this program calls hello() three times,
the code in the hello() function is executed
three times
Python - Ir. Endang Sunandar, [Link] 4
Output like this :
hai..
hai..
come here..
hai..
hai..
come here..
hai..
hai..
come here..
Python - Ir. Endang Sunandar, [Link] 5
def statement with Parameters
• When you call the print() or len() function, you
pass in values, called arguments
• By typing them between the parentheses
• You can also define your own functions that
accept arguments
Python - Ir. Endang Sunandar, [Link] 6
Script like this
def hello(name):
Print(“hello “,name)
hello(‘Alice’)
hello(‘Bob’)
• When you run this program :
hello Alice
helo Bob
Python - Ir. Endang Sunandar, [Link] 7
Return Values and return Statements
• When creating a function using the def
statement, we can specify what the return
value should be with a return statement.
• For examples, the following program defines a
function that returns a different string
depending on what number it is passed as an
argument
Python - Ir. Endang Sunandar, [Link] 8
import random
def getAnswer(answerNumber):
if answerNumber== 1:
return “Januari”
elif anserNumber==2:
return “Februari”
elif answerNumber==3
return “Maret”
elif answerNumber==4
return “April”
r = [Link](1,4)
fortune=getAnswer(r)
print(fortune)
Python - Ir. Endang Sunandar, [Link] 9
TUGAS – Functions Return Values
• Buatlah sebuah functions mengenai return
values dgn ketentuan sbb :
– Menggunakan metode input masukan angka dari
1 smp12
– Setelah itu tampilkan nama bulannya
– Misal : angka 6, bulannya : Juni
Python - Ir. Endang Sunandar, [Link] 10
Keyword Arguments and print
Hello
dunia
• The two string appear on separate lines because the print()
function automatically adds a newline character to end of the
string its passed
• However we can set the end keyword argument to change this
to a different string
print(“hello”,end=‘’”)
print(“dunia”)
• Hasil sprt ini :
hellodunia
Python - Ir. Endang Sunandar, [Link] 11
Keyword Arguments and print
print(“cats”,”dog”,”cow”)
• Outputnya sprt ini :
cats dog cow
• Dengan menggunakan keyword sep sprti ini :
print(“cats”,”dog”,”cow”, sep=“,”)
• Outputnya sprt ini :
cats,dog,cow
Python - Ir. Endang Sunandar, [Link] 12
Local variables cannot be used in
the Global Scope
def spam():
eggs=31337
spam()
print(eggs)
Error..!
Python - Ir. Endang Sunandar, [Link] 13
Local scopes cannot use variables in other
local scopes
def spam():
eggs=99
bacon()
print(eggs)
def bacon():
ham = 101
eggs=0
spam()
Python - Ir. Endang Sunandar, [Link] 14
Global Variables Can be read
from a Local Scope
def spam():
print(eggs)
eggs = 42
spam()
print(eggs)
Python - Ir. Endang Sunandar, [Link] 15
Local and Global Variables with
the Same Name
def spam():
eggs = 'spam local'
print(eggs) # prints 'spam local‘
def bacon():
eggs = 'bacon local'
print(eggs) # prints 'bacon local‘
spam()
print(eggs) # prints 'bacon local‘
eggs = 'global‘
bacon()
print(eggs) # prints 'global'
Python - Ir. Endang Sunandar, [Link] 16
When we run this program, it outputs the
following
bacon local
spam local
bacon local
global
Python - Ir. Endang Sunandar, [Link] 17