ref.tex 98 KB

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