Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

w4 Function Procedur

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Algoritma dan Pemograman

04– Function and Procedure

9/8/2019 1
Outline

What is modular programming, benefit of modular programming, function,


and procedure?

Calling Procedure and importing user define procedure

Actual Parameter(Argument) and Formal Parameter(Parameter)

List in python

IEH2B3 | Algoritma dan Pemrograman Komputer 2


What is modular programming
“to achieve a big objective, so it must be divided into small objective
until this small objective can be achieved by condition and potensial
that owned now”. (Al-Khuwarizmi).

A big program is harder to understand(read) and harder to trace for an


error. So, it is better for a program to be divided into some smaller
subprograms. A best subprogram hides detail operation form a part of
program that shouldn’t know about that subprogram.

This dividing technique is called modular programming. Some


programming language call a subprogram as subroutine, modul,
procedure and function.
IEH2B3 | Algoritma dan Pemrograman Komputer 3
What is procedure/function
In programming, procedure is a modul of program that do a specific
task/activity and produce a netto effect. A netto effect is known by
comparing initial state and final state.

So, for every procedure we need to define initial state before instructions
from procedure is done and final state that is wished after instructions in
procedure have been done.

In some programming language, procedure and function is same but


procedure doesn’t need to return value and function need to return value.

In some programming languages, they only have function.


IEH2B3 | Algoritma dan Pemrograman Komputer 4
What is procedure/function
When you perform a task that will be used multiple times you need to
define procedure / function.

After you define the procedure / function and then you want to
perform that procedure, so you can call the procedure / function.

Using procedure / function you can reuse same task multiple times,
easy to understand, easy to write, easy to test, and easy to fix/debug.

IEH2B3 | Algoritma dan Pemrograman Komputer 5


Procedure structure in algorithmic notation

procedure ProcedureName (parameter declaration, if needed) Basically, procedure


structure is same as
{procedure specification, consist of declaration about what is this algorithm structure
procedure do.
Initial State: condition before procedure executed.
which consist of:
Final State: condition after procedure } a. Header Section
b. Declaration Section
DECLARATION c. Algorithm Section
{all variable name that being used in this procedure and valid in
this procedure}

ALGORITHM Parameter declaration could be input or


output parameter
{procedure body, consist of sequence of instruction}
Example Procedure structure in algorithmic notation
procedure HitungRataRata(input N: integer, output u:real)
{ Menghitung rata-rata N buah bilangan bulat yang dibaca dari piranti masukan.}
{ K.Awal : N sudah berisi banyaknya bilangan bulat, N > 0 }
{ K.Akhir : u berisi rata-rata seluruh bilangan. }
DEKLARASI
x : integer { data bilangan bulat yang dibaca dari keyboard }
k : integer { pencacah banyak bilangan }
ALGORITMA :

jumlah ← 0 { inisiasi }
for k ← 1 to N do
read(x)
jumlah ← jumlah + x
endfor

u ← jumlah/N

IEH2B3 | Algoritma dan Pemrograman Komputer 7


Example Function structure in algorithmic notation
function H (input u,v,w: integer) → integer
{ Mengembalikan nilai H(u,v,w)= 2uv2 + 3vw + 10w.}
DEKLARASI
{ tidak ada}
ALGORITMA :
return 2*u*v*v + 3*v*w + 10*v

IEH2B3 | Algoritma dan Pemrograman Komputer 8


Function structure in python

def ProcedureName(): In python you don’t


“”” docstring/procedure specification, consist of declaration
about what is this procedure do. need declaration
Initial State: condition before procedure executed. section, you can
Final State: condition after procedure””” declare/use variable in
#body algorithm the body algorithm.
Calling Procedure
When you already define a procedure, you can access the procedure by calling
the procedure name from main program or other module.

The easiest way to create and use procedure is by defining the procedure in
same file with the main program.

Example:
def greet_user():
“””Display a simple greeting.””” This is Example of calling
print(“hello”) procedure

greet_user();
IEH2B3 | Algoritma dan Pemrograman Komputer 10
Parameter and Argument
Parameter is a piece of information the function needs to do its job.

Argument is a piece of information that is passed from a function call to


a function.

IEH2B3 | Algoritma dan Pemrograman Komputer 11


Importing User Define Procedure
When you develop a program in a team or you want to reuse your older
procedure you can import that procedure file into other main program
as long as those file located in the same directory.
Example: Example of parameter
student.py
def add_student_to_class(name,nim):
print(“student with name ”+ name+” and his/her nim is ”+nim+” has
been added to class”);

Example of Argument
Registration.py
Import student
student.add_student_to_class(“budi”,”1134849504”)
IEH2B3 | Algoritma dan Pemrograman Komputer 12
List in Python
List in python or know as Array in other language is an identical variable
that arranged in rows so that the address are linked and contiguous.

Array or list representation should be like this


0 1 2 3 4 5 6

To declare list in python we use this sign[]


Example:
list = [ ‘hamburger’, ‘pizza’, ‘doughnut’]

IEH2B3 | Algoritma dan Pemrograman Komputer 13


Using List in python
Example:
List = [ 40, 50, 60, 75, 55, 80]

list representation in table should be like this


0 1 2 3 4 5
40 50 60 75 55 80

If we need to get 60 from the list we can use this code

>>> list[2]

IEH2B3 | Algoritma dan Pemrograman Komputer 14


Another List function that you can try
Example: List = [1, 2, 3, 4, 5]
Function Expalnation Function Use Example Output
Append To add item to the list List.append(6) List = [ 1, 2, 3, 4, 5, 6]
Count To count a value in a list List.count(3) 1
Clear To clear a list List.clear() None
Pop To take and remove the last member of List.pop() 5
the list out
Remove To remove a value of the list List.remove(3) List = [1, 2, 3, 5]
Insert To insert a value in following index List.insert(2,-3) List = [1, 2,-3, 3, -4,5]
Sort To sort list ascending (for descending use List.sort() List = [1, 2, 3, 4, 5]
argument false)
Reverse To Reverse element of the list List.reverse() List = [5, 4, 3, 2, 1]

IEH2B3 | Algoritma dan Pemrograman Komputer 15


Try out
Buatlah program kalkulator dengan 4 buah menu operasi dan 1 menu keluar.
• 4 menu operasi yang dapat dilakukan oleh operator tersebut yaitu kali, bagi, tambah,
kurang.
• jika salah satu menu tersebut dipilih maka akan pindah ke prosedur masing-masing,
jika menu keluar dipilih maka program akan keluar.
• Untuk setiap operasi, program akan meminta dua buah input angka untuk dikalkukasi.

Buatlah program yang memiliki 4 menu yaitu menu input, menu lihat nilai maksimum,
lihat nilai minimum dan keluar.
• Menu input akan menambahkan elemen ke dalam list
• Menu maksimum merupakan prosedur untuk mencari nilai maksimum dalam list
• Menu minimum merupakan prosedur untuk mencari nilai minimum dalam list
• Menu keluar digunakan untuk mengakhiri program

IEH2B3 | Algoritma dan Pemrograman Komputer 16

You might also like