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); 
w,b); 
w= „,w); 
w= 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

2Bytesprog2 course_2014_c8_units

  • 1.
    Prog_2 course- 2014 2 bytes team Kinan keshkeh IT Engineering-Damascus University 3rd year
  • 2.
  • 3.
  • 4.
    Introduction How isthe 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 )
  • 5.
    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
  • 6.
    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 :
  • 7.
    Interface : orwhat 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
  • 8.
    Implementation : orwhat 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;
  • 9.
    After writing unitwhat 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 ……
  • 10.
    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 !!!
  • 11.
    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
  • 12.
    Program prog1; Usesmy_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); w,b); w= „,w); w= 5
  • 13.
    Program prog1; Usesmy_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
  • 14.
    Lets make itreal :D !!
  • 15.
    Exercise : أنشئوحذة بشهج تٍ وعشف ف هٍا تابع قٌىم بحساب ) a^n ( وإجشائ تٍ تقىم بحساب هعكىس عذد طب عٍ وإجشائ تٍ تقىم بطباعت عناصش سلسلت أحاد تٌ بشكل هعكىس )هن النها تٌ حتى البذا تٌ ( ) على أن تتن كتابت الإجشائ اٍث السابقت بشكل عىدي ( ثن استخذم هزه الىحذة ف بشناهج سئ سٍ ورلك لاستذعاء التىابع السابقت
  • 16.
    Recursion It‟s processthat procedure or function recall itself within the body of that procedure or function  Recursion always need to stop condition
  • 17.
    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;
  • 18.
    Procedure reverse(z:integer ;varz1: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.
  • 19.
    Program pro2bytes_team; usesrecursion_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);
  • 20.
    Writeln(„input the numberof 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.
  • 21.
    Execution : Functiony(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.
  • 22.
    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
  • 23.
    Group : grouplink Mobile phone- Kinan : 0994385748 Facebook account : kinan’s account 2 bytes team