Prog_2 course- 2014
2 bytes team
Kinan keshkeh
IT Engineering-Damascus University
3rd year
Units
Why units ?
Introduction
How is the compilation process ??
the principle of compilation is based on fragmentation
the program into several files then the compiler compile
each file separately then Connect all parts of the
compiled to configure executable file (conversion
program form machine language to “code” language )
Unit stop here
compilation
source file in
Pascal language
Text files
for some
procedure
or function
Unit
programing
ready to use
Libraries
ready to use
linked
Execution
unit Unit unitname
Interface
Implementation
Definition :
Uses
Const
Type
Var
The header of procedure or function
Uses
Const
Type
Var
The object of procedure or function
founded in interface part
End. After last end we have to put (.) not (;)
Unit divided into two
sections :
1) Interface :
2) Implementation :
Interface : or what we call it <<public part>> , in this part we can
define the visible things to any program or another unit for
example :
Uses wincrt;
Const pi=3.14;
Type arr=array[1..100] of integer;
var x:integer;
Function sum(x,y:integer):integer;
Function max(a,b:integer):integer;
Note: include interface part we only define the header of
procedure or function
Implementation : or what we call it <<private part>> in this part we can define invisible things, we can‟t deal with them when we go out from the definition of the unit
Uses wincrt;
Const max=100;
Type person=record
fname,lname:string;
end;
var c:char ;
Function sum(x,y:integer): integer;
Begin
Sum:=x+y;
end;
Function max(a,b:integer):integer;
Begin
if (a > b) then
Max:=a;
Else
Max:=b;
end;
After writing unit what do we have to do ?
1. Correct the errors, if any
2. Compilation
3. Save this file under unit‟s name with (TPU) <<unitname.TPU>>
For example :
Unit my_unit;
Save as:<<my_unit.TPU>>
4. Recall this unit in main program using
reserved word (uses)
uses my_unit ;
Note : unit is subprogram unexecutable
we cannot run it ……
Unit my_unit1;
Interface
Const val=200;
Var w : integer ;
procedure swap(var x,y:integer);
Implementation
var temp :integer ;
procedure swap(var x,y:integer);
begin
temp:=x;
x:=y;
y:=temp;
end;
End.
Compilation
Attention !!!
Unit my_unit1;
Interface
Const val=200;
Var w : integer ;
procedure swap(var x,y:integer);
Implementation
var temp :integer ;
readln(x);
procedure swap(var x,y:integer);
begin
temp:=x;
x:=y;
y:=temp;
end;
End.
{false}
There is no
Instructions in unit
Program prog1;
Uses my_unit1;
Var a,b :integer;
Begin
w:=5; temp:=10; a:=2; b:=3;
End.
{true} {false}
a= 3
b= 2
swap(a,b);
Writeln(„a= „,a);
Writeln(„b= „,b);
swap(w,b);
Writeln(„w= „,w);
Writeln(„b= „,b);
w= 3
b= 5
Program prog1;
Uses my_unit1;
Var a,b,w :integer;
Begin
w:=5; temp:=10; a:=2; b:=3;
End.
swap(w,b);
Writeln(„w= „,w);
Writeln(„b= „,b);
My_unit1.w If you want to deal
with w in my_unit
You should write Which w ????
{this}
my_unit1.w:=10
Lets make it
real :D !!
Exercise : وإجشائٍت ( a^n )أنشئ وحذة بشهجٍت وعشف فٍها تابع ٌقىم بحساب
تقىم بحساب هعكىس عذد طبٍعً وإجشائٍت تقىم بطباعت عناصش سلسلت على أن تتن كتابت ( ) هن النهاٌت حتى البذاٌت )أحادٌت بشكل هعكىس
ثن استخذم هزه الىحذة فً بشناهج ( اإلجشائٍاث السابقت بشكل عىدي سئٍسً ورلك الستذعاء التىابع السابقت
Recursion
It‟s process that procedure or function recall
itself within the body of that procedure or
function
Recursion always need to stop condition
Unit recursion_unit;
Interface
type list=^node;
node=record
value:integer;
next : list;
end;
Function y(a,n:integer): longint ;
Procedure reverse(z:integer ; var z1:integer);
Procedure printlist (p: list );
Implementation
Function y(a,n:integer): longint ;
Begin
If n=0 then
y:=1
else
y:=y(a,n-1)*a;
end;
Procedure reverse(z:integer ;var z1:integer);
Begin
If (z<>0) then
begin
Z1:=z1*10 + z mod 10;
Reverse (z div 10 , z1);
end;
end;
Procedure printlist(p : list );
Begin
If (p^.next <> nil ) then
Printlist(p^.next);
Write(p^.value:5);
end;
End.
Program pro2bytes_team;
uses recursion_unit;
var a,n,z,z1,m,x,i:integer;
p,temp:list;
Begin
Readln(a,n);
If (n < 0) then
Writeln(„a^n = „,1/y(a,n))
Else
Writeln(„a^n = „,y(a,n));
Writeln(„ input positive number „);
Readln(z);
z1:=0;
Reverse (z,z1);
Writeln(„z1 = „,z1);
Writeln(„input the number of nodes „);
Readln(m);
Writeln(„input the value „);
Readln(x);
new(p);
p^.value:=x;
temp:=p;
for i:=2 to m do
begin
new(temp^.next) ;
temp:=temp^.next;
Writeln(„input the value „);
Readln(x);
temp^.value:=x;
end;
temp^.next:=nil;
Printlist(p);
End.
Execution : Function y(a,n:integer): longint ;
Begin
If n=0 then
y:=1
else
y:=y(a,n-1)*a;
end;
n=3 a=2
a = n
y= * 2
n= 3
n= 2
n= 1
n= 0
y= * 2
y= * 2
y = 1
waiting waiting waiting stop condition
8
4 2 1
End.
Procedure printlist(p : list );
Begin
If (p^.next <> nil ) then
Printlist(p^.next);
Write(p^.value:5);
end;
End.
Execution :
next 20
15
10
5
p
nil
p
p
p
stop condition !!!
output
20
15
10
5
Group : group link
Mobile phone- Kinan : 0994385748
Facebook account : kinan’s account
2 bytes team