Algo Pekan 12
Algo Pekan 12
Informatics Engineering
Sekolah Tinggi Teknologi Bandung
2020
Python Functions
• A function is a block of code which only runs when it is called.
• You can pass data, known as parameters, into a function.
• A function can return data as a result.
my_function()
my_function("Emil")
my_function("Tobias")
my_function("Linus")
my_function("Emil", "Refsnes")
my_function("Emil")
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
Algorithm & Programming @ STT Bandung 2020
Passing a List as an Argument
You can send any data types of argument to a function (string, number,
list, dictionary etc.), and it will be treated as the same data type inside
the function. E.g. if you send a List as an argument, it will still be a List
when it reaches the function. Example :
def my_function(food):
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
my_function(fruits)
print(my_function(3))
print(my_function(5))
print(my_function(9))