Monday, June 6, 2011

procedure is a block of program code designed to solve common problems

procedure is a block of program code designed to solve common problems. Suppose the pascal programming language known as Writeln procedures, clrscr, textbackground, textcolor, readln, etc.. The invitation to these procedures vary from one procedure to another procedure. Suppose that function clrscr procedure to remove all the writing on the screen and position the cursor at X = 0 and Y = 0. Clrscr in the calling procedure does not require parameters; procedure Writeln / write function to print the output (output) to the screen. Procedure Writeln / write can be called with or without include parameters include parameters. For example, instructed the translator (compiler) Pascal to move the cursor down one line (Score Increase Y) and X remained in position 0 as shown in the following code:

begin
Writeln;
end.

In the above code was called Writeln procedures not followed by parameters. Pascal understand that if the Writeln procedure call without parameters then position the cursor on the monitor changes the cursor back to position X = 0 (Carriage Return) and the Y position plus One - Down The Line (Line Feed).

Another example we could send Writeln procedure to print a multiplication 10 * 10:

begin
Writeln (10 * 10);
end.

The code above can be explained that we call the Writeln procedure by sending a parameter multiplying 10 * 10. Because the Writeln procedure is a procedure that has been ready to use (built-in procedure) or have been defined by pascal eat the detail of the instructions in the Writeln procedure we do not know for sure.

We can also tell Writeln command to print a string of characters (string) "Respati Yogyakarta":

begin
Writeln ('Respati Yogyakarta');
end.

Or a combination of value and konstanta as follows:
begin
Writeln (10 * 10, 'Respati Yogyakarta');
end.

Flexibility Writeln procedure would be very helpful in giving commands to the computer.

User Defined Procedures
Pascal allows programmers to create their own procedures (user defined procedure). The procedure (syntax) the declaration made by the user procedure is:

namaprosedur procedure ([par1, par2, par3, ... parn]);
{Variables declarations}
{Declarations} procedure
{Functions declarations}
{Constants declarations}
{Declaration} label

begin

end;

Dikelarasikan procedures can then be called from the main program or from the procedure itself or other procedures.

The following will be a procedure to print the address Unriyo Yogyakarta:

CetakAlamat Program;
uses crt;
procedure Address ();
begin
Writeln ('University Respati Yogyakarta');
Writeln ('Jln.Laksda Adisucipto Km.6, 3 Depok, Sleman, Yogyakarta');
end;

