Kamis, 20 Januari 2011

FUNGCITION

FUNCTION
1. Preliminary
In Pascal, modular programming can be done in a variety of methods. Among the modules in one program, or in a separate module from the main program file. in a separate module, can be made unit which is a separate library. While in one program, can use the procedure or function. The use of the procedure have been discussed in previous modules.
Function is not much different than the procedure, because it is a sub-program implementation. However, the function must be declared with the type. Type shows the type of result declaration of the function. Type the dtulis at the end of the function declaration, which is preceded by a semicolon. Function blocks together with block procedure Begin backup word that begins and ends with the word End reserves and a semicolon.
At the function, the value passed back there in the name of the function (for the procedure, the parameters passed by reference). The name of the function must be used to accommodate the results of which will be sent from the function. Because the return value is in the name of the function, the function can be directly used to print the results, or value of the function can also be directly transferred to another variable identifier. While the procedure, the procedure name can not be used directly, which can be directly used is the parameter that contains the return value.
2. The word used reserve
Program: The specification name of the program and its parameters, is decorative and not affect the overall program itself Uses: define the name of the unit to be referenced by the program Var: linking an identifier and its type with a location in memory where the values of these types can be stored Function: the programs that do koputasi and return a value Begin: starting a program block End: end a program block If: mention the conditions for a statement that can be executed Then: mention the statement of action must be executed when the conditions are met Else: mention the statement of actions that can be executed while another action statement is not executed because the condition is not fulfilled. Repeat: define statement or block of statements that will be repeated until the state stops filled Until: defines the state of rest for the loop that begins with the Repeat Do: used to describe the action to perform For: defines the initial value iteration To: defines the value of the final iteration (iteration positive)
3. The procedure used
Write: writing a variable into a component file in a text file used to write one or more values to a file Writeln: executing the write procedure, then pulled out a marker of end-of-line (EOL) to the program files Read: read a file into a variable component, in a text file used to read one or more values into one or more variables Readln: executes a read procedure, and then move to the next line in program files
4. Functions used
Exp: to calculate the value of ex Ln: Ln standard function to calculate the value of Ln X 


1.   SIMPLE PROGRAM
a.       Program pertama
Program m8c1;
Uses wincrt;
Function hitung(var a, b, c : integer): integer;
Begin
     Hitung := a + b;
     c := a * b;
End;
Var
     x, y, z : integer;
Begin
     write ('Nilai x? ');
     readln (x);
     write ('Nilai y? ');
     readln (y);
     writeln;
     writeln (x, ' + ', y, ' = ', hitung(x,y,z));
     writeln (x, ' x ', y, ' = ', z);
End.

PROCEDURE FUNCITION

PROCEDURE

1. Preliminary In programming, when the size and complexity of the program is small, it is no problem when used in conventional encoding method that is monolithic. But when the program becomes increasingly large and complex, monolithic method would be very difficult for programming and program modifications, not to mention tracking when problems occur. Making the program with a variety of functions and capabilities will also be difficult, because it must always pay attention to the overall program. 


It required a modular programming method, where the programmer can separate certain processes to a separate part of the program. This separation allows the development of complex programs becomes easier, because certain processes have been isolated to minimize the complexity of the overall program code. In Pascal, one way to perform the separation is to use the procedure. 


The procedure is a separate processing code that is placed in a separate block that serves as a subprogram (a program that is part of the overall program.) With this procedure, the program can be more easily made and more easily understood so that more easily developed. Because the procedure also accommodates the provision and return value, then the whole program will also still be able to manipulate the value as well as a monolithic program.
2. The word used reserve Program: The specification name of the program and its parameters, is decorative and not affect the overall program itself 


Uses: define the name of the unit to be referenced by the program Var: linking an identifier and its type with a location in memory where the values of these types can be stored Begin: starting a program block End: end a program block 


Procedure: part of a program that runs a specific process, often based on a set of parameters If: mention the conditions for a statement that can be executed Then: mention the statement of action must be executed when the conditions are met Repeat: define statement or block of statements that will be repeated until the state stops filled Until: defines the state of rest for the loop that begins with the Repeat Case: mention the statement expression that searches for the appropriate conditions based on the condition of the available options statement 


Of: the word is used in conjunction Case reserves to mention the choice of appropriate conditions and actions to be executed when a condition selected Do: used to describe the action to perform For: defines the initial value iteration To: defines the value of the final iteration (iteration positive) Record: used to declare the record contains a collection of data from various types of data Type: used to declare an identifier that has a specific data type
3. The procedure used Write: writing a variable into a component file in a text file used to write one or more values to a file Writeln: executing the write procedure, then pulled out a marker of end-of-line (EOL) to the program files Read: read a file into a variable component, in a text file used to read one or more values into one or more variables Readln: executes a read procedure, and then move to the next line in program files Clrscr: cleaning the screen and return the cursor to the top left corner 




a.       Program pertama
Program m7c1;
uses wincrt;
var
     pil : char;
procedure menu;
begin
     clrscr;
     writeln ('MENU');
     writeln ('1. Bakso');
     writeln ('2. Bakmi');
     writeln ('3. Siomay');
     writeln ('4. Soto');
     writeln ('0. Bayar');
