1 / 13

SUBPROGRAM IN PASCAL

SUBPROGRAM IN PASCAL. PROCEDURE Lecture 5 CS1023. 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);

neka
Download Presentation

SUBPROGRAM IN PASCAL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SUBPROGRAM IN PASCAL PROCEDURE Lecture 5 CS1023

  2. Example of Procedureusing 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. 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. 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. 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? 4000 number num

  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. 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). 6000 answer number n result

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

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

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

  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 24/5

  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

More Related