Begin
clrscr;
Address} {Calling Procedure
Address ();
End.

Procedure Berparameter
Procedures are given specific tasks to take parameters as needed. Suppose there are desirable procedure to print numbers 1 to N. Here the N value is uncertain, can be 10, 20, 100. For this procedure call when we send the value of N and further process the printing procedure numbers 1 to N.


Print-Program;
Uses Crt;

Procedure CetakAngka1Sd (N: Byte);
Var
I: Byte;} {Declaration of Local
Begin
For I: = 1 To N Do
Writeln (I);
End;

{Main Program}
Begin
{Clear the screen}
clrscr;
Print a Procedure Call {20} s.d
CetakAngka1SD (20);
{Procedure Call Again Print Number 1 s.d 100}
CetakAngka1SD (100);
Readln; {Wait Pressing Enter key}
End.


Parameter Value and Reference Parameters
In general, the parameters passed to the procedure are the parameter value means the parameter value will not change after the procedure call. When the desired parameter value changes after calling the procedure, the parameter is called a reference parameter.

Create a procedure to calculate the number of runs the following:
1 / 1 +1 / 2 +1 / 3 + ... 1 / N
Desired number N is uncertain and at once the sum looks after the procedure call. Thus it takes 2 (two) parameters: N (parameter value) and amount (reference parameter).


Jumlah_Deret Program;
Uses Crt;

Procedure Number (N: Byte; Var Amount: Real);
Var
I: Byte;
Begin
Total: = 0;
For I: = 1 To N Do
Total: = Total + 1 / I
End;

Var
Cacah_Data: Byte;
Results: Real;
Begin
clrscr;
Cacah_Data: = 10;
{Procedure Call}
Total (Cacah_Data, Result);
Writeln ('Number of Series', Result: 8:2);
Readln;
End.

Variable Scope
The scope variables (variables scope) describes the accessibility of these variables. In Pascal there are 2 (two) types of scope that is global and local scope. Global scope indicates that the variable will be recognized throughout the program code. Local scope indicates that the variable is only known place declared.

Suppose there are programs such as the following:

Var
IGlobal: Byte;

Procedure LokalSatu;
Var
ILokal1: Byte;
Begin
ILokal1: = 10;
Writeln (ILokal1, IGlobal);
End;

Procedure LokalDua;
Var
ILokal2: Byte;
Begin
ILokal2: = 20;
Writeln (ILokal2, IGlobal);
End;

{Main Program}
Begin
IGlobal: = 10;
Local;
End.

In the above code IGlobal variable will be recognized throughout the source code (both in the Main Program, LokalSatu Procedure, Procedure LokalDua). While the variable will only be known in ILokal1 LokalSatu procedure. Likewise ILokal2 variables will only be known in LokalDua procedure.

We note again the following program code:

Lingkup_Lingkup Program;
Var
I: Byte;
Outlying Procedure;
Var
ITerluar: Byte;
Deepest Procedure;
Var
ITerdalam: Byte;
Begin
ITerdalam: = 20;
Writeln (I, ITerluar, ITerdalam);
End;
Begin
ITerluar: = 10;
Outlying} {Call Procedure
End;

{Main Program}
Begin
I: = 5;
Outlying;
End.

In the code above first global nature means it will be recognized in the main program, procedures for the outer and inner procedure. ITerluar variables will be global in that procedure so that the variable will be recognized procedures declared in the procedure. However ITerluar variables will not be recognized by the Main Program. ITerdalam variables will only be recognized by Deepest Procedure.

Recursive Procedure
The procedure that calls itself is called as recursive procedures. Suppose there is code like the following:

Procedure Print;
Begin
Writeln ('Respati');
{Procedure Call} Himself
Print;
End;

{Main Program}
Begin
{Procedure Call}
Print;
End.

The above source code can be explained as follows:
1. Start
2. Procedure Call Print
3. Print string 'Respati'
4. Procedure Call Print
5. Print string 'Respati'
6. Procedure Call Print
7. Print string 'Respati'
8. ff

Summons against itself will still be conducted Print procedure because there is no statement to stop calling. This calling will continue ongoing (continues call).
To overcome this problem it is necessary to make a statement that limits the procedure call. Suppose the above program code we change something like this:


Var
I: Byte;
Procedure Print;
Begin
IF (I <= 5) Then begin Writeln ('Respati'); {Procedure Call} Himself Print; I: = I +1; end; End; {Main Program} Begin I: = 1; {Procedure Call} Print; End. In the code above procedure will only be invoked Print 5x as much as stated in condition IF (I <= 5). If I> 5 then print the procedure will not be called again. Generally determined a condition that lay on the procedure call itself.

Parameter array
Array parameters can be passed to the procedure with little difference from the parameters of conventional type (byte, word, integer, real).

Jumlah_Data Program;
Const
N = 100;
Type
Data: Array [1 .. N] Of Single;
Var
Adata: Data;

Procedure Number (DataA: Data, Count: Byte) '
Begin
For I: = 1 To N Do
Jlh: = Jlh + DataA [I];
Writeln ('Number of Data Is', Jlh: 12:0);
End;

{Main Program}
Begin
{Enter data}
ADATA [1]: = 70;
ADATA [1]: = 65.5;
ADATA [1]: = 89;
ADATA [1]: = 77;
ADATA [1]: = 64;
ADATA [1]: = 78.5;
{Call Procedure Number of Data}
Total (Adata, 6);
Readln;
End.

Exercise Procedure
Known weight data Prodi student information system as below:
65,78,58,60,63,56,65,69,77
Make procedures for:
1.menginputkan N Fruit data
2.menghitung number, average weight and standard deviation
3.mengurutkan data in ascending (ascending)
4.mengurutkan data in descending order (descending)
5.melihat contents array

To resolve these cases will be made in the form pilihahan menu making it easier in the use of the program.
Menu Options
1. Input Data
2. Determine Total, Average and Standard Deviation
3. Sort Data In Ascending
4. Sort Data By Descending
5. Display Array Contents
Your choice [1 .. 5]:


Statistics Program;
Uses Crt;
Const
N = 100;

{Weight} Input Procedure Declaration
Procedure Input_Berat (Berat_Mhs: Weight: N: Byte);
Var
I: Byte;
Begin
For I: = 1 To N Do
Begin
write ('Enter The Weight', I ,':'); readln (Berat_Mhs [I]);
End;
End;

{Main Program}
Var
Repeat: Boolean;
Pills: ShortInt;
Begin
Repeat: = True;
While (Repeat) Do
Begin
clrscr;
gotoxy (25.1), write ('U U M E N T A M A');
gotoxy (25.2); write ('---------');
gotoxy (25.3), write ('1. Input Data ');
gotoxy (25.4), write ('2. Determine the Number of, Average, Standard Deviation ');
gotoxy (25.5), write ('3. Heavy Sort Descending ');
gotoxy (25.6), write ('4. Heavy Sort Ascending ');
gotoxy (25.7), write ('5. Show Array ");
gotoxy (25.8), write ('0. Done ');
pill: =- 1;
while (pills <0) or (pill> 5) do
begin
gotoxy (25.9), write ('Your Choice [0 .. 5]:'); readln (pills);
end;
Test {More}
Case (Pil) Of
1: Begin Weight} {Input Data

End;
2: Begin {Set Total, Average and Standard Deviation}

End;
3: Begin {} Ascending Sort By

End;
4: Begin {} Sort By Descending

End;
5: Begin Array} {Shown Contents

End;
0: Begin {End Program}

End;
End;} {End of Case
End; {End While}
End. {End Program}

No comments:

Post a Comment

 
THANK YOU FOR VISITING