Author: Raghunath Dhungel
How to declare Local routines in delphi ?
Answer:
It is possible to declare local procedures or Functions within a procedure or
function. Though it seems some how unusual at the first glance to declare Local
routines within a routine, it is efficient to do this. If we do not need them
anywhere other than the routine, why should make them public even within the unit ?
Let us place them in their proper place, proper routines in proper place !
We declare local variables, constants and types before the BEGIN statement of a
function or procedure. We can also include local routines here. Though it is
efficient to include local routines here, but, in practice, very few delphi
programmers use these techniques.
Example:
1
2 procedure PublicProc(p1: TypeofP1; p2: TypeofP2; ....pn: TypeOfPn);
3 var
4 v1: TypeofV1;
5 V2: TypeofV2;
6 .....
7 Vn: TypeofVn;
8 const
9 c1: TypeofC1;
10 { ............ }
11 { ............ }
12
13 procedure LocalProcedure1(p1: TypeofP1; p2: TypeofP2; ....pn: TypeOfPn);
14 var
15 { ............ }
16 { ............ }
17 begin
18 { ............ }
19 { ............ }
20 end;
21 procedure LocalProcedure2(p1: TypeofP1; p2: TypeofP2; ....pn: TypeOfPn);
22 var
23 { ............ }
24 { ............ }
25 begin
26 { ............ }
27 { ............ }
28 end;
29 function LocalFunction1(p1: TypeofP1; p2: TypeofP2; ....pn: TypeOfPn): ResultType;
30 var
31 { ............ }
32 { ............ }
33 begin
34 { ............ }
35 { ............ }
36 result := {..... }
37 end;
38 function LocalFunction2(p1: TypeofP1; p2: TypeofP2; ....pn: TypeOfPn): ResultType;
39 var
40 { ............ }
41 { ............ }
42 begin
43 { ............ }
44 { ............ }.
45 result := {..... }
46 end;
47
48 begin {PublicProc}
49 ...........
50 '''''''''''
51 LocalProcedure1(...., ...., ....);
52 ...........
53 LocalProcedure2(...., ...., ....);
54 ...........
55 v1 := LocalFunction1(...., ...., ....);
56 ...........
57 v2 := LocalFunction2(...., ...., ....);
58 ...........
59 ''''''
60 end; {PublicProc}
In this example, the scope of the nested routines
LocalProcedure1,
LocalProcedure2,
LocalFunction1 and
LocalFunction2
is limited only to PublicProc. No other routines in the same unit or in other
units can see them.
|