ref.tex 81 KB

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