Added upstream from http://ftp.icm.edu.pl/pub/loglan/
[loglan.git] / HTML / MicroMan / Procedur.htm
1 <!doctype html public "-//IETF//DTD HTML//EN">\r
2 <HTML>\r
3 \r
4 <HEAD>\r
5 \r
6 <TITLE>Procedures and functions</TITLE>\r
7 \r
8 <META NAME="GENERATOR" CONTENT="Internet Assistant for Word 1.0Z">\r
9 <META NAME="AUTHOR" CONTENT="NOM">\r
10 </HEAD>\r
11 \r
12 <BODY>\r
13 \r
14 <P>\r
15 <U><I>Loglan 82, A micro-manual of the programming language -\r
16 Basic constructs and facilities</I></U>\r
17 <H1><IMG ALIGN=MIDDLE SRC="gifs/logo2.gif"> 4) Procedures and\r
18 functions</H1>\r
19 <HR>\r
20 \r
21 <P>\r
22 Procedures and functions are well-known kinds of units. Their\r
23 syntax is modelled on Pascal's, though with some slight modifications.\r
24 Procedure (function) declaration consists of a specification part\r
25 and a body.\r
26 <P>\r
27 Example:\r
28 <P>\r
29 <IMG SRC="gifs/pf01.gif"> \r
30 <P>\r
31 Procedure or function specification begins with its identifier\r
32 preceded by the keyword unit. (The same syntax concerns any other\r
33 module named unit.) Then follows its kind declaration, its formal\r
34 parameters (if any), and the type of the returned value (only\r
35 for functions). A body consists of declaration lists for local\r
36 entities and a sequence of statements. The keyword <B>begin</B>\r
37 commences the sequence of statements, and is omitted, if this\r
38 sequence is empty. The value returned by a function equals to\r
39 the most recent value of the standard variable &quot;result&quot;,\r
40 implicitly declared in any function. This variable can be used\r
41 as a local auxiliary variable as well.\r
42 <P>\r
43 Example:\r
44 <P>\r
45 <IMG SRC="gifs/pf02.gif"> \r
46 <P>\r
47 The optional identifier at the end of a unit must repeat the identifier\r
48 of a unit. It is suggested that the compilers check the order\r
49 of unit nesting, so these optional occurrences of identifiers\r
50 would facilitate program debugging.\r
51 <P>\r
52 All the local variables of a unit are initialized (real with 0.0,\r
53 integer with 0, boolean with false etc.). Thus , for instance,\r
54 the value of function Newton is 0 for m&amp;gtn, since &quot;result&quot;\r
55 is also initialized, as any other local variable.\r
56 <P>\r
57 The return statement (return) completes the execution of a procedure\r
58 (function) body,i.e. return is made to the caller. If return does\r
59 not appear explicitly, return is made with the execution of the\r
60 final end of a unit. Upon return to the caller the procedure (function)\r
61 object is deallocated.\r
62 <P>\r
63 Functions are invoked in expressions with the corresponding list\r
64 of actual parameters. Procedures are invoked by call statement\r
65 (also with the corresponding list of actual parameters).\r
66 <P>\r
67 Example:\r
68 <P>\r
69 <IMG SRC="gifs/pf03.gif"> \r
70 <P>\r
71 Formal parameters are of four categories: variable parameters,\r
72 procedure parameters, function parameters and type parameters\r
73 (cf p.8). Variable parameters are considered local variables to\r
74 the unit. A variable parameter has one of three transmission modes:\r
75 input, output or inout. If no mode is explicitly given the input\r
76 mode is assumed. For instance in the unit declaration:\r
77 <P>\r
78 <IMG SRC="gifs/pf04.gif"> \r
79 <P>\r
80 x,y,b are input parameters , c,i are output parameters , and j\r
81 is inout parameter.\r
82 <P>\r
83 Input parameter acts as a local variable whose value is initialized\r
84 by the value of the corresponding actual parameter. Output parameter\r
85 acts as a local variable initialized in the standard manner (real\r
86 with 0.0, integer with 0, boolean with false etc.). Upon return\r
87 its value is assigned to the corresponding actual parameter, in\r
88 which case it must be a variable. However the address of such\r
89 an actual parameter is determined upon entry to the body. Inout\r
90 parameter acts as an input parameter and output parameter together.\r
91 <P>\r
92 Example:\r
93 <P>\r
94 <IMG SRC="gifs/pf05.gif"> \r
95 <P>\r
96 A procedure call to the above unit may be the following:\r
97 <P>\r
98 <IMG SRC="gifs/pf06.gif"> \r
99 <P>\r
100 where g,h,gi,hi are real variables.\r
101 <P>\r
102 No restriction is imposed on the order of declarations. In particular,\r
103 recursive procedures and functions can be declared without additional\r
104 announcements (in contrast to Pascal).\r
105 <P>\r
106 Example:\r
107 <P>\r
108 For two recursive sequences defined as:\r
109 <P>\r
110 a(n)=b(n-1)+n+2 n&gt;0\r
111 <P>\r
112 b(n)=a(n-1)+(n-1)*n n&gt;0\r
113 <P>\r
114 a(0)=b(0)=0\r
115 <P>\r
116 one can declare two functions:\r
117 <P>\r
118 <IMG SRC="gifs/pf07.gif"> \r
119 <P>\r
120 and invoke them:\r
121 <P>\r
122 k:=a(100)*b(50)+a(15);\r
123 <P>\r
124 Functions and procedures can be formal parameters as well.\r
125 <P>\r
126 Example:\r
127 <P>\r
128 <IMG SRC="gifs/pf08.gif"> \r
129 <P>\r
130 In the above declaration, after the input variable parameters\r
131 a,b,eps and the output variable parameter x, a function parameter\r
132 f appears. Note that its specification part is complete. Thus\r
133 the check of actual-formal parameter compatibility is possible\r
134 at compilation time. Making use of this syntactic facility is\r
135 not possible in general, if a formal procedure (function) is again\r
136 a formal parameter of a formal procedure (function). The second\r
137 degree of formal procedures (functions) nesting is rather scarce,\r
138 but LOGLAN-82 admits such a construct. Then formal procedure (function)\r
139 has no specification part and the full check of actual-formal\r
140 parameter compatibility is left to be done at run time.\r
141 <P>\r
142 Example:\r
143 <P>\r
144 <IMG SRC="gifs/pf09.gif"> \r
145 <P>\r
146 Procedure G is a first degree parameter, therefore it occurs with\r
147 complete specification part. Procedure H is a second degree parameter\r
148 and has no specification part. In this case a procedure call can\r
149 be strongly recursive:\r
150 <P>\r
151 <IMG SRC="gifs/pf10.gif"> <HR>\r
152 \r
153 <hr><P>\r
154 <A HREF="Modulari.htm"><IMG SRC="gifs/PrevPage.gif"></A> \r
155 <A HREF="HomePage.htm"><IMG SRC="gifs/HomePage.gif"></A> \r
156 <A HREF="Classes.htm"><IMG SRC="gifs/NextPage.gif"></A>\r
157 <HR>\r
158 \r
159 <ADDRESS>\r
160 Last update 02/07/95 \r
161 </ADDRESS>\r
162 \r
163 <ADDRESS>\r
164 Comments, suggestions and critiques are welcome to : <A HREF="mailto:linfo062@crisv2.univ-pau.fr">linfo062@crisv2.univ-pau.fr</A>\r
165 \r
166 </ADDRESS>\r
167 \r
168 </BODY>\r
169 \r
170 </HTML>\r