Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

1 SUBPROGRAM IN PASCAL PROCEDURE Lecture 5 CS1023.

Presentasi serupa


Presentasi berjudul: "1 SUBPROGRAM IN PASCAL PROCEDURE Lecture 5 CS1023."— Transcript presentasi:

1 1 SUBPROGRAM IN PASCAL PROCEDURE Lecture 5 CS1023

2 2 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? }

3 3 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.

4 4 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.

5 5 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

6 6 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.

7 7 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

8 8 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.

9 9 Example: Volume of a Cylinder program volumeFinder; procedure WriteCylinderVolume( r, h : real); var volume : real;{local variable for WriteCylinderVolume} begin volume := * 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.

10 10 Example: Swap two variables Procedure swap(var x : integer, var y: integer) Var temp: integer; Begin temp:= x; x:= y; y:= temp;

11 11 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

12 12 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  2 4 / 5

13 13 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


Download ppt "1 SUBPROGRAM IN PASCAL PROCEDURE Lecture 5 CS1023."

Presentasi serupa


Iklan oleh Google