Sie sind auf Seite 1von 30

KUG1C3 Dasar Algoritma dan

Pemrograman

Procedure

What is Procedure?
To understand the function of a procedure take a
note at the following illustration

Im Hungry! I want to eat!


Program eat_food
Kamus
Algoritma
output(im hungry)
output(get a plate)
output(grab any food)
output(eat the food)

Now Im doing something else


Program eat_food
Kamus
Algoritma
output(im hungry)
output(get a plate)
output(grab any food)
output(eat the food)
output(doing
output(doing
output(doing
output(doing

this)
that)
this again)
that again)

Im Hungry again, I want to eat


Program eat_food
Kamus
Algoritma
output(im hungry)
output(get a plate)
output(grab any food)
output(eat the food)
output(doing
output(doing
output(doing
output(doing

this)
that)
this again)
that again)

output(im hungry)
output(get a plate)
output(grab any food)
output(eat the food)

Then Im doing something else


again until Im hungry again
Program eat_food
Kamus
Algoritma
output(im hungry)
output(get a plate)
output(grab any food)
output(eat the food)
output(doing
output(doing
output(doing
output(doing

this)
that)
this again)
that again)

output(im hungry)
output(get a plate)
output(grab any food)
output(eat the food)
output(doing homwork)
output(doing laundry)
output(im hungry)
output(get a plate)
output(grab any food)
output(eat the food)

Eating process
Program eat_food
Kamus
Algoritma
output(im hungry)
output(get a plate)
output(grab any food)
output(eat the food)
output(doing
output(doing
output(doing
output(doing

this)
that)
this again)
that again)

output(im hungry)
output(get a plate)
output(grab any food)
output(eat the food)
output(doing homwork)
output(doing laundry)
output(im hungry)
output(get a plate)
output(grab any food)
output(eat the food)

Same process sequence,


happened again and again
To write it again and again
is wasting

Make it a Procedure
Program eat_food
Kamus
Algoritma
output(im hungry)
output(get a plate)
output(grab any food)
output(eat the food)
output(doing
output(doing
output(doing
output(doing

this)
that)
this again)
that again)

output(im hungry)
output(get a plate)
output(grab any food)
output(eat the food)
output(doing homwork)
output(doing laundry)
output(im hungry)
output(get a plate)
output(grab any food)
output(eat the food)

Procedure EATING
Kamus
Algoritma
output(im hungry)
output(get a plate)
output(grab any food)
output(eat the food)

Why dont we just pull those processes out


And make it as a procedure

Calling a Procedure
Program eat_food
Kamus
Algoritma
EATING
output(doing
output(doing
output(doing
output(doing

this)
that)
this again)
that again)

EATING
output(doing homwork)
output(doing laundry)
EATING

Procedure EATING
Kamus
Algoritma
output(im hungry)
output(get a plate)
output(grab any food)
output(eat the food)

Then we just have to call the procedure


to have the same result

Definition
A series of algorithmic instructions that are
named, and will produce a defined effect.
Specification
procedure name and parameters (if any)
initial state (IS) and final state (FS)
The procedure is defined in the dictionary of the main
program

When called"
Parameter association
IS and FS guarantees that the execution of the program
will result in the expected net effect

Definition
Every Procedure must be:
Defined the specification and written the code
Called when executed

Parameter
Without Parameter

using variable in main program as global


variable
must be careful because anyone can change
the value of global variable

With Parameter

With the same code, will produce different


result when executed according to the input
parameter
Parameter defined in procedure is called
formal parameter
Parameter written when calling the procedure
is called actual parameter

Local Variable vs. Global


Variable
Defining variable in Global Variable
Dictionary will make the variable available
for the program overall and for all
procedures/functions defined in main
algorithm
Defining variable in Local Variable
Dictionary will make the variable only
available within the scope of the
functions/procedures itself, and functions /
procedures defined in it

Defining Procedure

Must be defined
in title area

May be empty,
If any, must be defined
the variable and the type

How to Call a Procedure


When "called" association occurs
between actual and formal
parameters
list-name formal and actual
parameters must be the same
number, order and type.

Calling the Procedure


Actual parameter for input can use
Variables or constants that have been defined
in Dictionary
Direct value or value from other function or
expression

Actual Parameter for output must use


variable name
Actual Parameter for input/output
also must use variable name

Example Global Variable


Program simple_calculation
Kamus
A : integer
B : integer
C : integer
procedure simple_calc

Procedure simple_calc
Kamus
Algoritma
A 10 + B - C
C C + 4

Will use A,B,C from Global variable


All changes will affect global variabl

Algoritma
input(B,C)
Lets say we input 3 for B and 5 for C
simple_calc
output(A,B,C)
Output A = 8, B = 3, C = 9
input(B)
Then we change B = 2
simple_calc
output(A,B,C)
Output A = 3, B = 2, C = 13

Example Local Variable


Program simple_calculation
Kamus
A : integer
B : integer Global
C : integer
procedure simple_calc

Procedure simple_calc
Kamus
A, B, C : integer
Local
Algoritma
input (B,C)
A 10 + B - C
A = 10 stays
C C + 4
Will not be o
output (A,B,C)

Algoritma
input(B,C)
3 for B and 5 for C
simple_calc
output(A,B,C)
Output A=?, B=3, C=5
input(B)
Then we change B = 2
simple_calc
output(A,B,C)

