ref.tex 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451
  1. %
  2. % $Id$
  3. % This file is part of the FPC documentation.
  4. % Copyright (C) 1997, by Michael Van Canneyt
  5. %
  6. % The FPC documentation is free text; you can redistribute it and/or
  7. % modify it under the terms of the GNU Library General Public License as
  8. % published by the Free Software Foundation; either version 2 of the
  9. % License, or (at your option) any later version.
  10. %
  11. % The FPC Documentation is distributed in the hope that it will be useful,
  12. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. % Library General Public License for more details.
  15. %
  16. % You should have received a copy of the GNU Library General Public
  17. % License along with the FPC documentation; see the file COPYING.LIB. If not,
  18. % write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. % Boston, MA 02111-1307, USA.
  20. %
  21. \documentclass{report}
  22. %
  23. % Preamble
  24. %
  25. \usepackage{a4}
  26. \usepackage{makeidx}
  27. \usepackage{html}
  28. \latex{\usepackage{fpc}}
  29. \html{\input{fpc-html.tex}}
  30. \makeindex
  31. %
  32. % start of document.
  33. %
  34. \begin{document}
  35. \title{Free Pascal :\\ Reference guide.}
  36. \docdescription{Reference guide for Free Pascal.}
  37. \docversion{1.4}
  38. \date{March 1998}
  39. \author{Micha\"el Van Canneyt
  40. % \\ Florian Kl\"ampfl
  41. }
  42. \maketitle
  43. \tableofcontents
  44. \newpage
  45. \listoftables
  46. \newpage
  47. \section*{About this guide}
  48. This document describes all constants, types, variables, functions and
  49. procedures as they are declared in the system unit.
  50. Furthermore, it describes all pascal constructs supported by \fpc, and lists
  51. all supported data types. It does not, however, give a detailed explanation
  52. of the pascal language. The aim is to list which Pascal constructs are
  53. supported, and to show where the \fpc implementation differs from the
  54. Turbo Pascal implementation.
  55. Throughout this document, we will refer to functions, types and variables
  56. with \var{typewriter} font. Functions and procedures gave their own
  57. subsections, and for each function or procedure we have the following
  58. topics:
  59. \begin{description}
  60. \item [Declaration] The exact declaration of the function.
  61. \item [Description] What does the procedure exactly do ?
  62. \item [Errors] What errors can occur.
  63. \item [See Also] Cross references to other related functions/commands.
  64. \end{description}
  65. The cross-references come in two flavours:
  66. \begin{itemize}
  67. \item References to other functions in this manual. In the printed copy, a
  68. number will appear after this reference. It refers to the page where this
  69. function is explained. In the on-line help pages, this is a hyperlink, on
  70. which you can click to jump to the declaration.
  71. \item References to Unix manual pages. (For linux related things only) they
  72. are printed in \var{typewriter} font, and the number after it is the Unix
  73. manual section.
  74. \end{itemize}
  75. %
  76. % The Pascal language
  77. %
  78. \chapter{Supported Pascal language constructs}
  79. In this chapter we describe the pascal constructs supported by \fpc, as well
  80. as the supported data types.
  81. This is not intended as an introduction to the Pascal language, although all
  82. language constructs will be covered. The main goal is to explain what is
  83. supported by \fpc, and where the Free implementation differs from the Turbo
  84. Pascal one.
  85. \section{Data types}
  86. \fpc supports the same data types as Turbo Pascal, with some extensions from
  87. Delphi.
  88. \subsection{Integer types}
  89. The integer types predefined in \fpc are listed in \seet{integers}.
  90. \begin{FPCltable}{lcr}{Predefined integer types}{integers}
  91. Type & Range & Size in bytes \\ \hline
  92. Byte & 0 .. 255 & 1 \\
  93. Shortint & -127 .. 127 & 1\\
  94. Integer & -32768 .. 32767 & 2 \\
  95. Word & 0 .. 65535 & 2 \\
  96. Longint & -2147483648 .. 2147483648 & 4\\
  97. Cardinal\footnote{The cardinal type support is buggy until version 0.9.3} & 0..4294967296 & 4 \\ \hline
  98. \end{FPCltable}
  99. \fpc does automatic type conversion in expressions where different kinds of
  100. integer types are used.
  101. \fpc supports hexadecimal format the same way as Turbo Pascal does. To
  102. specify a constant value in hexadecimal format, prepend it with a dollar
  103. sign (\var{\$}). Thus, the hexadecimal \var{\$FF} equals 255 decimal.
  104. In addition to the support for hexadecimal notation, \fpc also supports
  105. binary notation. You can specify a binary number by preceding it with a
  106. percent sign (\var{\%}). Thus, \var{255} can be specified in binary notation
  107. as \var{\%11111111}.
  108. \subsection{Real types}
  109. \fpc uses the math coprocessor (or an emulation) for al its floating-point
  110. calculations. The native type for the coprocessor is \var{Double}. Other
  111. than that, all Turbo Pascal real types are supported. They're listed in
  112. \seet{Reals}.
  113. \begin{FPCltable}{lccr}{Supported Real types}{Reals}
  114. Type & Range & Significant digits & Size\footnote{In Turbo Pascal.} \\ \hline
  115. Real & 2.9E-39 .. 1.7E38 & 11-12 & 6 \\
  116. Single & 1.5E-45 .. 3.4E38 & 7-8 & 4 \\
  117. Double & 5.0E-324 .. 1.7E308 & 15-16 & 8 \\
  118. Extended & 1.9E-4951 .. 1.1E4932 & 19-20 & 10\\
  119. %Comp\footnote{\var{Comp} only holds integer values.} & -2E64+1 .. 2E63-1 & 19-20 & 8 \\
  120. \end{FPCltable}
  121. Until version 0.9.1 of the compiler, all the real types are mapped to type
  122. \var{Double}, meaning that they all have size 8. From version 0.9.3, the
  123. \var{Extended} and \var{single} types are defined with the same suze as in
  124. Turbo Pascal. The \seef{SizeOf} function is your friend here.
  125. \subsection{Character types}
  126. \subsubsection{Char}
  127. \fpc supports the type \var{Char}. A \var{Char} is exactly 1 byte in
  128. size, and contains one character.
  129. You can specify a character constant by enclosing the character in single
  130. quotes, as follows : 'a' or 'A' are both character constants.
  131. You can also specify a character by their ASCII
  132. value, by preceding the ASCII value with the number symbol (\#). For example
  133. specifying \var{\#65} would be the same as \var{'A'}.
  134. Also, the caret character (\verb+^+) can be used in combination with a letter to
  135. specify a character with ASCII value less than 27. Thus \verb+^G+ equals
  136. \var{\#7} (G is the seventh letter in the alphabet.)
  137. If you want to represent the single quote character, type it two times
  138. successively, thus \var{''''} represents the single quote character.
  139. \subsubsection{Strings}
  140. \fpc supports the \var{String} type as it is defined in Turbo Pascal.
  141. To declare a variable as a string, use the following declaration:
  142. \begin{verbatim}
  143. Var
  144. S : String[Size];
  145. \end{verbatim}
  146. This will declare \var{S} as a variable of type \var{String}, with maximum
  147. length \var{Size}. \var{Size} can be any value from \var{1} to \var{255}.
  148. \fpc reserves \var{Size+1} bytes for the string \var{S}, and in the zeroeth
  149. element of the string (\var{S[0]}) it will store the length of the variable.
  150. If you don't specify the size of the string, \var{255} is taken as a
  151. default.
  152. To specify a constant string, you enclose the string in single-quotes, just
  153. as a \var{Char} type, only now you can have more than one character.
  154. Given that \var{S} is of type \var{String}, the following are valid assignments:
  155. \begin{verbatim}
  156. S:='This is a string.';
  157. S:='One'+', Two'+', Three';
  158. S:='This isn''t difficult !';
  159. S:='This is a weird character : '#145' !';
  160. \end{verbatim}
  161. As you can see, the single quote character is represented by 2 single-quote
  162. characters next to each other. Strange characters can be specified by their
  163. ASCII value.
  164. The example shows also that you can add two strings. The resulting string is
  165. just the concatenation of the first with the second string, without spaces in
  166. between them. Strings can not be substracted, however.
  167. \subsubsection{PChar}
  168. \fpc supports the Delphi implementation of the \var{PChar} type. \var{PChar}
  169. is defined as a pointer to a \var{Char} type, but allows additional
  170. operations.
  171. The \var{PChar} type can be understood best as the Pascal equivalent of a
  172. C-style null-terminated string, i.e. a variable of type \var{PChar} is a pointer
  173. that points to an array of type \var{Char}, which is ended by a
  174. null-character (\var{\#0}).
  175. \fpc supports initializing of \var{PChar} typed constants, or a direct
  176. assignment. For example, the following pieces of code are equivalent:
  177. \begin{CodEx}
  178. \begin{verbatim}
  179. program one;
  180. var p : pchar;
  181. begin
  182. P:='This is a null-terminated string.';
  183. writeln (P);
  184. end.
  185. \end{verbatim}
  186. \end{CodEx}
  187. Results in the same as
  188. \begin{CodEx}
  189. \begin{verbatim}
  190. program two;
  191. const P : PChar = 'This is a null-terminated string.'
  192. begin
  193. Writeln (P);
  194. end.
  195. \end{verbatim}
  196. \end{CodEx}
  197. These examples also show that it is possible to write {\em the contents} of
  198. the string to a file of type \var{Text}.
  199. The \seestrings\_ unit contains procedures and functions that manipulate the
  200. \var{PChar} type as you can do it in C.
  201. Since it is equivalent to a pointer to a type \var{Char} variable, it is
  202. also possible to do the following:
  203. \begin{CodEx}
  204. \begin{verbatim}
  205. Program three;
  206. Var S : String[30];
  207. P : Pchar;
  208. begin
  209. S:='This is a null-terminated string.'#0;
  210. P:=@S[1];
  211. writeln (P);
  212. end.
  213. \end{verbatim}
  214. \end{CodEx}
  215. This will have the same result as the previous two examples.
  216. You cannot add null-terminated strings as you can do with normal Pascal
  217. strings. If you want to concatenate two \var{PChar} strings, you will need
  218. to use the \seestrings unit.
  219. However, it is possible to do some pointer arithmetic. You can use the
  220. operators \var{+} and \var{-} to do operations on \var{PChar} pointers.
  221. In \seet{PCharMath}, \var{P} and \var{Q} are of type \var{PChar}, and
  222. \var{I} is of type \var{Longint}.
  223. \begin{FPCltable}{lr}{\var{PChar} pointer arithmetic}{PCharMath}
  224. Operation & Result \\ \hline
  225. \var{P + I} & Adds \var{I} to the address pointed to by \var{P}. \\
  226. \var{I + P} & Adds \var{I} to the address pointed to by \var{P}. \\
  227. \var{P - I} & Substracts \var{I} from the address pointed to by \var{P}. \\
  228. \var{P - Q} & Returns, as an integer, the distance between 2 addresses \\
  229. & (or the number of characters between \var{P} and \var{Q}) \\
  230. \hline
  231. \end{FPCltable}
  232. \subsection{Booleans}
  233. \fpc supports the \var{Boolean} type, with its two pre-defined possible
  234. values \var{True} and \var{False}. These are the only two values that can be
  235. assigned to a \var{Boolean} type. Of course, any expression that resolves
  236. to a \var{boolean} value, can also be assigned to a boolean type.
  237. Assuming \var{B} to be of type \var{Boolean}, the following are valid
  238. assignments:
  239. \begin{verbatim}
  240. B:=True;
  241. B:=False;
  242. B:=1<>2; { Results in B:=True }
  243. \end{verbatim}
  244. Boolean expressions are also used in conditions.
  245. {\em Remark:} In \fpc, boolean expressions are always evaluated in such a
  246. way that when the result is known, the rest of the expression will no longer
  247. be evaluated (Called short-cut evaluation). In the following example, the function \var{Func} will never
  248. be called, which may have strange side-effects.
  249. \begin{verbatim}
  250. ...
  251. B:=False;
  252. A := B and Func;
  253. \end{verbatim}
  254. Here \var{Func} is a function which returns a \var{Boolean} type.
  255. \subsection{Arrays}
  256. \fpc supports arrays as in Turbo Pascal, multi-dimensional arrays
  257. and packed arrays are also supported.
  258. \subsection{Pointers}
  259. \fpc supports the use of pointers. A variable of the type \var{Pointer}
  260. contains an address in memory, where the data of another variable may be
  261. stored.
  262. Pointers can be typed, which means that they point to a particular kind of
  263. data. The type of this data is known at compile time.
  264. Consider the following example:
  265. \begin{CodEx}
  266. \begin{verbatim}
  267. Program pointers;
  268. type
  269. Buffer = String[255];
  270. BufPtr = ^Buffer;
  271. Var B : Buffer;
  272. BP : BufPtr;
  273. PP : Pointer;
  274. etc..
  275. \end{verbatim}
  276. \end{CodEx}
  277. In this example, \var{BP} {\em is a pointer to} a \var{Buffer} type; while \var{B}
  278. {\em is} a variable of type \var{Buffer}. \var{B} takes 256 bytes memory,
  279. and \var{BP} only takes 4 bytes of memory (enough to keep an adress in
  280. memory).
  281. {\em Remark:} \fpc treats pointers much the same way as C does. This means
  282. that you can treat a pointer to some type as being an array of this type.
  283. The pointer then points to the zeroeth element of this array. Thus the
  284. following pointer declaration
  285. \begin{verbatim}
  286. Var p : ^Longint;
  287. \end{verbatim}
  288. Can be considered equivalent to the following array declaration:
  289. \begin{verbatim}
  290. Var p : array[0..Infinity] of Longint;
  291. \end{verbatim}
  292. The reference \verb+P^+ is then the same as \var{p[0]}. The following program
  293. illustrates this maybe more clear:
  294. \begin{CodEx}
  295. \begin{verbatim}
  296. program PointerArray;
  297. var i : longint;
  298. p : ^longint;
  299. pp : array[0..100] of longint;
  300. begin
  301. for i:=0 to 100 do pp[i]:=i; { Fill array }
  302. p:=@pp[0]; { Let p point to pp }
  303. for i:=0 to 100 do if p[i]<>pp[i] then writeln ('Ohoh, problem !')
  304. end.
  305. \end{verbatim}
  306. \end{CodEx}
  307. \fpc doesn't support pointer arithmetic as C does, however.
  308. \subsection{Procedural types}
  309. \fpc has support for procedural types, although it differs from the Turbo
  310. Pascal implementation of them.
  311. The type declaration remains the same. The two following examples are valid
  312. type declarations:
  313. \begin{verbatim}
  314. Type TOneArg = Procedure (Var X : integer);
  315. TNoArg = Function : Real;
  316. var proc : TOneArg;
  317. func : TNoArg;
  318. \end{verbatim}
  319. Given these declarations, the following assignments are valid:
  320. \begin{verbatim}
  321. Procedure printit (Var X : Integer);
  322. begin
  323. writeln (x);
  324. end;
  325. ...
  326. P:=@printit;
  327. Func:=@Pi;
  328. \end{verbatim}
  329. From this example, the difference with Turbo Pascal is clear: In Turbo
  330. Pascal it isn't necessary to use the address operator (\var{@})
  331. when assigning a procedural type variable, whereas in \fpc it is required.
  332. \subsection{Records}
  333. \fpc supports records. The prototype type definition of a record is:
  334. \begin{verbatim}
  335. Type
  336. RecType = Record
  337. Element1 : type1;
  338. Element2,Element3 : type2;
  339. ...
  340. Elementn ; Typen;
  341. end;
  342. \end{verbatim}
  343. Variant records are also supported:
  344. \begin{verbatim}
  345. Type
  346. RecType = Record
  347. Element1 : type1;
  348. Case [PivotElmt:] Type Identifier of
  349. Value1 : (VarElt1, Varelt2 : Vartype1);
  350. Value2 : (VarElt3, Varelt4 : Vartype2);
  351. end;
  352. \end{verbatim}
  353. The variant part must be last in the record. The optional \var{PivotElmt}
  354. can be used to see which variant is active at a certain time.
  355. {\em Remark:} If you want to read a typed file with records, produced by
  356. a Turbo Pascal program, then chances are that you will not succeed in
  357. reading that file correctly.
  358. The reason for this is that by default, elements of a record are aligned at
  359. 2-byte boundaries, for performance reasons. This default behaviour can be
  360. changed with the \var{\{\$PackRecords n\}} switch. Possible values for
  361. \var{n} are 1, 2 and 4. This switch tells the compiler to align elements of
  362. a record or object or class on 1,2 or 4 byte boundaries.
  363. Take a look at the following program:
  364. \begin{CodEx}
  365. \begin{verbatim}
  366. Program PackRecordsDemo;
  367. type {$PackRecords 2}
  368. Trec1 = Record
  369. A : byte;
  370. B : Word;
  371. end;
  372. {$PACKRECORDS 1}
  373. Trec2 = Record
  374. A : Byte;
  375. B : Word;
  376. end;
  377. begin
  378. Writeln ('Size Trec1 : ',SizeOf(Trec1));
  379. Writeln ('Size Trec2 : ',SizeOf(Trec2));
  380. end.
  381. \end{verbatim}
  382. \end{CodEx}
  383. The output of this program will be :
  384. \begin{verbatim}
  385. Size Trec1 : 4
  386. Size Trec2 : 3
  387. \end{verbatim}
  388. And this is as expected. In \var{Trec1}, each of the elements \var{A} and
  389. \var{B} takes 2 bytes of memory, and in \var{Trec1}, \var{A} takes only 1
  390. byte of memory.
  391. {\em Remark:} As from version 0.9.3 (a developers' version), \fpc supports also the
  392. 'packed record', this is a record where all the elements are byte-aligned.
  393. Thus the two following declarations are equivalent:
  394. \begin{verbatim}
  395. {$PACKRECORDS 1}
  396. Trec2 = Record
  397. A : Byte;
  398. B : Word;
  399. end;
  400. {$PACKRECORDS 2}
  401. \end{verbatim}
  402. and
  403. \begin{verbatim}
  404. Trec2 = Packed Record
  405. A : Byte;
  406. B : Word;
  407. end;
  408. \end{verbatim}
  409. Note the \var{\{\$PACKRECORDS 2\}} after the first declaration !
  410. \subsection{Set types}
  411. \fpc supports the set types as in Turbo Pascal. The prototype of a set
  412. declaration is:
  413. \begin{verbatim}
  414. SetType = Set of TargetType;
  415. \end{verbatim}
  416. Each of the elements of \var{SetType} must be of type \var{TargetType}.
  417. \var{TargetType} can be any ordinal type with a range between \var{0} and
  418. \var{255}. A set can contain maximally \var{255} elements.
  419. The following is a valid set declaration:
  420. \begin{verbatim}
  421. Type Days = (Mon, Tue, Wed, Thu, Fri, Sqt, Sun);
  422. Var WeekDays : Set of days;
  423. \end{verbatim}
  424. Given this set declaration, the follwing assignment is legal:
  425. \begin{verbatim}
  426. WeekDays := [ Mon, Tue, Wed, Thu, Fri];
  427. \end{verbatim}
  428. The operators for manipulations of sets are listed in \seet{SetOps}.
  429. \begin{FPCltable}{lr}{Set Manipulation operators}{SetOps}
  430. Operation & Operator \\ \hline
  431. Union & + \\
  432. Difference & - \\
  433. Intersection & * \\ \hline
  434. \end{FPCltable}
  435. You can compare two sets with the \var{<>} and \var{=} operators, but not
  436. (yet) with the \var{<} and \var{>} operators.
  437. From compiler version 0.9.5, the compiler stores small sets (less than 32
  438. elements) in a longint, if the type range allows it. This allows for faster
  439. processing and decreases program size.
  440. \subsection{Enumeration types}
  441. Enumeration types are supported in \fpc. On top of the Turbo Pascal
  442. implementation, \fpc allows the following C-style extension of the
  443. enumeration type.
  444. \begin{verbatim}
  445. Type
  446. EnumType = (one, two, three, forty := 40);
  447. \end{verbatim}
  448. As a result, the ordinal number of \var{forty} is \var{40}, and not \var{4},
  449. as it would be when the \var{'= 40'} wasn't present.
  450. When specifying such an enumeration type, it is important to keep in mind
  451. that you should keep initialized set elements in ascending order. The
  452. following will produce a compiler error:
  453. \begin{verbatim}
  454. Type
  455. EnumType = (one, two, three, forty := 40, thirty:=30);
  456. \end{verbatim}
  457. It is necessary to keep \var{forty} and \var{Thirty} in the correct order.
  458. {\em Remark :} You cannot use the \var{Pred} and \var{Succ} functions on
  459. this kind of enumeration types. If you try to do that, you'll get a compiler
  460. error.
  461. \section{Constants}
  462. Just as in Turbo Pascal, \fpc supports both normal and typed constants.
  463. \subsection{Ordinary constants}
  464. Ordinary constants declarations are no different from the TP implementation.
  465. You can only declare constants of the following types: \var{Ordinal types},
  466. \var{Real types}, \var{Char}, and \var{String}.
  467. The following are all valid constant declarations:
  468. \begin{verbatim}
  469. Const
  470. e = 2.7182818; { Real type constant. }
  471. a = 2; { Integer type constant. }
  472. c = '4'; { Character type constant. }
  473. s = 'This is a constant string'; {String type constant.}
  474. \end{verbatim}
  475. Assigning a value to a constant is not permitted. Thus, given the previous
  476. declaration, the following will result in a compiler error:
  477. \begin{verbatim}
  478. s:='some other string';
  479. \end{verbatim}
  480. \subsection{Typed constants}
  481. Typed constants serve to provide a program with initialized variables.
  482. Contrary to ordinary constants, they may be assigned to at run-time.
  483. The difference with normal variables is that their value is initialised
  484. when the program starts, whereas normal variables must be initialised
  485. explicitly.
  486. The prototype of a typed constant declaration is:
  487. \begin{verbatim}
  488. Const
  489. SomeConst : SomeType = SomeValue;
  490. \end{verbatim}
  491. After that, the constant \var{SomeConst} will be of type \var{SomeType}, and
  492. have initial value \var{SomeValue}.
  493. Given the declaration:
  494. \begin{verbatim}
  495. Const
  496. S : String = 'This is a typed constant string';
  497. \end{verbatim}
  498. The following is a valid assignment:
  499. \begin{verbatim}
  500. S:='Result : '+Func;
  501. \end{verbatim}
  502. Where \var{Func} is a function that returns a \var{String}.
  503. Typed constants also allow you to initialize arrays and records. For arrays,
  504. the initial elements must be specified, surrounded by round brackets, and
  505. separated by commas. The number of elements must be exactly the same as
  506. number of elements in the declaration of the type.
  507. As an example:
  508. \begin{verbatim}
  509. Const
  510. tt : array [1..3] of string[20] = ('ikke','gij', 'hij');
  511. ti : array [1..3] of longint = (1,2,3);
  512. \end{verbatim}
  513. For constant records, you should specify each element of the record, in the
  514. form \var{Field : Value}, separated by commas, and surrounded by round
  515. brackets.
  516. As an example:
  517. \begin{verbatim}
  518. Type
  519. Point = record
  520. X,Y : Real
  521. end;
  522. Const
  523. Origin : Point = (X:0.0 , Y:0.0);
  524. \end{verbatim}
  525. The order of the fields in a constant record needs to be the same as in the type declaration,
  526. otherwise you'll get a compile-time error.
  527. \section{Objects}
  528. \fpc supports object oriented programming. In fact, part of the compiler is
  529. written using objects. Here we present some technical questions regarding
  530. object oriented programming in \fpc.
  531. \fpc supports 2 programming models for object-oriented programming.
  532. You can choose to program object oriented using the Turbo Pascal approach,
  533. or you can prefer the Delphi approach.
  534. \subsection{The Turbo Pascal approach}
  535. In the Turbo Pascal approach, Objects should be treated as a special kind of
  536. record. The record contains all the fields that are declared in the objects
  537. definition, and pointers to the methods that are associated to the objects'
  538. type.
  539. An object is declared just as you would declare a record; except that you
  540. can now declare procedures and fuctions as of they were part of the record.
  541. Objects can ''inherit'' fields and methods from ''parent'' objects. This means
  542. that you can use these fields and methods as if the were included in the
  543. objects you declared as a ''child'' object.
  544. Furthermore, you can declare fields, procedures and functions as \var{public}
  545. or \var{private}. By default, fields and methods are \var{public}, and are
  546. exported outside the current unit. Fields or methods that are declared
  547. \var{private} are only accessible in the current unit.
  548. The prototype declaration of an object is as follows :
  549. \begin{verbatim}
  550. TObj = Object [(ParentObjectType)]
  551. [Constructor ConstructorName;]
  552. [Destructor DestructorName;]
  553. Field1 : Type1;
  554. ...
  555. Fieldn : Typen;
  556. Method1;
  557. Method2;
  558. [private
  559. PrField1 : PrType1;
  560. ...
  561. PrFieldn : PrTypen;
  562. PrMethod1;
  563. ...
  564. PrMethodn;]
  565. [public
  566. PuField1 : PuType1;
  567. ..
  568. Pufield1 : PuTypen;
  569. PuMethod1;
  570. ...
  571. PuMethodn;]
  572. end;
  573. \end{verbatim}
  574. You can repeat as many \var{private} and \var{public} blocks as you want.
  575. \var{Method}s are normal function or procedure declarations.
  576. As can be seen in the prototype object declaration, \fpc supports
  577. constructors and destructors. You are responsible for calling the
  578. destructor and constructor explicitly when using objects.
  579. \fpc supports also the extended syntax of the \var{New} and \var{Dispose}
  580. procedures. In case you want to allocate a dynamic varible of an object
  581. type, you can specify the constructor's name in the call to \var{New}.
  582. The \var{New} is implemented as a function which returns a pointer to the
  583. instantiated object. Given the following declarations :
  584. \begin{verbatim}
  585. Type
  586. TObj = object;
  587. Constructor init;
  588. ...
  589. end;
  590. Pobj = ^TObj;
  591. Var PP : Pobj;
  592. \end{verbatim}
  593. Then the following 3 calls are equivalent :
  594. \begin{verbatim}
  595. pp:=new (Pobj,Init);
  596. \end{verbatim}
  597. and
  598. \begin{verbatim}
  599. new(pp,init);
  600. \end{verbatim}
  601. and also
  602. \begin{verbatim}
  603. new (pp);
  604. pp^.init;
  605. \end{verbatim}
  606. In the last case, the compiler will issue a warning that you should use the
  607. extended syntax of \var{new} and \var{dispose} to generate instances of an
  608. object. You can ignore this warning, but it's better programming practice to
  609. use the extended syntax to create instances of an object.
  610. Similarly, the \var{Dispose} procedure accepts the name of a destructor. The
  611. destructor will then be called, before removing the object from the heap.
  612. In view of the compiler warning remark, the now following Delphi approach may
  613. be considered a more natural way of object-oriented programming.
  614. {\em Remark:}
  615. \fpc also supports the packed object. This is the same as an object, only
  616. the elements (fields) of the object are byte-aligned, just as in the packed
  617. record.
  618. The declaration of a packed object is similar to the declaration
  619. of a packed record :
  620. \begin{verbatim}
  621. Type
  622. TObj = packed object;
  623. Constructor init;
  624. ...
  625. end;
  626. Pobj = ^TObj;
  627. Var PP : Pobj;
  628. \end{verbatim}
  629. Similarly, the \var{\{\$PACKRECORDS \}} directive acts on objects as well.
  630. \subsection{The Delphi approach}
  631. In the Delphi approach to Object Oriented Programming, everything revolves
  632. around the concept of 'Classes'.
  633. A class can be seen as a pointer to an object, or a pointer to a record.
  634. The prototype declaration of a class is as follows :
  635. \begin{verbatim}
  636. TObj = Class [(ParentClassType)]
  637. [Constructor ConstructorName;]
  638. [Destructor DestructorName;]
  639. Field1 : Type1;
  640. ...
  641. Fieldn : Typen;
  642. Method1;
  643. Method2;
  644. [private
  645. PrField1 : PrType1;
  646. ...
  647. PrFieldn : PrTypen;
  648. PrMethod1;
  649. ...
  650. PrMethodn;]
  651. [public
  652. PuField1 : PuType1;
  653. ..
  654. Pufield1 : PuTypen;
  655. PuMethod1;
  656. ...
  657. PuMethodn;]
  658. end;
  659. \end{verbatim}
  660. You can repeat as many \var{private} and \var{public} blocks as you want.
  661. \var{Method}s are normal function or procedure declarations.
  662. As you can see, the declaration of a class is almost identical to the
  663. declaration of an object. The real difference between objects and classes
  664. is in the way they are created;
  665. Classes must be created using their constructor. Remember that A class is a
  666. pointer to an object, so when you declare a variable of some class, the
  667. compiler just allocates a pointer, not the entire object. The constructor of
  668. a class returns a pointer to an initialized instance of the object.
  669. So, to initialize an instance of some class, you do the following :
  670. \begin{verbatim}
  671. ClassVar:=ClassType.ConstructorName;
  672. \end{verbatim}
  673. {\em Remark :}
  674. \begin{itemize}
  675. \item \fpc doesn't support the concept of properties yet.
  676. \item The \var{\{\$Packrecords \}} directive also affects classes.
  677. i.e. the alignment in memory of the different fields depends on the
  678. value of the \var{\{\$Packrecords \}} directive.
  679. \item Just as for objects and records, you can declare a packed class.
  680. This has the same effect as on an object, or record, namely that the
  681. elements are aligned on 1-byte boundaries. i.e. as close as possible.
  682. \end{itemize}
  683. \section{Statements controlling program flow.}
  684. \subsection{Assignments}
  685. In addition to the standard Pascal assignment operator (\var{:=}), \fpc
  686. supports some c-style constructions. All available constructs are listed in
  687. \seet{assignments}.
  688. \begin{FPCltable}{lr}{Allowed C constructs in \fpc}{assignments}
  689. Assignment & Result \\ \hline
  690. a += b & Adds \var{b} to \var{a}, and stores the result in \var{a}.\\
  691. a -= b & Substracts \var{b} from \var{a}, and stores the result in
  692. \var{a}. \\
  693. a *= b & Multiplies \var{a} with \var{b}, and stores the result in
  694. \var{a}. \\
  695. a /= b & Divides \var{a} through \var{b}, and stores the result in
  696. \var{a}. \\ \hline
  697. \end{FPCltable}
  698. For these connstructs to work, you should specify the \var{-Sc}
  699. command-line switch.
  700. {\em Remark:} These constructions are just for typing convenience, they
  701. don't generate different code.
  702. \fpc also supports typed assignments. This means that an assignment
  703. statement has a definite type, and hence can be assigned to another
  704. variable. The type of the assignment \var{a:=b} is the type of \var{a}
  705. (or, in this case, of \var{b}), and this can be assigned to another
  706. variable : \var{c:=a:=b;}.
  707. To summarize: the construct
  708. \begin{verbatim}
  709. a:=b:=c;
  710. \end{verbatim}
  711. results in both \var{a} and \var{b} being assign the value of \var{c}, which
  712. may be an expression.
  713. For this construct to be allowed, it is necessary to specify the \var{-Sa4}
  714. switch on the command line.
  715. \subsection{The \var{Case} statement}
  716. \fpc supports the \var{case} statement. Its prototype is
  717. \begin{verbatim}
  718. Case Pivot of
  719. Label1 : Statement1;
  720. Label2 : Statement2;
  721. ...
  722. Labeln : Statementn;
  723. [Else
  724. AlternativeStatement]
  725. end;
  726. \end{verbatim}
  727. \var{label1} until \var{Labeln} must be known at compile-time, and can be of
  728. the following types : enumeration types, Ordinal types (except boolean), and
  729. chars. \var{Pivot} must also be one of these types.
  730. The statements \var{Statement1} etc., can be compound statements (i.e. a
  731. \var{begin..End} block).
  732. {\em Remark:} Contrary to Turbo Pascal, duplicate case labels are not
  733. allowed in \fpc, so the following code will generate an error when
  734. compiling:
  735. \begin{verbatim}
  736. Var i : integer;
  737. ...
  738. Case i of
  739. 3 : DoSomething;
  740. 1..5 : DoSomethingElse;
  741. end;
  742. \end{verbatim}
  743. The compiler will generate a \var{Duplicate case label} error when compiling
  744. this, because the 3 also appears (implicitly) in the range \var{1..5}
  745. {\em Remark:} In versions earlier than 0.9.7, there was an incompatibility here
  746. with Turbo Pascal. Where in Turbo Pascal you could do the following:
  747. \begin{verbatim}
  748. case Pivot of
  749. ...
  750. Else
  751. begin
  752. Statement1
  753. Statement2
  754. end;
  755. \end{verbatim}
  756. You needed to do the following in \fpc :
  757. \begin{verbatim}
  758. case Pivot of
  759. ...
  760. Else
  761. begin
  762. Statement1
  763. Statement2
  764. end;
  765. end;
  766. \end{verbatim}
  767. So there's an extra \var{end} keyword at the end. But from version 0.9.7
  768. this has been fixed.
  769. \subsection{The \var{For..to/downto..do} statement}
  770. \fpc supports the \var{For} loop construction. The prototypes are:
  771. \begin{verbatim}
  772. For Counter:=Lowerbound to Upperbound Do Statement;
  773. or
  774. For Counter:=Upperbound downto Lowerbound Do Statement;
  775. \end{verbatim}
  776. \var{Statement} can be a compound statement. In the first case, if
  777. \var{Lowerbound} is larger than \var{Upperbound} then \var{Statement} will
  778. never be executed.
  779. \subsection{The \var{Goto} statement}
  780. \fpc supports the \var{goto} jump statement. Its prototype is
  781. \begin{verbatim}
  782. var
  783. jumpto : label
  784. ...
  785. Jumpto :
  786. Statement;
  787. ...
  788. Goto jumpto;
  789. ...
  790. \end{verbatim}
  791. The jump label must be defined in the same block as the \var{Goto}
  792. statement.
  793. To be able to use the \var{Goto} statement, you need to specify the \var{-Sg}
  794. compiler switch.
  795. \subsection{The \var{If..then..else} statement}
  796. The \var{If .. then .. else..} prototype is:
  797. \begin{verbatim}
  798. If Expression1 Then Statement1;
  799. or
  800. If Expression2 then
  801. Statement2
  802. else
  803. Statement3;
  804. \end{verbatim}
  805. Be aware of the fact that the boolean expressions \var{Expression1} and
  806. \var{Expression2} will be short-cut evaluated. (Meaning that the evaluation
  807. will be stopped at the point where the outcome is known with certainty)
  808. Also, after \var{Statement2}, no semicolon (\var{;}) is alllowed.
  809. All statements can be compound statements.
  810. \subsection{The \var{Repeat..until} statement}
  811. The prototype of the \var{Repeat..until} statement is
  812. \begin{verbatim}
  813. Repeat
  814. Statement1;
  815. Statement2;
  816. Until Expression;
  817. \end{verbatim}
  818. This will execute \var{Statement1} etc. until \var{Expression} evaluates to
  819. \var{True}. Since \var{Expression} is evaluated {\em after} the execution of the
  820. statements, they are executed at least once.
  821. Be aware of the fact that the boolean expressions \var{Expression1} and
  822. \var{Expression2} will be short-cut evaluated. (Meaning that the evaluation
  823. will be stopped at the point where the outcome is known with certainty)
  824. \subsection{The \var{While..do} statement}
  825. The prototype of the \var{While..do} statement is
  826. \begin{verbatim}
  827. While Expression Do
  828. Statement;
  829. \end{verbatim}
  830. This will execute \var{Statement} as long as \var{Expression} evaluates to
  831. \var{True}. Since \var{Expression} is evaluated {\em before} the execution
  832. of \var{Statement}, it is possible that \var{Statement} isn't executed at
  833. all.
  834. \var{Statement} can be a compound statement.
  835. Be aware of the fact that the boolean expressions \var{Expression1} and
  836. \var{Expression2} will be short-cut evaluated. (Meaning that the evaluation
  837. will be stopped at the point where the outcome is known with certainty)
  838. \subsection{The \var{With} statement}
  839. The with statement serves to access the elements of a record, without
  840. having to specify the name of the record. Given the declaration:
  841. \begin{verbatim}
  842. Type Passenger = Record
  843. Name : String[30];
  844. Flight : String[10];
  845. end;
  846. Var TheCustomer : Passenger;
  847. \end{verbatim}
  848. The following statements are completely equivalent:
  849. \begin{verbatim}
  850. TheCustomer.Name:='Michael';
  851. TheCustomer.Flight:='PS901';
  852. \end{verbatim}
  853. and
  854. \begin{verbatim}
  855. With TheCustomer do
  856. begin
  857. Name:='Michael';
  858. Flight:='PS901';
  859. end;
  860. \end{verbatim}
  861. \subsection{Compound statements}
  862. Compound statements are a group of statements, separated by semicolons,
  863. that are surrounded by the keywords \var{Begin} and \var{End}. The
  864. Last statement doesn't need to be followed by a semicolon, although it is
  865. allowed.
  866. \section{Using functions and procedures}
  867. \fpc supports the use of functions and procedures, but with some extras:
  868. Function overloading is supported, as well as \var{Const} parameters and
  869. open arrays.
  870. {\em remark:} In the subsequent paragraph the word \var{procedure} and
  871. \var{function} will be used interchangeably. The statements made are
  872. valid for both.
  873. \subsection{Function overloading}
  874. Function overloading simply means that you can define the same function more
  875. than once, but each time with a different set of arguments.
  876. When the compiler encounters a unction call, it will look at the function
  877. parameters to decide which od the defined function
  878. This can be useful if you want to define the same function for different
  879. types. For example, if the RTL, the \var{Dec} procedure is
  880. is defined as:
  881. \begin{verbatim}
  882. ...
  883. Dec(Var I : longint;decrement : longint);
  884. Dec(Var I : longint);
  885. Dec(Var I : Byte;decrement : longint);
  886. Dec(Var I : Byte);
  887. ...
  888. \end{verbatim}
  889. When the compiler encounters a call to the dec function, it wil first search
  890. which function it should use. It therefore checks the parameters in your
  891. function call, and looks if there is a function definition which maches the
  892. specified parameter list. If the compiler finds such a function, a call is
  893. inserted to that function. If no such function is found, a compiler error is
  894. generated.
  895. \subsection{\var{Const} parameters}
  896. In addition to \var{var} parameters and normal parameters (call by value,
  897. call by reference), \fpc also supports \var{Const} parameters. You can
  898. specify a \var{Const} parameter as follows:
  899. \begin{verbatim}
  900. Function Name (Const S: Type_Of_S) : ResultType
  901. \end{verbatim}
  902. A constant argument is passed by refenence
  903. (i.e. the function or procedure receives a pointer to the passed ,
  904. but you are not allowed to assign to it, this will result in a compiler error.
  905. The main use for this is reducing the stack size, hence improving
  906. performance.
  907. \subsection{Open array parameters}
  908. \fpc supports the passing of open arrays, i.e. You can declare a procedure
  909. with an array of unspecified length as a parameter, as in Delphi.
  910. The prototype declaration for open array parameters is:
  911. \begin{verbatim}
  912. Function Func ( ... [Var|Const] Ident : Array of Type ...) : ReturnType;
  913. ProcedureFunction Func (... [Var|Const] Ident : Array of Type ...);
  914. \end{verbatim}
  915. The \var{[Var|Const]} means that open parameters can be passed by reference
  916. or as a constant parameter.
  917. In a function or procedure, you can pass open arrays only to functions which
  918. are also declared with open arrays as parameters, {\em not} to functions or
  919. procedures which accept arrays of fixed length.
  920. \section{Using assembler in your code}
  921. \fpc supports the use of assembler in your code, but not inline
  922. assembler. assembly functions (i.e. functions declared with the
  923. \var{Assembler} keyword) are supported as of version 0.9.7.
  924. {\em Remark :}
  925. \fpc issues AT\&T assembly language, as understood by most
  926. unix assemblers (most notably : GNU \var{as}).
  927. Intel assembler syntax is available as of version 0.9.8 but the Intel
  928. support isn't complete in the sense that it is converted to AT\&T syntax,
  929. and some constructions aren't supported by the conversion mechanism (see
  930. \progref for more information about this).
  931. Therefore all examples of assembly language will be given in AT\&T syntax,
  932. as it is the 'native' assembly from \fpc.
  933. The following is an example of assembler inclusion in your code.
  934. \begin{verbatim}
  935. ...
  936. Statements;
  937. ...
  938. Asm
  939. Movl 0,%eax
  940. ...
  941. end;
  942. ...
  943. Statements;
  944. \end{verbatim}
  945. The assembler instructions between the \var{Asm} and \var{end} keywords will
  946. be inserted in the assembler generated by the compiler.
  947. You can still use comditionals in your assembler, the compiler will
  948. recognise it, and treat it as any other conditionals.
  949. Contrary to Turbo Pascal, it isn't possible (yet) to reference variables by
  950. their names in the assembler parts of your code.
  951. \section{Modifiers}
  952. \fpc doesn't support all Turbo Pascal modifiers, but
  953. does support a number of additional modifiers. They are used mainly for assembler and
  954. reference to C object files.
  955. \subsection{Public}
  956. The \var{Public} keyword is used to declare a function globally in a unit.
  957. This is useful if you don't want a function to be accessible from the unit
  958. file, but you do want the function to be accessible from the object file.
  959. as an example:
  960. \begin{CodEx}
  961. \begin{verbatim}
  962. Unit someunit;
  963. interface
  964. Function First : Real;
  965. Implementation
  966. Function First : Real;
  967. begin
  968. First:=0;
  969. end;
  970. Function Second : Real; [Public];
  971. begin
  972. Second:=1;
  973. end;
  974. end.
  975. \end{verbatim}
  976. \end{CodEx}
  977. If another program or unit uses this unit, it will not be able to use the
  978. function \var{Second}, since it isn't declared in the interface part.
  979. However, it will be possible to access the function \var{Second} at the
  980. assembly-language level, by using it's mangled name (\progref).
  981. \subsection{cdecl}
  982. \label{se:cdecl}
  983. The \var{cdecl} modifier can be used to declare a function that uses a C
  984. type calling convention. This must be used if you wish to acces functions in
  985. an object file generated by a C compiler. It allows you to use the function in
  986. your code, and at linking time, you must link the object file containing the
  987. \var{C} implementation of the function or procedure.
  988. As an example:
  989. \begin{CodEx}
  990. \begin{verbatim}
  991. program CmodDemo;
  992. {$LINKLIB c}
  993. Const P : Pchar = 'This is fun !';
  994. Function strlen (P : Pchar) : Longint; cdecl; external;
  995. begin
  996. Writeln ('Length of (',p,') : ',strlen(p))
  997. end.
  998. \end{verbatim}
  999. \end{CodEx}
  1000. When compiling this, and linking to the C-library, you will be able to call
  1001. the \var{strlen} function throughout your program. The \var{external}
  1002. directive tells the compiler that the function resides in an external
  1003. object filebrary (see \ref{se:external}).
  1004. {\em Remark} The parameters in our declaration of the \var{C} function should
  1005. match exactly the ones in the declaration in \var{C}. Since \var{C} is case
  1006. sensitive, this means also that the name of the
  1007. function must be exactly the same. the \fpc compiler will use the name {\em
  1008. exactly} as it is typed in the declaration.
  1009. \subsection{popstack}
  1010. \label{se:popstack}
  1011. Popstack does the same as \var{cdecl}, namely it tells the \fpc compiler
  1012. that a function uses the C calling convention. In difference with the
  1013. \var{cdecl} modifier, it still mangles the name of the function as it would
  1014. for a normal pascal function.
  1015. With \var{popstack} you could access functions by their pascal names in a
  1016. library.
  1017. \subsection{external}
  1018. \label{se:external}
  1019. The \var{external} modifier can be used to declare a function that resides in
  1020. an external object file. It allows you to use the function in
  1021. your code, and at linking time, you must link the object file containing the
  1022. implementation of the function or procedure.
  1023. As an example:
  1024. \begin{CodEx}
  1025. \begin{verbatim}
  1026. program CmodDemo;
  1027. {$Linklib c}
  1028. Const P : Pchar = 'This is fun !';
  1029. Function strlen (P : Pchar) : Longint; cdecl; external;
  1030. begin
  1031. Writeln ('Length of (',p,') : ',strlen(p))
  1032. end.
  1033. \end{verbatim}
  1034. \end{CodEx}
  1035. {\em Remark} The parameters in our declaration of the \var{external} function
  1036. should match exactly the ones in the declaration in the object file.
  1037. Since \var{C} is case sensitive, this means also that the name of the
  1038. function must be exactly the same.
  1039. The \var{external} modifier has also an extended syntax:
  1040. \begin{enumerate}
  1041. \item
  1042. \begin{verbatim}
  1043. external 'lname';
  1044. \end{verbatim}
  1045. Tells the compiler that the function resides in library 'lname'. The
  1046. compiler will the automatically link this library to your program.
  1047. \item
  1048. \begin{verbatim}
  1049. external 'lname' name Fname;
  1050. \end{verbatim}
  1051. Tells the compiler that the function resides in library 'lname', but with
  1052. name 'Fname'. The compiler will the automatically link this library to your
  1053. program, and use the correct name for the function.
  1054. \item \windows and \ostwo only:
  1055. \begin{verbatim}
  1056. external 'lname' Index Ind;
  1057. \end{verbatim}
  1058. Tells the compiler that the function resides in library 'lname', but with
  1059. indexname \var{Ind}. The compiler will the automatically link this library to your
  1060. program, and use the correct index for the function.
  1061. \end{enumerate}
  1062. \subsection{Export}
  1063. Sometimes you must provide a callback function for a C library, or you want
  1064. your routines to be callable from a C program. Since \fpc and C use
  1065. different calling schemes for functions and procedures\footnote{More
  1066. techically: In C the calling procedure must clear the stack. In \fpc, the
  1067. subroutine clears the stack.}, the compiler must be told to generate code
  1068. that can be called from a C routine. This is where the \var{Export} modifier
  1069. comes in. Contrary to the other modifiers, it must be specified separately,
  1070. as follows:
  1071. \begin{verbatim}
  1072. function DoSquare (X : longint) : longint; export;
  1073. begin
  1074. ...
  1075. end;
  1076. \end{verbatim}
  1077. The square brackets around the modifier are not allowed in this case.
  1078. {\em Remark:} You cannot call an exported function from within \fpc programs.
  1079. If you try to do so, the compiler will complain when compiling your source
  1080. code.
  1081. If you do want to call an exported procedure from pascal, you must use a
  1082. dummy function:
  1083. \begin{verbatim}
  1084. Procedure RealDoSomething;
  1085. begin
  1086. ...
  1087. end;
  1088. Procedure DoSomething; export;
  1089. begin
  1090. RealDoSomething;
  1091. end;
  1092. \end{verbatim}
  1093. In this example, from your \fpc code, you can call the \var{RealDoSomething}
  1094. procedure. If someone wants to link to your code from a C program, he can
  1095. call the \var{DoSomething} procedure. Both calls will have the same effect.
  1096. {\em Remark:}
  1097. as of version 0.9.8, \fpc supports the Delphi \var{cdecl} modifier.
  1098. This modifier works in the same way as the \var{export} modifier.
  1099. More information about these modifiers can be found in the \progref, in the
  1100. section on the calling mechanism and the chapter on linking.
  1101. \subsection{StdCall}
  1102. As of version 0.9.8, \fpc supports the Delphi \var{stdcall} modifier.
  1103. This modifier does actually nothing, since the \fpc compiler by default
  1104. pushes parameters from right to left on the stack, which is what the
  1105. modifier does under Delphi (which pushes parameters on the stack from left to
  1106. right).
  1107. More information about this modifier can be found in the \progref, in the
  1108. section on the calling mechanism and the chapter on linking.
  1109. \subsection{Alias}
  1110. The \var{Alias} modifier allows you to specify a different name for a
  1111. procedure or function. This is mostly useful for referring to this procedure
  1112. from assembly language constructs. As an example, consider the following
  1113. program:
  1114. \begin{CodEx}
  1115. \begin{verbatim}
  1116. Program Aliases;
  1117. Procedure Printit; [Alias : 'DOIT'];
  1118. begin
  1119. Writeln ('In Printit (alias : "DOIT")');
  1120. end;
  1121. begin
  1122. asm
  1123. call DOIT
  1124. end;
  1125. end.
  1126. \end{verbatim}
  1127. \end{CodEx}
  1128. {\rm Remark:} the specified alias is inserted straight into the assembly
  1129. code, thus it is case sensitive.
  1130. The \var{Alias} modifier, combined with the \var{Public} modifier, make a
  1131. powerful tool for making externally accessible object files.
  1132. \subsection{[RegisterList]}
  1133. This modifier list is used to indicate the registers that are modified by an
  1134. assembler block in your code. The compiler stores certain results in the
  1135. registers. If you modify theregisters in an assembly block, the compiler
  1136. should, sometimes, be told about it.
  1137. The prototype syntax of the \var{Registerlist} modifier is:
  1138. \begin{verbatim}
  1139. asm
  1140. statements
  1141. end; ['register1','register2',...,'registern'];
  1142. \end{verbatim}
  1143. Where \var{'register'} is one of \var{'EAX',EBX',ECX','EDX'} etc.
  1144. \subsection{Unsupported Turbo Pascal modifiers}
  1145. The modifiers that exist in Turbo pascal, but aren't supported by \fpc, are
  1146. listed in \seet{Modifs}.
  1147. \begin{FPCltable}{lr}{Unsupported modifiers}{Modifs}
  1148. Modifier & Why not supported ? \\ \hline
  1149. Near & \fpc is a 32-bit compiler.\\
  1150. Far & \fpc is a 32-bit compiler. \\
  1151. External & Replaced by \var{C} modifier. \\ \hline
  1152. \end{FPCltable}
  1153. %
  1154. % System unit reference guide.
  1155. %
  1156. \chapter{Reference : The system unit}
  1157. The system unit contains the standard supported functions of \fpc. It is the
  1158. same for all platforms. Basically it is the same as the system unit provided
  1159. with Borland or Turbo Pascal.
  1160. Functions are listed in alphabetical order.
  1161. Arguments to functions or procedures that are optional are put between
  1162. square brackets.
  1163. The pre-defined constants and variables are listed in the first section. The
  1164. second section contains the supported functions and procedures.
  1165. \section{Types, Constants and Variables}
  1166. \subsection{Types}
  1167. The following integer types are defined in the System unit:
  1168. \begin{verbatim}
  1169. shortint = -128..127;
  1170. longint = $80000000..$7fffffff;
  1171. integer = -32768..32767;
  1172. byte = 0..255;
  1173. word = 0..65535;
  1174. \end{verbatim}
  1175. The following Real types are declared:
  1176. \begin{verbatim}
  1177. double = real;
  1178. {$ifdef VER0_6}
  1179. extended = real;
  1180. single = real;
  1181. {$endif VER0_6}
  1182. \end{verbatim}
  1183. And the following pointer types:
  1184. \begin{verbatim}
  1185. pchar = ^char;
  1186. ppchar = ^pchar;
  1187. \end{verbatim}
  1188. \subsection{Constants}
  1189. The following constants for file-handling are defined in the system unit:
  1190. \begin{verbatim}
  1191. Const
  1192. fmclosed = $D7B0;
  1193. fminput = $D7B1;
  1194. fmoutput = $D7B2;
  1195. fminout = $D7B3;
  1196. fmappend = $D7B4;
  1197. filemode : byte = 2;
  1198. \end{verbatim}
  1199. Further, the following general-purpose constants are also defined:
  1200. \begin{verbatim}
  1201. const
  1202. test8086 : byte = 2; { always i386 or newer }
  1203. test8087 : byte = 3; { Always 387 or higher. emulated if needed. }
  1204. erroraddr : pointer = nil;
  1205. errorcode : word = 0;
  1206. { max level in dumping on error }
  1207. max_frame_dump : word = 20;
  1208. \end{verbatim}
  1209. \subsection{Variables}
  1210. The following variables are defined and initialized in the system unit:
  1211. \begin{verbatim}
  1212. var
  1213. output,input,stderr : text;
  1214. exitproc : pointer;
  1215. exitcode : word;
  1216. stackbottom : longint;
  1217. loweststack : longint;
  1218. \end{verbatim}
  1219. The variables \var{ExitProc}, \var{exitcode} are used in the \fpc exit
  1220. scheme. It works similarly to the on in Turbo Pascal:
  1221. When a program halts (be it through the call of the \var{Halt} function or
  1222. \var{Exit} or through a run-time error), the exit mechanism checks the value
  1223. of \var{ExitProc}. If this one is non-\var{Nil}, it is set to \var{Nil}, and
  1224. the procedure is called. If the exit procedure exits, the value of ExitProc
  1225. is checked again. If it is non-\var{Nil} then the above steps are repeated.
  1226. So if you want to install your exit procedure, you should save the old value
  1227. of \var{ExitProc} (may be non-\var{Nil}, since other units could have set it before
  1228. you did). In your exit procedure you then restore the value of
  1229. \var{ExitProc}, such that if it was non-\var{Nil} the exit-procedure can be
  1230. called.
  1231. The \var{ErrorAddr} and \var{ExitCode} can be used to check for
  1232. error-conditions. If \var{ErrorAddr} is non-\var{Nil}, a run-time error has
  1233. occurred. If so, \var{ExitCode} contains the error code. If \var{ErrorAddr} is
  1234. \var{Nil}, then {ExitCode} contains the argument to \var{Halt} or 0 if the
  1235. program terminated normally.
  1236. \var{ExitCode} is always passed to the operating system as the exit-code of
  1237. your process.
  1238. Under \file{GO32}, the following constants are also defined :
  1239. \begin{verbatim}
  1240. const
  1241. seg0040 = $0040;
  1242. segA000 = $A000;
  1243. segB000 = $B000;
  1244. segB800 = $B800;
  1245. \end{verbatim}
  1246. These constants allow easy access to the bios/screen segment via mem/absolute.
  1247. \section{Functions and Procedures}
  1248. \function{Abs}{(X : Every numerical type)}{Every numerical type}
  1249. {\var{Abs} returns the absolute value of a variable. The result of the
  1250. function has the same type as its argument, which can be any numerical
  1251. type.}
  1252. {None.}
  1253. {\seef{Round}}
  1254. \input{refex/ex1.tex}
  1255. \function{Addr}{(X : Any type)}{Pointer}
  1256. {\var{Addr} returns a pointer to its argument, which can be any type, or a
  1257. function or procedure name. The returned pointer isn't typed.
  1258. The same result can be obtained by the \var{@} operator, which can return a
  1259. typed pointer (\progref). }
  1260. {None}
  1261. {\seef{SizeOf}}
  1262. \input{refex/ex2.tex}
  1263. \procedure{Append}{(Var F : Text)}
  1264. {\var{Append} opens an existing file in append mode. Any data written to
  1265. \var{F} will be appended to the file. If the file didn't exist, it will be
  1266. created, contrary to the Turbo Pascal implementation of \var{Append}, where
  1267. a file needed to exist in order to be opened by
  1268. append.
  1269. Only text files can be opened in append mode.
  1270. }
  1271. {If the file can't be created, a run-time error will be generated.}
  1272. {\seep{Rewrite},\seep{Append}, \seep{Reset}}
  1273. \input{refex/ex3.tex}
  1274. \function{Arctan}{(X : Real)}{Real}
  1275. {\var{Arctan} returns the Arctangent of \var{X}, which can be any real type.
  1276. The resulting angle is in radial units.}{None}{\seef{Sin}, \seef{Cos}}
  1277. \input{refex/ex4.tex}
  1278. \procedure{Assign}{(Var F; Name : String)}
  1279. {\var{Assign} assigns a name to \var{F}, which can be any file type.
  1280. This call doesn't open the file, it just assigns a name to a file variable,
  1281. and marks the file as closed.}
  1282. {None.}
  1283. {\seep{Reset}, \seep{Rewrite}, \seep{Append}}
  1284. \input{refex/ex5.tex}
  1285. \procedure{Blockread}{(Var F : File; Var Buffer; Var Count : Longint [; var
  1286. Result : Longint])}
  1287. {\var{Blockread} reads \var{count} or less records from file \var{F}. The
  1288. result is placed in \var{Buffer}, which must contain enough room for
  1289. \var{Count} records. The function cannot read partial records.
  1290. If \var{Result} is specified, it contains the number of records actually
  1291. read. If \var{Result} isn't specified, and less than \var{Count} records were
  1292. read, a run-time error is generated. This behavior can be controlled by the
  1293. \var{\{\$i\}} switch. }
  1294. {If \var{Result} isn't specified, then a run-time error is generated if less
  1295. than \var{count} records were read.}
  1296. {\seep{Blockwrite},\seep{Reset}, \seep{Assign}}
  1297. \input{refex/ex6.tex}
  1298. \procedure{Blockwrite}{(Var F : File; Var Buffer; Var Count : Longint)}
  1299. {\var{Blockread} writes \var{count} records from \var{buffer} to the file
  1300. \var{F}.
  1301. If the records couldn't be written to disk, a run-time error is generated.
  1302. This behavior can be controlled by the \var{\{\$i\}} switch.
  1303. }
  1304. {A run-time error is generated if, for some reason, the records couldn't be
  1305. written to disk.}
  1306. {\seep{Blockread},\seep{Reset}, \seep{Assign}}
  1307. For the example, see \seep{Blockread}.
  1308. \procedure{Chdir}{(const S : string)}
  1309. {\var{Chdir} changes the working directory of the process to \var{S}.}
  1310. {If the directory \var{S} doesn't exist, a run-time error is generated.}
  1311. {\seep{Mkdir}, \seep{Rmdir}}
  1312. \input{refex/ex7.tex}
  1313. \function{Chr}{(X : byte)}{Char}
  1314. {\var{Chr} returns the character which has ASCII value \var{X}.}
  1315. {None.}
  1316. {\seef{Ord},\seep{Str}}
  1317. \input{refex/ex8.tex}
  1318. \procedure{Close}{(Var F : Anyfiletype)}
  1319. {\var{Close} flushes the buffer of the file \var{F} and closes \var{F}.
  1320. After a call to \var{Close}, data can no longer be read from or written to
  1321. \var{F}.
  1322. To reopen a file closed with \var{Close}, it isn't necessary to assign the
  1323. file again. A call to \seep{Reset} or \seep{Rewrite} is sufficient.}
  1324. {None.}{\seep{Assign}, \seep{Reset}, \seep{Rewrite}}
  1325. \input{refex/ex9.tex}
  1326. \function{Concat}{(S1,S2 [,S3, ... ,Sn])}{String}
  1327. {\var{Concat} concatenates the strings \var{S1},\var{S2} etc. to one long
  1328. string. The resulting string is truncated at a length of 255 bytes.
  1329. The same operation can be performed with the \var{+} operation.}
  1330. {None.}
  1331. {\seef{Copy}, \seep{Delete}, \seep{Insert}, \seef{Pos}, \seef{Length}}
  1332. \input{refex/ex10.tex}
  1333. \function{Copy}{(Const S : String;Index : Integer;Count : Byte)}{String}
  1334. {\var{Copy} returns a string which is a copy if the \var{Count} characters
  1335. in \var{S}, starting at position \var{Index}. If \var{Count} is larger than
  1336. the length of the string \var{S}, the result is truncated.
  1337. If \var{Index} is larger than the length of the string \var{S}, then an
  1338. empty string is returned.}
  1339. {None.}
  1340. {\seep{Delete}, \seep{Insert}, \seef{Pos}}
  1341. \input{refex/ex11.tex}
  1342. \function{Cos}{(X : real)}{Real}
  1343. {\var{Cos} returns the cosine of \var{X}, where X is an angle, in radians.}
  1344. {None.}
  1345. {\seef{Arctan}, \seef{Sin}}
  1346. \input{refex/ex12.tex}
  1347. \Function{CSeg}{Word}
  1348. {\var{CSeg} returns the Code segment register. In \fpc, it returns always a
  1349. zero, since \fpc is a 32 bit compiler.}
  1350. {None.}
  1351. {\seef{DSeg}, \seef{Seg}, \seef{Ofs}, \seef{Ptr}}
  1352. \input{refex/ex13.tex}
  1353. \procedure{Dec}{(Var X : Any ordinal type[; Decrement : Longint])}
  1354. {\var{Dec} decreases the value of \var{X} with \var{Decrement}.
  1355. If \var{Decrement} isn't specified, then 1 is taken as a default.}
  1356. {A range check can occur, or an underflow error, if you try to decrease \var{X}
  1357. below its minimum value.}
  1358. {\seep{Inc}}
  1359. \input{refex/ex14.tex}
  1360. \procedure{Delete}{(var S : string;Index : Integer;Count : Integer)}
  1361. {\var{Delete} removes \var{Count} characters from string \var{S}, starting
  1362. at position \var{Index}. All remaining characters are shifted \var{Count}
  1363. positions to the left, and the length of the string is adjusted.
  1364. }
  1365. {None.}
  1366. {\seef{Copy},\seef{Pos},\seep{Insert}}
  1367. \input{refex/ex15.tex}
  1368. \procedure{Dispose}{(P : pointer)}
  1369. {\var{Dispose} releases the memory allocated with a call to \seep{New}.
  1370. The pointer \var{P} must be typed. The released memory is returned to the
  1371. heap.}
  1372. {An error will occur if the pointer doesn't point to a location in the
  1373. heap.}
  1374. {\seep{New}, \seep{Getmem}, \seep{Freemem}}
  1375. \input{refex/ex16.tex}
  1376. \Function{DSeg}{Word}
  1377. {\var{DSeg} returns the data segment register. In \fpc, it returns always a
  1378. zero, since \fpc is a 32 bit compiler.}
  1379. {None.}
  1380. {\seef{CSeg}, \seef{Seg}, \seef{Ofs}, \seef{Ptr}}
  1381. \input{refex/ex17.tex}
  1382. \function{Eof}{[(F : Any file type)]}{Boolean}
  1383. {\var{Eof} returns \var{True} if the file-pointer has reached the end of the
  1384. file, or if the file is empty. In all other cases \var{Eof} returns
  1385. \var{False}.
  1386. If no file \var{F} is specified, standard input is assumed.}
  1387. {None.}
  1388. {\seef{Eoln}, \seep{Assign}, \seep{Reset}, \seep{Rewrite}}
  1389. \input{refex/ex18.tex}
  1390. \function{Eoln}{[(F : Text)]}{Boolean}
  1391. {\var{Eof} returns \var{True} if the file pointer has reached the end of a
  1392. line, which is demarcated by a line-feed character (ASCII value 10), or if
  1393. the end of the file is reached.
  1394. In all other cases \var{Eof} returns \var{False}.
  1395. If no file \var{F} is specified, standard input is assumed.
  1396. It can only be used on files of type \var{Text}.}
  1397. {None.}
  1398. {\seef{Eof}, \seep{Assign}, \seep{Reset}, \seep{Rewrite}}
  1399. \input{refex/ex19.tex}
  1400. \procedure{Erase}{(Var F : Any file type)}
  1401. {\var{Erase} removes an unopened file from disk. The file should be
  1402. assigned with \var{Assign}, but not opened with \var{Reset} or \var{Rewrite}}
  1403. {A run-time error will be generated if the specified file doesn't exist.}
  1404. {\seep{Assign}}
  1405. \input{refex/ex20.tex}
  1406. \procedure{Exit}{([Var X : return type )]}
  1407. {\var{Exit} exits the current subroutine, and returns control to the calling
  1408. routine. If invoked in the main program routine, exit stops the program.
  1409. The optional argument \var{X} allows to specify a return value, in the case
  1410. \var{Exit} is invoked in a function. The function result will then be
  1411. equal to \var{X}.}
  1412. {None.}
  1413. {\seep{Halt}}
  1414. \input{refex/ex21.tex}
  1415. \function{Exp}{(Var X : real)}{Real}
  1416. {\var{Exp} returns the exponent of \var{X}, i.e. the number \var{e} to the
  1417. power \var{X}.}
  1418. {None.}{\seef{Ln}, \seef{Power}}
  1419. \input{refex/ex22.tex}
  1420. \function{Filepos}{(Var F : Any file type)}{Longint}
  1421. {\var{Filepos} returns the current record position of the file-pointer in file
  1422. \var{F}. It cannot be invoked with a file of type \var{Text}.}
  1423. {None.}
  1424. {\seef{Filesize}}
  1425. \input{refex/ex23.tex}
  1426. \function{Filesize}{(Var F : Any file type)}{Longint}
  1427. {\var{Filepos} returns the total number of records in file \var{F}.
  1428. It cannot be invoked with a file of type \var{Text}. (under \linux, this
  1429. also means that it cannot be invoked on pipes.)
  1430. If \var{F} is empty, 0 is returned.
  1431. }
  1432. {None.}
  1433. {\seef{Filepos}}
  1434. \input{refex/ex24.tex}
  1435. \procedure{Fillchar}{(Var X;Count : Longint;Value : char or byte);}
  1436. {\var{Fillchar} fills the memory starting at \var{X} with \var{Count} bytes
  1437. or characters with value equal to \var{Value}.
  1438. }
  1439. {No checking on the size of \var{X} is done.}
  1440. {\seep{Fillword}, \seep{Move}}
  1441. \input{refex/ex25.tex}
  1442. \procedure{Fillword}{(Var X;Count : Longint;Value : Word);}
  1443. {\var{Fillword} fills the memory starting at \var{X} with \var{Count} words
  1444. with value equal to \var{Value}.
  1445. }
  1446. {No checking on the size of \var{X} is done.}
  1447. {\seep{Fillword}, \seep{Move}}
  1448. \input{refex/ex76.tex}
  1449. \procedure{Flush}{(Var F : Text)}
  1450. {\var{Flush} empties the internal buffer of file \var{F} and writes the
  1451. contents to disk. The file is \textit{not} closed as a result of this call.}
  1452. {If the disk is full, a run-time error will be generated.}
  1453. {\seep{Close}}
  1454. \input{refex/ex26.tex}
  1455. \function{Frac}{(X : real)}{Real}
  1456. {\var{Frac} returns the non-integer part of \var{X}.}
  1457. {None.}
  1458. {\seef{Round}, \seef{Int}}
  1459. \input{refex/ex27.tex}
  1460. \procedure{Freemem}{(Var P : pointer; Count : longint)}
  1461. {\var{Freemem} releases the memory occupied by the pointer \var{P}, of size
  1462. \var{Count}, and returns it to the heap. \var{P} should point to the memory
  1463. allocated to a dynamical variable.}
  1464. {An error will occur when \var{P} doesn't point to the heap.}
  1465. {\seep{Getmem}, \seep{New}, \seep{Dispose}}
  1466. \input{refex/ex28.tex}
  1467. \procedure{Getdir}{(drivenr : byte;var dir : string)}
  1468. {\var{Getdir} returns in \var{dir} the current directory on the drive
  1469. \var{drivenr}, where {drivenr} is 1 for the first floppy drive, 3 for the
  1470. first hard disk etc. A value of 0 returns the directory on the current disk.
  1471. On \linux, \var{drivenr} is ignored, as there is only one directory tree.}
  1472. {An error is returned under \dos, if the drive requested isn't ready.}
  1473. {\seep{Chdir}}
  1474. \input{refex/ex29.tex}
  1475. \procedure{Getmem}{(var p : pointer;size : longint)}
  1476. {\var{Getmem} reserves \var{Size} bytes memory on the heap, and returns a
  1477. pointer to this memory in \var{p}. If no more memory is available, nil is
  1478. returned.}
  1479. {None.}
  1480. {\seep{Freemem}, \seep{Dispose}, \seep{New}}
  1481. For an example, see \seep{Freemem}.
  1482. \procedure{Halt}{[(Errnum : byte]}
  1483. {\var{Halt} stops program execution and returns control to the calling
  1484. program. The optional argument \var{Errnum} specifies an exit value. If
  1485. omitted, zero is returned.}
  1486. {None.}
  1487. {\seep{Exit}}
  1488. \input{refex/ex30.tex}
  1489. \function{Hi}{(X : Ordinal type)}{Word or byte}
  1490. {\var{Hi} returns the high byte or word from \var{X}, depending on the size
  1491. of X. If the size of X is 4, then the high word is returned. If the size is
  1492. 2 then the high byte is retuned.
  1493. \var{hi} cannot be invoked on types of size 1, such as byte or char.}
  1494. {None}
  1495. {\seef{Lo}}
  1496. \input{refex/ex31.tex}
  1497. \procedure{Inc}{(Var X : Any ordinal type[; Increment : Longint])}
  1498. {\var{Inc} increases the value of \var{X} with \var{Increment}.
  1499. If \var{Increment} isn't specified, then 1 is taken as a default.}
  1500. {A range check can occur, or an overflow error, if you try to increase \var{X}
  1501. over its maximum value.}
  1502. {\seep{Dec}}
  1503. \input{refex/ex32.tex}
  1504. \procedure{Insert}{(Var Source : String;var S : String;Index : integer)}
  1505. {\var{Insert} inserts string \var{S} in string \var{Source}, at position
  1506. \var{Index}, shifting all characters after \var{Index} to the right. The
  1507. resulting string is truncated at 255 characters, if needed.}
  1508. {None.}
  1509. {\seep{Delete}, \seef{Copy}, \seef{Pos}}
  1510. \input{refex/ex33.tex}
  1511. \function{Int}{(X : real)}{Real}
  1512. {\var{Int} returns the integer part of any real \var{X}, as a real.}
  1513. {None.}
  1514. {\seef{Frac}, \seef{Round}}
  1515. \input{refex/ex34.tex}
  1516. \Function{IOresult}{Word}
  1517. {IOresult contains the result of any input/output call, when the
  1518. \var{\{\$i-\}} compiler directive is active, and IO checking is disabled. When the
  1519. flag is read, it is reset to zero.
  1520. If \var{IOresult} is zero, the operation completed successfully. If
  1521. non-zero, an error occurred. The following errors can occur:
  1522. \dos errors :
  1523. \begin{description}
  1524. \item [2\ ] File not found.
  1525. \item [3\ ] Path not found.
  1526. \item [4\ ] Too many open files.
  1527. \item [5\ ] Access denied.
  1528. \item [6\ ] Invalid file handle.
  1529. \item [12\ ] Invalid file-access mode.
  1530. \item [15\ ] Invalid disk number.
  1531. \item [16\ ] Cannot remove current directory.
  1532. \item [17\ ] Cannot rename across volumes.
  1533. \end{description}
  1534. I/O errors :
  1535. \begin{description}
  1536. \item [100\ ] Error when reading from disk.
  1537. \item [101\ ] Error when writing to disk.
  1538. \item [102\ ] File not assigned.
  1539. \item [103\ ] File not open.
  1540. \item [104\ ] File not opened for input.
  1541. \item [105\ ] File not opened for output.
  1542. \item [106\ ] Invalid number.
  1543. \end{description}
  1544. Fatal errors :
  1545. \begin{description}
  1546. \item [150\ ] Disk is write protected.
  1547. \item [151\ ] Unknown device.
  1548. \item [152\ ] Drive not ready.
  1549. \item [153\ ] Unknown command.
  1550. \item [154\ ] CRC check failed.
  1551. \item [155\ ] Invalid drive specified..
  1552. \item [156\ ] Seek error on disk.
  1553. \item [157\ ] Invalid media type.
  1554. \item [158\ ] Sector not found.
  1555. \item [159\ ] Printer out of paper.
  1556. \item [160\ ] Error when writing to device.
  1557. \item [161\ ] Error when reading from device.
  1558. \item [162\ ] Hardware failure.
  1559. \end{description}
  1560. }
  1561. {None.}
  1562. {All I/O functions.}
  1563. \input{refex/ex35.tex}
  1564. \function{Length}{(S : String)}{Byte}
  1565. {\var{Length} returns the length of the string \var{S},
  1566. which is limited to 255. If the strings \var{S} is empty, 0 is returned.
  1567. {\em Note:} The length of the string \var{S} is stored in \var{S[0]}.
  1568. }
  1569. {None.}
  1570. {\seef{Pos}}
  1571. \input{refex/ex36.tex}
  1572. \function{Ln}{(X : real)}{Real}
  1573. {
  1574. \var{Ln} returns the natural logarithm of the real parameter \var{X}.
  1575. \var{X} must be positive.
  1576. }
  1577. {An run-time error will occur when \var{X} is negative.}
  1578. {\seef{Exp}, \seef{Power}}
  1579. \input{refex/ex37.tex}
  1580. \function{Lo}{(O : Word or Longint)}{Byte or Word}
  1581. {\var{Lo} returns the low byte of its argument if this is of type
  1582. \var{Integer} or
  1583. \var{Word}. It returns the low word of its argument if this is of type
  1584. \var{Longint} or \var{Cardinal}.}
  1585. {None.}
  1586. {\seef{Ord}, \seef{Chr}}
  1587. \input{refex/ex38.tex}
  1588. \function{Lowercase}{(C : Char or String)}{Char or String}
  1589. {\var{Lowercase} returns the lowercase version of its argument \var{C}.
  1590. If its argument is a string, then the complete string is converted to
  1591. lowercase. The type of the returned value is the same as the type of the
  1592. argument.}
  1593. {None.}
  1594. {\seef{Upcase}}
  1595. \input{refex/ex73.tex}
  1596. \procedure{Mark}{(Var P : Pointer)}
  1597. {\var{Mark} copies the current heap-pointer to \var{P}.}
  1598. {None.}
  1599. {\seep{Getmem}, \seep{Freemem}, \seep{New}, \seep{Dispose}, \seef{Maxavail}}
  1600. \input{refex/ex39.tex}
  1601. \Function{Maxavail}{Longint}
  1602. {\var{Maxavail} returns the size, in bytes, of the biggest free memory block in
  1603. the heap.
  1604. {\em Remark:} The heap grows dynamically if more memory is needed than is
  1605. available.}
  1606. {None.}
  1607. {\seep{Release}, \seef{Memavail},\seep{Freemem}, \seep{Getmem}}
  1608. \input{refex/ex40.tex}
  1609. \Function{Memavail}{Longint}
  1610. {\var{Memavail} returns the size, in bytes, of the free heap memory.
  1611. {\em Remark:} The heap grows dynamically if more memory is needed than is
  1612. available.}
  1613. {None.}
  1614. {\seef{Maxavail},\seep{Freemem}, \seep{Getmem}}
  1615. \input{refex/ex41.tex}
  1616. \procedure{Mkdir}{(const S : string)}
  1617. {\var{Chdir} creates a new directory \var{S}.}
  1618. {If a parent-directory of directory \var{S} doesn't exist, a run-time error is generated.}
  1619. {\seep{Chdir}, \seep{Rmdir}}
  1620. For an example, see \seep{Rmdir}.
  1621. \procedure{Move}{(var Source,Dest;Count : longint)}
  1622. {\var{Move} moves \var{Count} bytes from \var{Source} to \var{Dest}.}
  1623. {If either \var{Dest} or \var{Source} is outside the accessible memory for
  1624. the process, then a run-time error will be generated. With older versions of
  1625. the compiler, a segmentation-fault will occur. }
  1626. {\seep{Fillword}, \seep{Fillchar}}
  1627. \input{refex/ex42.tex}
  1628. \procedure{New}{(Var P : Pointer[, Constructor])}
  1629. {\var{New} allocates a new instance of the type pointed to by \var{P}, and
  1630. puts the address in \var{P}.
  1631. If P is an object, then it is possible to
  1632. specify the name of the constructor with which the instance will be created.}
  1633. {If not enough memory is available, \var{Nil} will be returned.}
  1634. {\seep{Dispose}, \seep{Freemem}, \seep{Getmem}, \seef{Memavail},
  1635. \seef{Maxavail}}
  1636. For an example, see \seep{Dispose}.
  1637. \function{Odd}{(X : longint)}{Boolean}
  1638. {\var{Odd} returns \var{True} if \var{X} is odd, or \var{False} otherwise.}
  1639. {None.}
  1640. {\seef{Abs}, \seef{Ord}}
  1641. \input{refex/ex43.tex}
  1642. \function{Ofs}{Var X}{Longint}
  1643. {\var{Ofs} returns the offset of the address of a variable.
  1644. This function is only supported for compatibility. In \fpc, it
  1645. returns always the complete address of the variable, since \fpc is a 32 bit
  1646. compiler.
  1647. }
  1648. {None.}
  1649. {\seef{DSeg}, \seef{CSeg}, \seef{Seg}, \seef{Ptr}}
  1650. \input{refex/ex44.tex}
  1651. \function{Ord}{(X : Ordinal type)}{Byte}
  1652. {\var{Ord} returns the Ordinal value of a ordinal-type variable \var{X}.}
  1653. {None.}
  1654. {\seef{Chr}}
  1655. \input{refex/ex45.tex}
  1656. \Function{Paramcount}{Longint}
  1657. {\var{Paramcount} returns the number of command-line arguments. If no
  1658. arguments were given to the running program, \var{0} is returned.
  1659. }
  1660. {None.}
  1661. {\seef{Paramstr}}
  1662. \input{refex/ex46.tex}
  1663. \function{Paramstr}{(L : Longint)}{String}
  1664. {\var{Paramstr} returns the \var{L}-th command-line argument. \var{L} must
  1665. be between \var{0} and \var{Paramcount}, these values included.
  1666. The zeroth argument is the name with which the program was started.
  1667. }
  1668. {Under Linux, command-line arguments may be longer than 255 characters. In
  1669. that case, the string is truncated. If you want to access the complete
  1670. string, you must use the \var{argv} pointer to access the real values of the
  1671. command-line parameters.}
  1672. {\seef{Paramcount}}
  1673. For an example, see \seef{Paramcount}.
  1674. \Function{Pi}{Real}
  1675. {\var{Pi} returns the value of Pi (3.1415926535897932385).}
  1676. {None.}
  1677. {\seef{Cos}, \seef{Sin}}
  1678. \input{refex/ex47.tex}
  1679. \function{Pos}{(Const Substr : String;Const S : String)}{Byte}
  1680. {\var{Pos} returns the index of \var{Substr} in \var{S}, if \var{S} contains
  1681. \var{Substr}. In case \var{Substr} isn't found, \var{0} is returned.
  1682. The search is case-sensitive.
  1683. }
  1684. {None}
  1685. {\seef{Length}, \seef{Copy}, \seep{Delete}, \seep{Insert}}
  1686. \input{refex/ex48.tex}
  1687. \function{Power}{(base,expon : Real)}{Real}
  1688. {
  1689. \var{Power} returns the value of \var{base} to the power \var{expon}.
  1690. \var{Base} and \var{expon} can be of type longint, in which case the
  1691. result will also be a longint.
  1692. The function actually returns \var{Exp(expon*Ln(base))}
  1693. }{None.}{\seef{Exp}, \seef{Ln}}
  1694. \input{refex/ex78.tex}
  1695. \function{Ptr}{(Sel,Off : Longint)}{Pointer}
  1696. {
  1697. \var{Ptr} returns a pointer, pointing to the address specified by
  1698. segment \var{Sel} and offset \var{Off}.
  1699. {\em Remark 1:} In the 32-bit flat-memory model supported by \fpc, this
  1700. function is obsolete.
  1701. {\em Remark 2:} The returned address is simply the offset. If you recompile
  1702. the RTL with \var{-dDoMapping} defined, then the compiler returns the
  1703. following : \var{ptr:=pointer(\$e0000000+sel shl 4+off)} under \dos, or
  1704. \var{ptr:=pointer(sel shl 4+off)} on other OSes.
  1705. }
  1706. {None.}
  1707. {\seef{Addr}}
  1708. \input{refex/ex59.tex}
  1709. \function{Random}{[(L : longint)]}{Longint or Real}
  1710. {\var{Random} returns a random number larger or equal to \var{0} and
  1711. strictly less than \var{L}.
  1712. If the argument \var{L} is omitted, a real number between 0 and 1 is returned.
  1713. (0 included, 1 excluded)}
  1714. {None.}
  1715. {\seep{Randomize}}
  1716. \input{refex/ex49.tex}
  1717. \Procedure{Randomize}
  1718. {\var{Randomize} initializes the random number generator of \fpc, by giving
  1719. a value to \var{Randseed}, calculated with the system clock.
  1720. }
  1721. {None.}
  1722. {\seef{Random}}
  1723. For an example, see \seef{Random}.
  1724. \procedure{Read}{([Var F : Any file type], V1 [, V2, ... , Vn])}
  1725. {\var{Read} reads one or more values from a file \var{F}, and stores the
  1726. result in \var{V1}, \var{V2}, etc.; If no file \var{F} is specified, then
  1727. standard input is read.
  1728. If \var{F} is of type \var{Text}, then the variables \var{V1, V2} etc. must be
  1729. of type \var{Char}, \var{Integer}, \var{Real} or \var{String}.
  1730. If \var{F} is a typed file, then each of the variables must be of the type
  1731. specified in the declaration of \var{F}. Untyped files are not allowed as an
  1732. argument.}
  1733. {If no data is available, a run-time error is generated. This behavior can
  1734. be controlled with the \var{\{\$i\}} compiler switch.}
  1735. {\seep{Readln}, \seep{Blockread}, \seep{Write}, \seep{Blockwrite}}
  1736. \input{refex/ex50.tex}
  1737. \procedure{Readln}{[Var F : Text], V1 [, V2, ... , Vn])}
  1738. {\var{Read} reads one or more values from a file \var{F}, and stores the
  1739. result in \var{V1}, \var{V2}, etc. After that it goes to the next line in
  1740. the file (defined by the \var{LineFeed (\#10)} character).
  1741. If no file \var{F} is specified, then standard input is read.
  1742. The variables \var{V1, V2} etc. must be of type \var{Char}, \var{Integer},
  1743. \var{Real}, \var{String} or \var{PChar}.
  1744. }
  1745. {If no data is available, a run-time error is generated. This behavior can
  1746. be controlled with the \var{\{\$i\}} compiler switch.}
  1747. {\seep{Read}, \seep{Blockread}, \seep{Write}, \seep{Blockwrite}}
  1748. For an example, see \seep{Read}.
  1749. \procedure{Release}{(Var P : pointer)}
  1750. {\var{Release} sets the top of the Heap to the location pointed to by
  1751. \var{P}. All memory at a location higher than \var{P} is marked empty.}
  1752. {A run-time error will be generated if \var{P} points to memory outside the
  1753. heap.}
  1754. {\seep{Mark}, \seef{Memavail}, \seef{Maxavail}, \seep{Getmem}, \seep{Freemem}
  1755. \seep{New}, \seep{Dispose}}
  1756. For an example, see \seep{Mark}.
  1757. \procedure{Rename}{(Var F : Any Filetype; Const S : String)}
  1758. {\var{Rename} changes the name of the assigned file \var{F} to \var{S}.
  1759. \var{F}
  1760. must be assigned, but not opened.}
  1761. {A run-time error will be generated if \var{F} isn't assigned,
  1762. or doesn't exist.}
  1763. {\seep{Erase}}
  1764. \input{refex/ex77.tex}
  1765. \procedure{Reset}{(Var F : Any File Type[; L : longint])}
  1766. {\var{Reset} opens a file \var{F} for reading. \var{F} can be any file type.
  1767. If \var{F} is an untyped or typed file, then it is opened for reading and
  1768. writing. If \var{F} is an untyped file, the record size can be specified in
  1769. the optional parameter \var{L}. Default a value of 128 is used.}
  1770. {If the file cannot be opened for reading, then a run-time error is
  1771. generated. This behavior can be changed by the \var{\{\$i\} } compiler switch.}
  1772. {\seep{Rewrite}, \seep{Assign}, \seep{Close}}
  1773. \input{refex/ex51.tex}
  1774. \procedure{Rewrite}{(Var F : Any File Type[; L : longint])}
  1775. {\var{Rewrite} opens a file \var{F} for writing. \var{F} can be any file type.
  1776. If \var{F} is an untyped or typed file, then it is opened for reading and
  1777. writing. If \var{F} is an untyped file, the record size can be specified in
  1778. the optional parameter \var{L}. Default a value of 128 is used.
  1779. if \var{Rewrite} finds a file with the same name as \var{F}, this file is
  1780. truncated to length \var{0}. If it doesn't find such a file, a new file is
  1781. created.
  1782. }
  1783. {If the file cannot be opened for writing, then a run-time error is
  1784. generated. This behavior can be changed by the \var{\{\$i\} } compiler switch.}
  1785. {\seep{Reset}, \seep{Assign}, \seep{Close}}
  1786. \input{refex/ex52.tex}
  1787. \procedure{Rmdir}{(const S : string)}
  1788. {\var{Rmdir} removes the directory \var{S}.}
  1789. {If \var{S} doesn't exist, or isn't empty, a run-time error is generated.
  1790. }
  1791. {\seep{Chdir}, \seep{Rmdir}}
  1792. \input{refex/ex53.tex}
  1793. \function{Round}{(X : real)}{Longint}
  1794. {\var{Round} rounds \var{X} to the closest integer, which may be bigger or
  1795. smaller than \var{X}.}
  1796. {None.}
  1797. {\seef{Frac}, \seef{Int}, \seef{Trunc}}
  1798. \input{refex/ex54.tex}
  1799. \procedure{Runerror}{(ErrorCode : Word)}
  1800. {\var{Runerror} stops the execution of the program, and generates a
  1801. run-time error \var{ErrorCode}.}
  1802. {None.}
  1803. {\seep{Exit}, \seep{Halt}}
  1804. \input{refex/ex55.tex}
  1805. \procedure{Seek}{(Var F; Count : Longint)}
  1806. {\var{Seek} sets the file-pointer for file \var{F} to record Nr. \var{Count}.
  1807. The first record in a file has \var{Count=0}. F can be any file type, except
  1808. \var{Text}. If \var{F} is an untyped file, with no specified record size, 128
  1809. is assumed.}
  1810. {A run-time error is generated if \var{Count} points to a position outside
  1811. the file, or the file isn't opened.}
  1812. {\seef{Eof}, \seef{SeekEof}, \seef{SeekEoln}}
  1813. \input{refex/ex56.tex}
  1814. \function{SeekEof}{[(Var F : text)]}{Boolean}
  1815. {\var{SeekEof} returns \var{True} is the file-pointer is at the end of the
  1816. file. It ignores all whitespace.
  1817. Calling this function has the effect that the file-position is advanced
  1818. until the first non-whitespace character or the end-of-file marker is
  1819. reached.
  1820. If the end-of-file marker is reached, \var{True} is returned. Otherwise,
  1821. False is returned.
  1822. If the parameter \var{F} is omitted, standard \var{Input} is assumed.
  1823. }
  1824. {A run-time error is generated if the file \var{F} isn't opened.}
  1825. {\seef{Eof}, \seef{SeekEoln}, \seep{Seek}}
  1826. \input{refex/ex57.tex}
  1827. \function{SeekEoln}{[(Var F : text)]}{Boolean}
  1828. {\var{SeekEoln} returns \var{True} is the file-pointer is at the end of the
  1829. current line. It ignores all whitespace.
  1830. Calling this function has the effect that the file-position is advanced
  1831. until the first non-whitespace character or the end-of-line marker is
  1832. reached.
  1833. If the end-of-line marker is reached, \var{True} is returned. Otherwise,
  1834. False is returned.
  1835. The end-of-line marker is defined as \var{\#10}, the LineFeed character.
  1836. If the parameter \var{F} is omitted, standard \var{Input} is assumed.}
  1837. {A run-time error is generated if the file \var{F} isn't opened.}
  1838. {\seef{Eof}, \seef{SeekEof}, \seep{Seek}}
  1839. \input{refex/ex58.tex}
  1840. \function{Seg}{Var X}{Longint}
  1841. {\var{Seg} returns the segment of the address of a variable.
  1842. This function is only supported for compatibility. In \fpc, it
  1843. returns always 0, since \fpc is a 32 bit compiler, segments have no meaning.
  1844. }
  1845. {None.}
  1846. {\seef{DSeg}, \seef{CSeg}, \seef{Ofs}, \seef{Ptr}}
  1847. \input{refex/ex60.tex}
  1848. \procedure{SetTextBuf}{(Var f : Text; Var Buf[; Size : Word])}
  1849. {\var{SetTextBuf} assigns an I/O buffer to a text file. The new buffer is
  1850. located at \var{Buf} and is \var{Size} bytes long. If \var{Size} is omitted,
  1851. then \var{SizeOf(Buf)} is assumed.
  1852. The standard buffer of any text file is 128 bytes long. For heavy I/0
  1853. operations this may prove too slow. The \var{SetTextBuf} procedure allows
  1854. you to set a bigger buffer for your application, thus reducing the number of
  1855. system calls, and thus reducing the load on the system resources.
  1856. The maximum size of the newly assigned buffer is 65355 bytes.
  1857. {\em Remark 1:} Never assign a new buffer to an opened file. You can assign a
  1858. new buffer immediately after a call to \seep{Rewrite}, \seep{Reset} or
  1859. \var{Append}, but not after you read from/wrote to the file. This may cause
  1860. loss of data. If you still want to assign a new buffer after read/write
  1861. operations have been performed, flush the file first. This will ensure that
  1862. the current buffer is emptied.
  1863. {\em Remark 2:} Take care that the buffer you assign is always valid. If you
  1864. assign a local variable as a buffer, then after your program exits the local
  1865. program block, the buffer will no longer be valid, and stack problems may
  1866. occur.
  1867. }
  1868. {No checking on \var{Size} is done.}
  1869. {\seep{Assign}, \seep{Reset}, \seep{Rewrite}, \seep{Append}}
  1870. \input{refex/ex61.tex}
  1871. \function{Sin}{(X : real)}{Real}
  1872. {\var{Sin} returns the sine of its argument \var{X}, where \var{X} is an
  1873. angle in radians.}
  1874. {None.}
  1875. {\seef{Cos}, \seef{Pi}, \seef{Exp}}
  1876. \input{refex/ex62.tex}
  1877. \function{SizeOf}{(X : Any Type)}{Longint}
  1878. {\var{SizeOf} Returns the size, in bytes, of any variable or type-identifier.
  1879. {\em Remark:} this isn't really a RTL function. Its result is calculated at
  1880. compile-time, and hard-coded in your executable.}
  1881. {None.}
  1882. {\seef{Addr}}
  1883. \input{refex/ex63.tex}
  1884. \Function{Sptr}{Pointer}
  1885. {\var{Sptr} returns the current stack pointer.
  1886. }{None.}{}
  1887. \input{refex/ex64.tex}
  1888. \function{Sqr}{(X : Real)}{Real}
  1889. {\var{Sqr} returns the square of its argument \var{X}.}
  1890. {None.}
  1891. {\seef{Sqrt}, \seef{Ln}, \seef{Exp}}
  1892. \input{refex/ex65.tex}
  1893. \function{Sqrt}{(X : Real)}{Real}
  1894. {\var{Sqrt} returns the square root of its argument \var{X}, which must be
  1895. positive.}
  1896. {If \var{X} is negative, then a run-time error is generated.}
  1897. {\seef{Sqr}, \seef{Ln}, \seef{Exp}}
  1898. \input{refex/ex66.tex}
  1899. \Function{SSeg}{Longint}
  1900. { \var{SSeg} returns the Stack Segment. This function is only
  1901. supported for compatibolity reasons, as \var{Sptr} returns the
  1902. correct contents of the stackpointer.}
  1903. {None.}{\seef{Sptr}}
  1904. \input{refex/ex67.tex}
  1905. \procedure{Str}{(Var X[:NumPlaces[:Decimals]]; Var S : String)}
  1906. {\var{Str} returns a string which represents the value of X. X can be any
  1907. numerical type.
  1908. The optional \var{NumPLaces} and \var{Decimals} specifiers control the
  1909. formatting of the string.}
  1910. {None.}
  1911. {\seep{Val}}
  1912. \input{refex/ex68.tex}
  1913. \function{Swap}{(X)}{Type of X}
  1914. {\var{Swap} swaps the high and low order bytes of \var{X} if \var{X} is of
  1915. type \var{Word} or \var{Integer}, or swaps the high and low order words of
  1916. \var{X} if \var{X} is of type \var{Longint} or \var{Cardinal}.
  1917. The return type is the type of \var{X}}
  1918. {None.}{\seef{Lo}, \seef{Hi}}
  1919. \input{refex/ex69.tex}
  1920. \function{Trunc}{(X : real)}{Longint}
  1921. {\var{Trunc} returns the integer part of \var{X},
  1922. which is always smaller than (or equal to) \var{X}.}
  1923. {None.}
  1924. {\seef{Frac}, \seef{Int}, \seef{Trunc}}
  1925. \input{refex/ex70.tex}
  1926. \procedure{Truncate}{(Var F : file)}
  1927. {\var{Truncate} truncates the (opened) file \var{F} at the current file
  1928. position.
  1929. }{Errors are reported by IOresult.}{\seep{Append}, \seef{Filepos},
  1930. \seep{Seek}}
  1931. \input{refex/ex71.tex}
  1932. \function{Upcase}{(C : Char or string)}{Char or String}
  1933. {\var{Upcase} returns the uppercase version of its argument \var{C}.
  1934. If its argument is a string, then the complete string is converted to
  1935. uppercase. The type of the returned value is the same as the type of the
  1936. argument.}
  1937. {None.}
  1938. {\seef{Lowercase}}
  1939. \input{refex/ex72.tex}
  1940. \procedure{Val}{(const S : string;var V;var Code : word)}
  1941. {\var{Val} converts the value represented in the string \var{S} to a numerical
  1942. value, and stores this value in the variable \var{V}, which
  1943. can be of type \var{Longint}, \var{real} and \var{Byte}.
  1944. If the conversion isn't succesfull, then the parameter \var{Code} contains
  1945. the index of the character in \var{S} which prevented the conversion.
  1946. The string \var{S} isn't allow to contain spaces.}
  1947. {If the conversion doesn't succeed, the value of \var{Code} indicates the
  1948. position where the conversion went wrong.}
  1949. {\seep{Str}}
  1950. \input{refex/ex74.tex}
  1951. \procedure{Write}{([Var F : Any filetype;] V1 [; V2; ... , Vn)]}
  1952. {\var{Write} writes the contents of the variables \var{V1}, \var{V2} etc. to
  1953. the file \var{F}. \var{F} can be a typed file, or a \var{Text} file.
  1954. If \var{F} is a typed file, then the variables \var{V1}, \var{V2} etc. must
  1955. be of the same type as the type in the declaration of \var{F}. Untyped files
  1956. are not allowed.
  1957. If the parameter \var{F} is omitted, standard output is assumed.
  1958. If \var{F} is of type \var{Text}, then the necessary conversions are done
  1959. such that the output of the variables is in human-readable format.
  1960. This conversion is done for all numerical types. Strings are printed exactly
  1961. as they are in memory, as well as \var{PChar} types.
  1962. The format of the numerical conversions can be influenced through
  1963. the following modifiers:
  1964. \var{ OutputVariable : NumChars [: Decimals ] }
  1965. This will print the value of \var{OutputVariable} with a minimum of
  1966. \var{NumChars} characters, from which \var{Decimals} are reserved for the
  1967. decimals. If the number cannot be represented with \var{NumChars} characters,
  1968. \var{NumChars} will be increased, until the representation fits. If the
  1969. representation requires less than \var{NumChars} characters then the output
  1970. is filled up with spaces, to the left of the generated string, thus
  1971. resulting in a right-aligned representation.
  1972. If no formatting is specified, then the number is written using its natural
  1973. length, with a space in front of it if it's positive, and a minus sign if
  1974. it's negative.
  1975. Real numbers are, by default, written in scientific notation.
  1976. }
  1977. {If an error occurs, a run-time error is generated. This behavior can be
  1978. controlled with the \var{\{\$i\}} switch. }
  1979. {\seep{Writeln}, \seep{Read}, \seep{Readln}, \seep{Blockwrite} }
  1980. \procedure{Writeln}{[([Var F : Text;] [V1 [; V2; ... , Vn)]]}
  1981. {\var{Writeln} does the same as \seep{Write} for text files, and emits a
  1982. Carriage Return - LineFeed character pair after that.
  1983. If the parameter \var{F} is omitted, standard output is assumed.
  1984. If no variables are specified, a Carriage Return - LineFeed character pair
  1985. is emitted, resulting in a new line in the file \var{F}.
  1986. {\em Remark:} Under \linux, the Carriage Return character is omitted, as
  1987. customary in Unix environments.
  1988. }
  1989. {If an error occurs, a run-time error is generated. This behavior can be
  1990. controlled with the \var{\{\$i\}} switch. }
  1991. {\seep{Write}, \seep{Read}, \seep{Readln}, \seep{Blockwrite}}
  1992. \input{refex/ex75.tex}
  1993. %
  1994. % The index.
  1995. %
  1996. \printindex
  1997. \end{document}