ref.tex 94 KB

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