Output A=?, B=2, C=5

Will use A,B,C from Local


variable. All changes will
stay inside the procedure
and will not affect global
variable.

Example Global and Local Variable


Program simple_calculation
Kamus
A : integer
B : integer
C : integer
procedure simple_calc

Procedure simple_calc
Kamus
B : integer
Algoritma
A 10 + B - C
C C + 4

Algoritma
Will use A,C from Global
input(B,C)
3 for B and 5 for C
Variable,
simple_calc
and B from Local Variable
output(A,B,C)
Output A=5, B=3, C=9
input(B)
Then we change B = 2
simple_calc
output(A,B,C)
Output A=1, B=2, C=13

Example Using Parameter


Procedure simple_calc(input:B,C:integer; output:A:integer)
Kamus
Algoritma
A 10 + B - C
C C + 4

Example Using Parameter


Program simple_calculation
Kamus
A,B,C : integer
procedure
simple_calc(I:B,C:integer;
O:A:integer)

Procedure simple_calc(
Input:B,C:integer;
Output:A:integer)
Kamus
Algoritma
A 10 + B - C
C C + 4

Algoritma
3 for B and 5 for C
A,B,C will use values
input(B,C)
from parameter
simple_calc(B,C,A)
output(A,B,C) Output A=8,B=3, C=5
Only value A that will be returned
to global variable,
input(B)
Then we change B = any
2 changes in parameter input
simple_calc(B,C,A)
will not affects in global
output(A,B,C)
Output A=7, B=2, C=5

Careful with Parameter Calling


Program simple_calculation
Kamus
A,B,C : integer
procedure simple_calc(input
B,C:integer; output
A:integer)

Procedure simple_calc(
input B,C:integer; output
A:integer)
Kamus
Algoritma
A 10 + B - C
C C + 4

Algoritma
3 for B and 5 for C
input(B,C)
simple_calc(B,C,A)
output(A,B,C)
Output A=8, B=3, C=5
simple_calc(C,B,A)
output(A,B,C)
simple_calc(A,B,C)
output(A,B,C)

Swap B and C when calling


Output A=12, B=3, C=5
Swap A, B, and C
Output A=0, B=3, C=7

Formal Parameter
List of variable name to define the
procedure, so that it can be executed
using different variable
type parameter :
input, receive input value from the given variable to
the procedure
output, return output value from procedure to the
assigned variable
Input/output, receive input value from the given
variable, and output the return value to the same
variable

Actual Parameter
The assigned variable or value that used when calling a
procedure
Can be variable or direct value for input parameter
Must be variable for output parameter, so that the return
value from procedure can be stored in the assigned variable
When calling a procedure, the type of actual parameter must
match the formal parameter
input parameter must defined the initial value
output parameter need not be defined in value
input/output parameter must defined the initial value.

At the time of the call, there was an association between


formal and actual parameters corresponding position
(posisition based), then type var or value (price) should be
compatible

Formal vs. Actual Parameter


Program simple_calculation
Kamus
A,B,C : integer
procedure simple_calc(input
B,C:integer; output
A:integer)
Actual Parameter
Algoritma
input(B,C)
simple_calc(B,C,A)
output(A,B,C)
simple_calc(B,20,A)
output(A,B,C)

Procedure simple_calc(
input B,C:integer; output
A:integer)
Kamus
Algoritma
A 10 + B - C
C C + 4
Formal Parameter

Modular Programming
Program is divided into modules that was welldefined in the form of procedures
Should have a clear definitions and scope so that
can be invoked independently
Easier for programmers to "read" a very large
program
In some programming languages, the programmer
does not need to know anything about the
"content" of a procedure / function because it has
been "reserved" by the programming language.
Programmers just need to "call" it

Question?

Procedure vs Function
Fungsi dan Prosedur sama-sama sub-program
Fungsi untuk mentransformasikan nilai, sedangkan
prosedur untuk melakukan serangkaian pekerjaan
Prosedur tidak memiliki nilai hasil ()
Oleh karena itu, pemanggilan prosedur hanya
menggunakan Nama dan parameter saja tidak dilakukan
hal lainnya (missal assignment atau Output ke layar)
Di prosedur ada 3 jenis parameter, yaitu : Input, Output,
Input/Output
Input hanya bisa menerima nilai
Output hanya bisa mengeluarkan
Input/Output bisa menerima dan mengeluarkan

Procedure Yes(Input A:integer, Output B:Integer, Input/Output C:integer)


Kamus Lokal
Algoritma
A A+5
B15
CC+5
Program Tes
Kamus
Procedure Yes(Input A:integer, Output B:Integer, Input/Output C:integer)
X,Y,Z : Integer
Algoritma
X5, Y10, Z15
Yes(X,Y,Z)
Output(X,Y,Z) [HASIL OUTPUT ADALAH 5,15,20]

LATIHAN
Procedure Yes(Input/Output B,A:integer)
Kamus Lokal
C : integer
Algoritma
Yes2(B)
C B
BA
AC
Program Tes
Kamus
Procedure Yes(Input/Output A:integer,
Input/Output B:Integer)
A,B : Integer
Algoritma
A5, B10
Yes(A,B)
Output(A,B)

Procedure Yes2(Input
A : integer)
Kamus Lokal
Algoritma
AA*2
Output(A)

Das könnte Ihnen auch gefallen