var

advertisement
SUBPROGRAM IN PASCAL
PROCEDURE
Lecture 5
CS1023
1
Example of Procedure
using pass-by-value
program doubleTrouble;
procedure double (num : integer);
begin
num := num * 2;
writeln('The double is ', num);
end;
var
number : integer;
begin
number := 3;
double(number);
writeln('The number is ', number);
end.
{What is the output? }
2
Pass by reference
The default for procedure parameters is pass by value.
Sometimes, we want the procedure to modify the actual
parameter.
We use pass by reference to pass the memory location of the
actual parameter to the procedure.
Use the key word, var, in the formal parameter to indicate
that the parameter is pass-by-reference.
3
Example
program doubleTrouble;
procedure double2(var num : integer);
begin
num := num * 2;
writeln('The double is ', num);
end;
var
number : integer;
begin
number := 3;
double2(number);
writeln('The number is now ', number);
end.
4
Execution of doubleTrouble
When double2( ) is called, the address of number is passed to
the parameter, num.
num and number label the same location in memory.
When the value of num changes, the value of number also
changes (because they label the same memory location).
What is the output?
number
num
4000
5
More than one parameter
program CalcCube;
procedure Cube( n : integer; var result :
integer);
begin
result := n * n * n;
end;
var
number : integer;
answer : integer;
begin
writeln('Enter a number: ');
readln(number);
Cube(number, answer);
writeln('The cube of ', number, ' is ',
answer);
end.
6
Matching up parameters
Actual parameters are matched up in order with the formal
parameters.
In the previous example, the value of number is passed to n
(pass-by-value).
The address of answer is passed to result (pass-by-reference).
number
n
answer
result
6000
7
Local Variables for Procedures
Procedures can have variables in addition to parameters.
A variable declared within a procedure definition is local to
that procedure.
A local variable is invisible to other procedures and the main
program.
8
Example: Volume of a Cylinder
program volumeFinder;
procedure WriteCylinderVolume( r, h : real);
var
volume : real;
{local variable for
WriteCylinderVolume}
begin
volume := 3.14159 * r * r * h;
writeln( 'The volume is ', volume);
end;
var
radius, height : real; {variables for main program}
begin
writeln('Enter the radius and the height: ');
readln(radius);
readln(height);
WriteCylinderVolume(radius, height);
end.
9
Example: Swap two variables
Procedure swap(var x : integer, var
y: integer)
Var
temp: integer;
Begin
temp:= x;
x:= y;
y:= temp;
10
Exercises
• Tuliskan prosedur PUTAR3BIL untuk memutar tiga buah
bilangan integer dengan menggunakan prosedur swap.
Contoh:
– Jika a berisi 1, b berisi 2, dan c berisi 3, maka hasilnya:
a berisi 3, b berisi 1, dan c berisi 2.
• Tuliskan prosedur untuk menyederhanakan bilangan
rasional atau pecahan berbentuk a/b dimana a dan b adalah
bilangan bulat dan a<b. contoh: jika diberikan pecahan 5/10
 ½.
• Tuliskan algoritma untuk menjumlahkan dua buah matriks.
Ukuran matriks MxN diberikan oleh pengguna. Algoritma
tersebut dibangun dari tiga prosedur, yaitu prosedur
masukan dua buah matriks, prosedur menjumlahkan kedua
matriks tersebut, dan prosedur menampilkan hasil
penjumlahannya.
11
Exercises
• Tuliskan prosedur untuk menyederhanakan
pecahan x/y menjadi bentuk a b/c dimana x
dan y adalah bilangan bulat dan x > y.
Contoh: 14/5  24/5
12
Referensi
• Liem Inggriani, Diktat kuliah IF223
Algoritma dan Pemrograman, Jurusan
Teknik Informatika ITB, 1999
• DT/266/1, Programming Language,
Departement of Computer Science.
• Data structure, CSCI 262, Lecture 2 Classes
and Abstract Data Types
13
Download