end;
procedure kerja (n:char);
begin
     case n of
     '1': begin
               write ('Pesan Bakso'); readln;
          end;
     '2': begin
               write ('Pesan Bakmi'); readln;
          end;
     '3': begin
               write ('Pesan Siomay'); readln;
          end;
     '4': begin
               write ('Pesan Soto'); readln;
          end;
     '0': begin
               writeln ('Bayar dan bye');
          end;
     end;
end;
begin
     repeat
           menu;
           writeln;
           write ('Pilihan anda? '); readln (pil);
           kerja (pil);
     until pil = '0';
end.

RECORD AND ARRAY OF RECORD

RECORD & ARRAY OF RECORD


1. Preliminary Using the array, you can only save the values you entered into the memory during the program running in the same type. For example, you will not be able to save data in addition to integer form if you already have an array defined as type integer. So you can store various types of data when your program runs, you need other mechanisms to do this in Pascal, in this case is a record. With records, you can store a value in the form of integer, real, char, string, or other data types together. This program allows users to store various data simultaneously for the program is still running, and can use the data for various purposes. To note, the record usually consists of several fields only, making it difficult to use to store data in a number of arrays. This is because there is no mechanism in the record index. For that use a combination of arrays and records, called with an array of records. Array of records makes it possible to store more data (as much as the array index), each containing data 'property' which consists of various types of data (as many fields from record). Because it is very important to master an array of records as a basis for programming that involves a lot of data at once which consists of various types of data. If this section has been mastered, you are ready to manipulate the data even further through a programming using Pascal.
2. The word used reserve Program: The specification name of the program and its parameters, is decorative and not affect the overall program itself Uses: define the name of the unit to be referenced by the program Var: linking an identifier and its type with a location in memory where the values of these types can be stored Begin: starting a program block End: end a program block Array: used to define an array type Record: used to declare the record contains a collection of data from various types of data With: used to make reference to a field in the record Type: used to declare an identifier that has a specific data type Do: used to describe the action to perform For: defines the initial value iteration To: defines the value of the final iteration (iteration positive) If: mention the conditions for a statement that can be executed Then: mention the statement of action must be executed when the conditions are met Else: mention the statement of actions that can be executed while another action statement is not executed because the condition is not fulfilled.
3. The procedure used Write: writing a variable into a component file in a text file used to write one or more values to a file Writeln: executing the write procedure, then pulled out a marker of end-of-line (EOL) to the program files Read: read a file into a variable component, in a text file used to read one or more values into one or more variables Readln: executes a read procedure, and then move to the next line in program files
4. Functions used Readkey: read a character entered through keyboard 



a.       Contoh pertama
Program m6c1;
uses wincrt;
type
     anggota = record
         nama   : string[20];
         alamat : string[30];
         jenkel, status : char;
         umur   : byte;
     end;
var
     peserta : anggota;
     tampil  : char;
     stat    : string[15];
     kel     : string[9];
begin
     writeln;
     writeln ('DATA PESERTA');
     writeln;
     write ('Nama : ');
     readln (peserta.nama);
     write ('Alamat : ');
     readln (peserta.alamat);
     write ('Jenis kelamin (L/P) : ');
     readln (peserta.jenkel);
     If (peserta.jenkel = 'L') or (peserta.jenkel = 'l') then
          kel := 'Laki-laki'
     else
          kel := 'Perempuan';
     write ('Umur : ');
     readln (peserta.umur);
     write ('Menikah (Y/T) : ');
     readln (peserta.status);
     If (peserta.status = 'Y') or (peserta.status = 'y') then
          stat := 'Menikah'
     else
          stat := 'Tidak menikah';
     writeln;
     write ('Tampilkan data (Y/T)? ');
     tampil := readkey;
     writeln;
     If (tampil = 'Y') or (tampil = 'y') then
     begin
          writeln;
          writeln('Nama          : ',peserta.nama);
          writeln('Alamat        : ',peserta.alamat);
          writeln('Jenis Kelamin : ',kel);
          writeln('Umur          : ',peserta.umur,' tahun');
          writeln('Status        : ',stat);
     end;
end.

b.      Contoh ke-dua
Program m6c2;
uses wincrt;
type
     databrg = record
             namabrg : string[15];
             unitbrg : byte;
     end;
var
     jumlahbrg, i : integer;
     total : word;
     barang : array [1..20] of databrg;
begin
     writeln;
     write ('Jumlah barang? ');
     readln (jumlahbrg);
     for i := 1 to jumlahbrg do
     begin
          writeln;
          writeln ('Barang ke ',i);
          with barang[i] do
          begin
               write ('Nama barang : ');
               readln (namabrg);
               write ('Unit : ');
               readln (unitbrg);
          end;
     end;
     writeln;
     writeln ('=========================');
     writeln ('     INVENTARIS DATA');
     writeln ('=========================');
     total := 0;
     for i := 1 to jumlahbrg do
     begin
          with barang[i] do
          begin
               total := total + unitbrg;
               writeln (namabrg:15, unitbrg:5);
          end;
     end;
     writeln;
     writeln ('******** Total: ',total:4);
end.