Basics-FunctionAttributes.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
  4. <sect1 id="Function_Attributes__Parameter_Passing">
  5. <title>Function Definitions<indexterm>
  6. <primary>Functions</primary>
  7. </indexterm> (Parameter Passing<indexterm>
  8. <primary><emphasis role="bold">Parameter Passing</emphasis></primary>
  9. </indexterm>)</title>
  10. <para>All of the basic Definition types can also become functions by
  11. defining them to accept passed parameters (arguments<indexterm>
  12. <primary>arguments</primary>
  13. </indexterm>). The fact that it receives parameters doesn't change the
  14. essential nature of the Definition's type, it simply makes it more
  15. flexible.</para>
  16. <para>Parameter definitions always appear in parentheses attached to the
  17. Definition's name. You may define the function to receive as many parameters
  18. as needed to create the desired functionality by simply separating each
  19. succeeding parameter definition with a comma.</para>
  20. <para>The format of parameter definitions is as follows:</para>
  21. <para>DefinitionName<emphasis role="bold">( [
  22. </emphasis><emphasis>ValueType</emphasis><emphasis role="bold"> ]
  23. </emphasis><emphasis>AliasName</emphasis><emphasis role="bold"> [
  24. =</emphasis><emphasis>DefaultValue</emphasis><emphasis role="bold"> ] ) :=
  25. </emphasis>expression<emphasis role="bold">;</emphasis></para>
  26. <informaltable colsep="0" frame="none" rowsep="0">
  27. <tgroup cols="2">
  28. <colspec align="left" colwidth="122.40pt" />
  29. <colspec />
  30. <tbody>
  31. <row>
  32. <entry><emphasis>ValueType</emphasis></entry>
  33. <entry>Optional. Specifies the type of data being passed. If
  34. omitted, the default is INTEGER (see <emphasis role="bold">Value
  35. Types<indexterm>
  36. <primary><emphasis role="bold">Value Types</emphasis></primary>
  37. </indexterm></emphasis>). This also may include the CONST keyword
  38. (see <emphasis role="bold">CONST<indexterm>
  39. <primary>CONST</primary>
  40. </indexterm></emphasis>) to indicate that the passed value will
  41. always be treated as a constant.</entry>
  42. </row>
  43. <row>
  44. <entry><emphasis>AliasName</emphasis></entry>
  45. <entry>Names the parameter for use in the expression.</entry>
  46. </row>
  47. <row>
  48. <entry><emphasis>DefaultValue</emphasis></entry>
  49. <entry>Optional. Provides the value to use in the expression if the
  50. parameter is omitted. The <emphasis>DefaultValue</emphasis> may be
  51. the keyword ALL if the ValueType is SET (see the <emphasis
  52. role="bold">SET<indexterm>
  53. <primary>SET</primary>
  54. </indexterm></emphasis> keyword) to indicate all possible values
  55. for that type of set, or empty square brackets<indexterm>
  56. <primary>square brackets</primary>
  57. </indexterm> ([ ]) to indicate no possible value for that type of
  58. set.</entry>
  59. </row>
  60. <row>
  61. <entry><emphasis>expression</emphasis></entry>
  62. <entry>The function's operation for which the parameters are
  63. used.</entry>
  64. </row>
  65. </tbody>
  66. </tgroup>
  67. </informaltable>
  68. <sect2 id="Simple_Value_Type_Parameters">
  69. <title>Simple Value Type Parameters</title>
  70. <para>If the optional <emphasis>ValueType</emphasis> is any of the simple
  71. types (BOOLEAN, INTEGER, REAL, DECIMAL, STRING, QSTRING, UNICODE, DATA,
  72. VARSTRING, VARUNICODE), the <emphasis>ValueType</emphasis> may include the
  73. CONST keyword (see <emphasis role="bold">CONST</emphasis>) to indicate
  74. that the passed value will always be treated as a constant (typically used
  75. only in ECL prototypes of external functions).</para>
  76. <programlisting>ValueAttribute := 15;
  77. FirstFunction(INTEGER x=5) := x + 5;
  78. //takes an integer parameter named "x" and "x" is used in the
  79. //arithmetic expression to indicate the usage of the parameter
  80. SecondAttribute := FirstFunction(ValueAttribute);
  81. // The value of SecondAttribute is 20
  82. ThirdAttribute := FirstFunction();
  83. // The value of ThirdAttribute is 10, omitting the parameter</programlisting>
  84. </sect2>
  85. <sect2 id="SET_Parameters">
  86. <title>SET Parameters</title>
  87. <para>The <emphasis>DefaultValue</emphasis> for SET parameters<indexterm>
  88. <primary>SET parameters</primary>
  89. </indexterm> may be a default set of values, the keyword ALL to indicate
  90. all possible values for that type of set, or empty square brackets ([ ])
  91. to indicate no possible value for that type of set (and empty set).</para>
  92. <programlisting>SET OF INTEGER1 SetValues := [5,10,15,20];
  93. IsInSetFunction(SET OF INTEGER1 x=SetValues,y) := y IN x;
  94. OUTPUT(IsInSetFunction([1,2,3,4],5)); //false
  95. OUTPUT(IsInSetFunction(,5)); // true</programlisting>
  96. </sect2>
  97. <sect2 id="Passing_DATASET_Parameters">
  98. <title>Passing DATASET Parameters</title>
  99. <para>Passing a DATASET or a derived recordset as a parameter may be
  100. accomplished using the following syntax:</para>
  101. <para><emphasis>DefinitionName</emphasis><emphasis role="bold">(
  102. DATASET<indexterm>
  103. <primary>DATASET parameter</primary>
  104. </indexterm>(</emphasis><emphasis> recstruct </emphasis><emphasis
  105. role="bold">) </emphasis><emphasis>AliasName</emphasis><emphasis
  106. role="bold"> ) := </emphasis><emphasis>expression</emphasis><emphasis
  107. role="bold">;</emphasis></para>
  108. <para>The required<emphasis> recstruct</emphasis> names the RECORD
  109. structure attribute that defines the layout of fields in the passed
  110. DATASET parameter. The <emphasis>recstruct</emphasis> may alternatively
  111. use the RECORDOF function. The required<emphasis> AliasName</emphasis>
  112. names the dataset for use in the function and is used in the Definition's
  113. <emphasis>expression</emphasis> to indicate where in the operation the
  114. passed parameter is to be used. See the <emphasis role="bold">DATASET as a
  115. Value Type</emphasis> discussion in the DATASET documentation for further
  116. examples.</para>
  117. <programlisting> MyRec := {STRING1 Letter};
  118. SomeFile := DATASET([{'A'},{'B'},{'C'},{'D'},{'E'}],MyRec);
  119. FilteredDS(DATASET(MyRec) ds) := ds(Letter NOT IN ['A','C','E']);
  120. //passed dataset referenced as “ds” in expression
  121. OUTPUT(FilteredDS(SomeFile));</programlisting>
  122. </sect2>
  123. <sect2 id="Passing_Typeless_Parameters">
  124. <title>Passing Typeless Parameters</title>
  125. <para>Passing parameters of any type may be accomplished using the keyword
  126. ANY<indexterm>
  127. <primary>ANY</primary>
  128. </indexterm> as the passed value type:</para>
  129. <para><emphasis>DefinitionName</emphasis> <emphasis role="bold">(
  130. ANY</emphasis> <emphasis>AliasName</emphasis> <emphasis role="bold"> ) :=
  131. </emphasis><emphasis>expression</emphasis><emphasis
  132. role="bold">;</emphasis></para>
  133. <programlisting>a := 10;
  134. b := 20;
  135. c := '1';
  136. d := '2';
  137. e := '3';
  138. f := '4';
  139. s1 := [c,d];
  140. s2 := [e,f];
  141. ds1 := DATASET(s1,{STRING1 ltr});
  142. ds2 := DATASET(s2,{STRING1 ltr});
  143. MyFunc(ANY l, ANY r) := l + r;
  144. MyFunc(a,b); //returns 30
  145. MyFunc(a,c); //returns '101'
  146. MyFunc(c,d); //returns '12'
  147. MyFunc(s1,s2); //returns a set: ['1','2','3','4']
  148. MyFunc(ds1,ds2); //returns 4 records: '1', '2', '3', and '4'
  149. </programlisting>
  150. </sect2>
  151. <sect2 id="Passing_Function_Parameters">
  152. <title>Passing Function Parameters</title>
  153. <para>Passing a Function as a parameter may be accomplished using either
  154. of the following syntax options as the <emphasis>ValueType</emphasis> for
  155. the parameter:</para>
  156. <para><emphasis>FunctionName</emphasis>(<emphasis
  157. role="bold"></emphasis><emphasis>parameters</emphasis><emphasis
  158. role="bold"></emphasis>)<emphasis></emphasis></para>
  159. <para><emphasis>PrototypeName</emphasis></para>
  160. <informaltable colsep="0" frame="none" rowsep="0">
  161. <tgroup cols="2">
  162. <colspec align="left" colwidth="122.40pt" />
  163. <colspec />
  164. <tbody>
  165. <row>
  166. <entry><emphasis>FunctionName</emphasis></entry>
  167. <entry>The name of a function, the type of which may be passed as
  168. a parameter.</entry>
  169. </row>
  170. <row>
  171. <entry><emphasis>parameters</emphasis></entry>
  172. <entry>The parameter definitions for the
  173. <emphasis>FunctionName</emphasis> parameter.</entry>
  174. </row>
  175. <row>
  176. <entry><emphasis>PrototypeName</emphasis></entry>
  177. <entry>The name of a previously defined function to use as the
  178. type of function that may be passed as a parameter.</entry>
  179. </row>
  180. </tbody>
  181. </tgroup>
  182. </informaltable>
  183. <para>The following code provides examples of both methods:</para>
  184. <programlisting>//a Function prototype:
  185. INTEGER actionPrototype(INTEGER v1, INTEGER v2) := 0;
  186. INTEGER aveValues(INTEGER v1, INTEGER v2) := (v1 + v2) DIV 2;
  187. INTEGER addValues(INTEGER v1, INTEGER v2) := v1 + v2;
  188. INTEGER multiValues(INTEGER v1, INTEGER v2) := v1 * v2;
  189. //a Function prototype using a function prototype:
  190. INTEGER applyPrototype(INTEGER v1, actionPrototype actionFunc) := 0;
  191. //using the Function prototype and a default value:
  192. INTEGER applyValue2(INTEGER v1,
  193. actionPrototype actionFunc = aveValues) :=
  194. actionFunc(v1, v1+1)*2;
  195. //Defining the Function parameter inline, witha default value:
  196. INTEGER applyValue4(INTEGER v1,
  197. INTEGER actionFunc(INTEGER v1,INTEGER v2) = aveValues)
  198. := actionFunc(v1, v1+1)*4;
  199. INTEGER doApplyValue(INTEGER v1,
  200. INTEGER actionFunc(INTEGER v1, INTEGER v2))
  201. := applyValue2(v1+1, actionFunc);
  202. //producing simple results:
  203. OUTPUT(applyValue2(1)); // 2
  204. OUTPUT(applyValue2(2)); // 4
  205. OUTPUT(applyValue2(1, addValues)); // 6
  206. OUTPUT(applyValue2(2, addValues)); // 10
  207. OUTPUT(applyValue2(1, multiValues)); // 4
  208. OUTPUT(applyValue2(2, multiValues)); // 12
  209. OUTPUT(doApplyValue(1, multiValues)); // 12
  210. OUTPUT(doApplyValue(2, multiValues)); // 24
  211. //An attribute taking function parameters which themselves
  212. //have parameters that are functions...
  213. STRING doMany(INTEGER v1,
  214. INTEGER firstAction(INTEGER v1,
  215. INTEGER actionFunc(INTEGER v1,INTEGER v2)),
  216. INTEGER secondAction(INTEGER v1,
  217. INTEGER actionFunc(INTEGER v1,INTEGER v2)),
  218. INTEGER actionFunc(INTEGER v1,INTEGER v2))
  219. := (STRING)firstAction(v1, actionFunc) + ':' + (STRING)secondaction(v1, actionFunc);
  220. OUTPUT(doMany(1, applyValue2, applyValue4, addValues));
  221. // produces "6:12"
  222. OUTPUT(doMany(2, applyValue4, applyValue2,multiValues));
  223. // produces "24:12" </programlisting>
  224. </sect2>
  225. <sect2 id="Passing_NAMED_Parameters">
  226. <title>Passing NAMED Parameters</title>
  227. <para>Passing values to a function defined to receive multiple parameters,
  228. many of which have default values (and are therefore omittable), is
  229. usually accomplished by “counting commas” to ensure that the values you
  230. choose to pass are passed to the correct parameter by the parameter's
  231. position in the list. This method becomes untenable when there are many
  232. optional parameters.</para>
  233. <para>The easier method is to use the following NAMED parameter syntax,
  234. which eliminates the need to include extraneous commas as place holders to
  235. put the passed values in the proper parameters:</para>
  236. <para>Attr := FunctionName<emphasis role="bold">( [ NAMED<indexterm>
  237. <primary>NAMED</primary>
  238. </indexterm> ] </emphasis><emphasis>AliasName</emphasis><emphasis
  239. role="bold"> := </emphasis><emphasis>value </emphasis><emphasis
  240. role="bold">);</emphasis></para>
  241. <informaltable colsep="0" frame="none" rowsep="0">
  242. <tgroup cols="2">
  243. <colspec align="left" colwidth="122.40pt" />
  244. <colspec />
  245. <tbody>
  246. <row>
  247. <entry><emphasis>NAMED</emphasis></entry>
  248. <entry>Optional. Required only when the <emphasis>AliasName
  249. </emphasis>clashes with a reserved word.</entry>
  250. </row>
  251. <row>
  252. <entry><emphasis>AliasName</emphasis></entry>
  253. <entry>The names of the parameter in the attribute's function
  254. definition.</entry>
  255. </row>
  256. <row>
  257. <entry><emphasis>value</emphasis></entry>
  258. <entry>The value to pass to the parameter.</entry>
  259. </row>
  260. </tbody>
  261. </tgroup>
  262. </informaltable>
  263. <para>This syntax is used in the call to the function and allows you to
  264. pass values to specific parameters by their
  265. <emphasis>AliasName</emphasis>, without regard for their position in the
  266. list. All unnamed parameters passed must precede any NAMED
  267. parameters.</para>
  268. <programlisting>outputRow(BOOLEAN showA = FALSE, BOOLEAN showB = FALSE,
  269. BOOLEAN showC = FALSE, STRING aValue = 'abc',
  270. INTEGER bValue = 10, BOOLEAN cValue = TRUE) :=
  271. OUTPUT(IF(showA,' a='+aValue,'')+
  272. IF(showB,' b='+(STRING)bValue,'')+
  273. IF(showc,' c='+(STRING)cValue,''));
  274. outputRow(); //produce blanks
  275. outputRow(TRUE); //produce "a=abc"
  276. outputRow(,,TRUE); //produce "c=TRUE"
  277. outputRow(NAMED showB := TRUE); //produce “b=10”
  278. outputRow(TRUE, NAMED aValue := 'Changed value');
  279. //produce “a=Changed value”
  280. outputRow(,,,'Changed value2',NAMED showA := TRUE);
  281. //produce "a=Changed value2"
  282. outputRow(showB := TRUE); //produce “b=10”
  283. outputRow(TRUE, aValue := 'Changed value');
  284. outputRow(,,,'Changed value2',showA := TRUE);
  285. </programlisting>
  286. </sect2>
  287. </sect1>