ref.tex 182 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166
  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{ifthen}
  26. \usepackage{xspace}
  27. \usepackage{a4dutch}
  28. \usepackage{makeidx}
  29. \usepackage{html}
  30. \usepackage{htmllist}
  31. \usepackage{fancyheadings}
  32. \usepackage{epsfig}
  33. \usepackage{multicol}
  34. \usepackage{fpc}
  35. \latex{%
  36. \ifpdf
  37. \usepackage[pdftex,bookmarks=true]{hyperref}
  38. \pdfcompresslevel=9
  39. \pdfpagewidth=210mm
  40. \pdfpageheight=297mm
  41. \pdfinfo{/Author(Michael Van Canneyt)
  42. /Title(Standard units Reference Guide)
  43. /Subject(Free Pascal Reference guide)
  44. /Keywords(Free Pascal, Language, System Unit)
  45. }
  46. \fi
  47. }
  48. %
  49. \html{\input{fpc-html.tex}}
  50. %
  51. % Settings
  52. %
  53. \pagestyle{fancy}
  54. \makeindex
  55. %
  56. % Syntax style
  57. %
  58. \usepackage{syntax}
  59. \input{syntax/diagram.tex}
  60. \latex{\usepackage{listings}\usepackage{lst017}\usepackage{lstdelphi}%
  61. \blankstringtrue
  62. \selectlisting{delphi}
  63. \stringstyle{\ttfamily}
  64. \keywordstyle{\bfseries}
  65. \prelisting{\sffamily\sloppy}
  66. }
  67. %
  68. % Start of document.
  69. %
  70. \begin{document}
  71. \title{Free Pascal :\\ Reference guide.}
  72. \docdescription{Reference guide for Free Pascal.}
  73. \docversion{1.4}
  74. \input{date.inc}
  75. \author{Micha\"el Van Canneyt}
  76. \maketitle
  77. \tableofcontents
  78. \newpage
  79. \listoftables
  80. \newpage
  81. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  82. % Introduction
  83. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  84. \section{About this guide}
  85. This document describes all constants, types, variables, functions and
  86. procedures as they are declared in the system unit.
  87. Furthermore, it describes all pascal constructs supported by \fpc, and lists
  88. all supported data types. It does not, however, give a detailed explanation
  89. of the pascal language. The aim is to list which Pascal constructs are
  90. supported, and to show where the \fpc implementation differs from the
  91. Turbo Pascal implementation.
  92. \subsection{Notations}
  93. Throughout this document, we will refer to functions, types and variables
  94. with \var{typewriter} font. Functions and procedures have their own
  95. subsections, and for each function or procedure we have the following
  96. topics:
  97. \begin{description}
  98. \item [Declaration] The exact declaration of the function.
  99. \item [Description] What does the procedure exactly do ?
  100. \item [Errors] What errors can occur.
  101. \item [See Also] Cross references to other related functions/commands.
  102. \end{description}
  103. The cross-references come in two flavours:
  104. \begin{itemize}
  105. \item References to other functions in this manual. In the printed copy, a
  106. number will appear after this reference. It refers to the page where this
  107. function is explained. In the on-line help pages, this is a hyperlink, on
  108. which you can click to jump to the declaration.
  109. \item References to Unix manual pages. (For linux related things only) they
  110. are printed in \var{typewriter} font, and the number after it is the Unix
  111. manual section.
  112. \end{itemize}
  113. \subsection{Syntax diagrams}
  114. All elements of the pascal language are explained in syntax diagrams.
  115. Syntax diagrams are like flow charts. Reading a syntax diagram means that
  116. you must get from the left side to the right side, following the arrows.
  117. When you are at the right of a syntax diagram, and it ends with a single
  118. arrow, this means the syntax diagram is continued on the next line. If
  119. the line ends on 2 arrows pointing to each other, then the diagram is
  120. continued on the next line.
  121. syntactical elements are written like this
  122. \begin{mysyntdiag}
  123. \synt{syntactical\ elements\ are\ like\ this}
  124. \end{mysyntdiag}
  125. keywords you must type exactly as in the diagram:
  126. \begin{mysyntdiag}
  127. \lit*{keywords\ are\ like\ this}
  128. \end{mysyntdiag}
  129. When you can repeat something there is an arrow around it:
  130. \begin{mysyntdiag}
  131. \<[b] \synt{this\ can\ be\ repeated} \\ \>
  132. \end{mysyntdiag}
  133. When there are different possibilities, they are listed in columns:
  134. \begin{mysyntdiag}
  135. \(
  136. \synt{First\ possibility} \\
  137. \synt{Second\ possibility}
  138. \)
  139. \end{mysyntdiag}
  140. Note, that one of the possibilities can be empty:
  141. \begin{mysyntdiag}
  142. \[
  143. \synt{First\ possibility} \\
  144. \synt{Second\ possibility}
  145. \]
  146. \end{mysyntdiag}
  147. This means that both the first or second possibility are optional.
  148. Of course, all these elements can be combined and nested.
  149. \part{The Pascal language}
  150. %
  151. % The Pascal language
  152. %
  153. \chapter{Pascal Tokens}
  154. In this chapter we describe all the pascal reserved words, as well as the
  155. various ways to denote strings, numbers identifiers etc.
  156. \section{Symbols}
  157. Free Pascal allows all characters, digits and some special ASCII symbols
  158. in a Pascal source file.
  159. \input{syntax/symbol.syn}
  160. The following characters have a special meaning:
  161. \begin{verbatim}
  162. + - * / = < > [ ] . , ( ) : ^ @ { } $ #
  163. \end{verbatim}
  164. and the following character pairs too:
  165. \begin{verbatim}
  166. <= >= := += -= *= /= (* *) (. .) //
  167. \end{verbatim}
  168. When used in a range specifier, the character pair \var{(.} is equivalent to
  169. the left square bracket \var{[}. Likewise, the character pair \var{.)} is
  170. equivalent to the right square bracket \var{]}.
  171. When used for comment delimiters, the character pair \var{(*} is equivalent
  172. to the left brace \var{\{} and the character pair \var{*)} is equivalent
  173. to the right brace \var{\}}.
  174. These character pairs retain their normal meaning in string expressions.
  175. \section{Comments}
  176. \fpc supports the use of nested comments. The following constructs are valid
  177. comments:
  178. \begin{verbatim}
  179. (* This is an old style comment *)
  180. { This is a Trubo Pascal comment }
  181. // This is a Delphi comment. All is ignored till the end of the line.
  182. \end{verbatim}
  183. The last line would cause problems when attempting to compile with Delphi or
  184. Turbo Pascal. These compiler would consider the first matching brace
  185. \var{\}} as the end of the comment delimiter. If you wish to have this
  186. behaviour, you can use the \var{-So} switch, and the \fpc compiler will
  187. act the same way.
  188. The following are valid ways of nesting comments:
  189. \begin{verbatim}
  190. { Comment 1 (* comment 2 *) }
  191. (* Comment 1 { comment 2 } *)
  192. { comment 1 // Comment 2 }
  193. (* comment 1 // Comment 2 *)
  194. // comment 1 (* comment 2 *)
  195. // comment 1 { comment 2 }
  196. \end{verbatim}
  197. The last two comments {\em must} be on one line. The following two will give
  198. errors:
  199. \begin{verbatim}
  200. // Valid comment { No longer valid comment !!
  201. }
  202. \end{verbatim}
  203. and
  204. \begin{verbatim}
  205. // Valid comment (* No longer valid comment !!
  206. *)
  207. \end{verbatim}
  208. The compiler will react with a 'invalid character' error when it encounters
  209. such constructs, regardless of the \var{-So} switch.
  210. \section{Reserved words}
  211. Reserved words are part of the Pascal language, and cannot be redefined.
  212. They will be denoted as {\sffamily\bfseries this} throughout the syntax
  213. diagrams. Reserved words can be typed regardless of case, i.e. Pascal is
  214. case insensitive.
  215. We make a distinction between Turbo Pascal and Delphi reserved words, since
  216. with the \var{-So} switch, only the Turbo Pascal reserved words are
  217. recognised, and the Delphi ones can be redefined. By default, \fpc
  218. recognises the Delphi reserved words.
  219. \subsection{Turbo Pascal reserved words}
  220. The following keywords exist in Turbo Pascal mode
  221. \begin{multicols}{4}
  222. \begin{verbatim}
  223. absolute
  224. and
  225. array
  226. asm
  227. begin
  228. break
  229. case
  230. const
  231. constructor
  232. continue
  233. destructor
  234. dispose
  235. div
  236. do
  237. downto
  238. else
  239. end
  240. exit
  241. false
  242. file
  243. for
  244. function
  245. goto
  246. if
  247. implementation
  248. in
  249. inherited
  250. inline
  251. interface
  252. label
  253. mod
  254. new
  255. nil
  256. not
  257. object
  258. of
  259. on
  260. operator
  261. or
  262. packed
  263. procedure
  264. program
  265. record
  266. repeat
  267. self
  268. set
  269. shl
  270. shr
  271. string
  272. then
  273. to
  274. true
  275. try
  276. type
  277. unit
  278. until
  279. uses
  280. var
  281. while
  282. with
  283. xor
  284. \end{verbatim}
  285. \end{multicols}
  286. \subsection{Delphi reserved words}
  287. The Delphi (II) reserved words are the same as the pascal ones, plus the
  288. following ones:
  289. \begin{multicols}{4}
  290. \begin{verbatim}
  291. as
  292. class
  293. except
  294. exports
  295. finalization
  296. finally
  297. initialization
  298. is
  299. library
  300. on
  301. property
  302. raise
  303. try
  304. \end{verbatim}
  305. \end{multicols}
  306. \subsection{\fpc reserved words}
  307. On top of the Turbo Pascal and Delphi reserved words, \fpc also considers
  308. the following as reserved words:
  309. \begin{multicols}{4}
  310. \begin{verbatim}
  311. dispose
  312. exit
  313. export
  314. false
  315. new
  316. popstack
  317. true
  318. \end{verbatim}
  319. \end{multicols}
  320. \subsection{Modifiers}
  321. The following is a list of all modifiers. Contrary to Delphi, \fpc doesn't
  322. allow you to redefine these modifiers.
  323. \begin{multicols}{4}
  324. \begin{verbatim}
  325. absolute
  326. abstract
  327. alias
  328. assembler
  329. cdecl
  330. default
  331. export
  332. external
  333. far
  334. forward
  335. index
  336. name
  337. near
  338. override
  339. pascal
  340. popstack
  341. private
  342. protected
  343. public
  344. published
  345. read
  346. register
  347. stdcall
  348. virtual
  349. write
  350. \end{verbatim}
  351. \end{multicols}
  352. Remark that predefined types such as \var{Byte}, \var{Boolean} and constants
  353. such as \var{maxint} are {\em not} reserved words. They are
  354. identifiers, declared in the system unit. This means that you can redefine
  355. these types. You are, however, not encouraged to do this, as it will cause
  356. a lot of confusion.
  357. \section{Identifiers}
  358. Identifiers denote constants, types, variables, procedures and functions,
  359. units, and programs. All names of things that you define are identifiers.
  360. An identifier consists of 255 significant characters (letters, digits and
  361. the underscore character), from which the first must be an alphanumeric
  362. character, or an underscore (\var{\_})
  363. The following diagram gives the basic syntax for identifiers.
  364. \input{syntax/identifier.syn}
  365. \section{Numbers}
  366. Numbers are denoted in decimal notation. Real (or decimal) numbers are
  367. written using engeneering notation (e.g. \var{0.314E1}).
  368. \fpc supports hexadecimal format the same way as Turbo Pascal does. To
  369. specify a constant value in hexadecimal format, prepend it with a dollar
  370. sign (\var{\$}). Thus, the hexadecimal \var{\$FF} equals 255 decimal.
  371. In addition to the support for hexadecimal notation, \fpc also supports
  372. binary notation. You can specify a binary number by preceding it with a
  373. percent sign (\var{\%}). Thus, \var{255} can be specified in binary notation
  374. as \var{\%11111111}.
  375. The following diagrams show the syntax for numbers.
  376. \input{syntax/numbers.syn}
  377. \section{Labels}
  378. Labels can be digit sequences or identifiers.
  379. \input{syntax/label.syn}
  380. \section{Character strings}
  381. A character string (or string for short) is a sequence of zero or more
  382. characters from the ASCII character set, enclosed by single quotes, and on 1
  383. line of the program source.
  384. A character set with nothing between the quotes (\var{'{}'}) is an empty
  385. string.
  386. \input{syntax/string.syn}
  387. \chapter{Constants}
  388. Just as in Turbo Pascal, \fpc supports both normal and typed constants.
  389. \section{Ordinary constants}
  390. Ordinary constants declarations are no different from the Turbo Pascal or
  391. Delphi implementation.
  392. \input{syntax/const.syn}
  393. The compiler must be able to evaluate the expression in a constant
  394. declaration at compile time. This means that most of the functions
  395. in the Run-Time library cannot be used in a constant declaration.
  396. Operators such as \var{+, -, *, /, not, and, or, div(), mod(), ord(), chr(),
  397. sizeof} can be used, however. For more information on expressions,
  398. You can only declare constants of the following types: \var{Ordinal types},
  399. \var{Real types}, \var{Char}, and \var{String}.
  400. The following are all valid constant declarations:
  401. \begin{verbatim}
  402. Const
  403. e = 2.7182818; { Real type constant. }
  404. a = 2; { Integer type constant. }
  405. c = '4'; { Character type constant. }
  406. s = 'This is a constant string'; {String type constant.}
  407. s = chr(32)
  408. ls = SizeOf(Longint);
  409. \end{verbatim}
  410. Assigning a value to a constant is not permitted. Thus, given the previous
  411. declaration, the following will result in a compiler error:
  412. \begin{verbatim}
  413. s := 'some other string';
  414. \end{verbatim}
  415. \section{Typed constants}
  416. Typed constants serve to provide a program with initialized variables.
  417. Contrary to ordinary constants, they may be assigned to at run-time.
  418. The difference with normal variables is that their value is initialised
  419. when the program starts, whereas normal variables must be initialised
  420. explicitly.
  421. \input{syntax/tconst.syn}
  422. Given the declaration:
  423. \begin{verbatim}
  424. Const
  425. S : String = 'This is a typed constant string';
  426. \end{verbatim}
  427. The following is a valid assignment:
  428. \begin{verbatim}
  429. S := 'Result : '+Func;
  430. \end{verbatim}
  431. Where \var{Func} is a function that returns a \var{String}.
  432. Typed constants also allow you to initialize arrays and records. For arrays,
  433. the initial elements must be specified, surrounded by round brackets, and
  434. separated by commas. The number of elements must be exactly the same as
  435. number of elements in the declaration of the type.
  436. As an example:
  437. \begin{verbatim}
  438. Const
  439. tt : array [1..3] of string[20] = ('ikke', 'gij', 'hij');
  440. ti : array [1..3] of Longint = (1,2,3);
  441. \end{verbatim}
  442. For constant records, you should specify each element of the record, in the
  443. form \var{Field : Value}, separated by commas, and surrounded by round
  444. brackets.
  445. As an example:
  446. \begin{verbatim}
  447. Type
  448. Point = record
  449. X,Y : Real
  450. end;
  451. Const
  452. Origin : Point = (X:0.0 , Y:0.0);
  453. \end{verbatim}
  454. The order of the fields in a constant record needs to be the same as in the type declaration,
  455. otherwise you'll get a compile-time error.
  456. \chapter{Types}
  457. All variables have a type. \fpc supports the same basic types as Turbo
  458. Pascal, with some extra types from Delphi.
  459. You can declare your own types, which is in essence defining an identifier
  460. that can be used to denote your custom type when declaring variables further
  461. in the source code.
  462. \input{syntax/typedecl.syn}
  463. There are 7 major type classes :
  464. \input{syntax/type.syn}
  465. The last class, {\sffamily type identifier}, is just a means to give another
  466. name to a type. This gives you a way to make types platform independent, by
  467. only using your own types, and then defining these types for each platform
  468. individually. The programmer that uses your units doesn't have to worry
  469. about type size and so on. It also allows you to use shortcut names for
  470. fully qualified type names. You can e.g. define \var{system.longint} as
  471. \var{Olongint} and then redefine \var{longint}.
  472. \section{Base types}
  473. The base or simple types of \fpc are the Delphi types.
  474. We will discuss each separate.
  475. \input{syntax/typesim.syn}
  476. \subsection{Ordinal types}
  477. With the exception of Real types, all base types are ordinal types.
  478. Ordinal types have the following characteristics:
  479. \begin{enumerate}
  480. \item Ordinal types are countable and ordered, i.e. it is, in principle,
  481. possible to start counting them one bye one, in a specified order.
  482. This property allows the operation of functions as \seep{Inc}, \seef{Ord},
  483. \seep{Dec}
  484. on ordinal types to be defined.
  485. \item Ordinal values have a smallest possible value. Trying to apply the
  486. \seef{Pred} function on the smallest possible value will generate a range
  487. check error.
  488. \item Ordinal values have a largest possible value. Trying to apply the
  489. \seef{Succ} function on the larglest possible value will generate a range
  490. check error.
  491. \end{enumerate}
  492. \subsubsection{Integers}
  493. A list of pre-defined ordinal types is presented in \seet{ordinals}
  494. \begin{FPCltable}{l}{Predefined ordinal types}{ordinals}
  495. Name\\ \hline
  496. Integer \\
  497. Shortint \\
  498. SmallInt \\
  499. Longint \\
  500. Byte \\
  501. Word \\
  502. Cardinal \\
  503. Boolean \\
  504. ByteBool \\
  505. LongBool \\
  506. Char \\ \hline
  507. \end{FPCltable}
  508. The integer types, and their ranges and sizes, that are predefined in
  509. \fpc are listed in \seet{integers}.
  510. \begin{FPCltable}{lcr}{Predefined integer types}{integers}
  511. Type & Range & Size in bytes \\ \hline
  512. Byte & 0 .. 255 & 1 \\
  513. Shortint & -127 .. 127 & 1\\
  514. Integer & -32768 .. 32767 & 2\footnote{The integer type is redefined as
  515. longint if you are in Delphi or ObjFPC mode, and has then size 4} \\
  516. Word & 0 .. 65535 & 2 \\
  517. Longint & -2147483648 .. 2147483648 & 4\\
  518. Cardinal\footnote{The cardinal type support is buggy until version 0.99.6} & 0..4294967296 & 4 \\ \hline
  519. \end{FPCltable}
  520. \fpc does automatic type conversion in expressions where different kinds of
  521. integer types are used.
  522. \subsubsection{Boolean types}
  523. \fpc supports the \var{Boolean} type, with its two pre-defined possible
  524. values \var{True} and \var{False}, as well as the \var{ByteBool},
  525. \var{WordBool} and \var{LongBool}. These are the only two values that can be
  526. assigned to a \var{Boolean} type. Of course, any expression that resolves
  527. to a \var{boolean} value, can also be assigned to a boolean type.
  528. \begin{FPCltable}{lll}{Boolean types}{booleantypes}
  529. Name & Size & Ord(True) \\ \hline
  530. Boolean & 1 & 1 \\
  531. ByteBool & 1 & Any nonzero value \\
  532. WordBool & 2 & Any nonzero value \\
  533. LongBool & 4 & Any nonzero value \\ \hline
  534. \end{FPCltable}
  535. Assuming \var{B} to be of type \var{Boolean}, the following are valid
  536. assignments:
  537. \begin{verbatim}
  538. B := True;
  539. B := False;
  540. B := 1<>2; { Results in B := True }
  541. \end{verbatim}
  542. Boolean expressions are also used in conditions.
  543. {\em Remark:} In \fpc, boolean expressions are always evaluated in such a
  544. way that when the result is known, the rest of the expression will no longer
  545. be evaluated (Called short-cut evaluation). In the following example, the function \var{Func} will never
  546. be called, which may have strange side-effects.
  547. \begin{verbatim}
  548. ...
  549. B := False;
  550. A := B and Func;
  551. \end{verbatim}
  552. Here \var{Func} is a function which returns a \var{Boolean} type.
  553. {\em Remark:} The wordbool, longbool and bytebool were not supported
  554. by \fpc until version 0.99.6.
  555. \subsubsection{Enumeration types}
  556. Enumeration types are supported in \fpc. On top of the Turbo Pascal
  557. implementation, \fpc allows also a C-style extension of the
  558. enumeration type, where a value is assigned to a particular element of
  559. the enumeration list.
  560. \input{syntax/typeenum.syn}
  561. (see \seec{Expressions} for how to use expressions)
  562. When using assigned enumerated types, the assigned elements must be in
  563. ascending numerical order in the list, or the compiler will complain.
  564. The expressions used in assigned enumerated elements must be known at
  565. compile time.
  566. So the following is a correct enumerated type declaration:
  567. \begin{verbatim}
  568. Type
  569. Direction = ( North, East, South, West );
  570. \end{verbatim}
  571. The C style enumeration type looks as follows:
  572. \begin{verbatim}
  573. Type
  574. EnumType = (one, two, three, forty := 40,fortyone);
  575. \end{verbatim}
  576. As a result, the ordinal number of \var{forty} is \var{40}, and not \var{3},
  577. as it would be when the \var{':= 40'} wasn't present.
  578. The ordinal value of \var{fortyone} is then {41}, and not \var{4}, as it
  579. would be when the assignment wasn't present. After an assignment in an
  580. enumerated definition the compiler adds 1 to the assigned value to assign to
  581. the next enumerated value.
  582. When specifying such an enumeration type, it is important to keep in mind
  583. that you should keep the enumerated elements in ascending order. The
  584. following will produce a compiler error:
  585. \renewcommand{\prelisting}{\sffamily}
  586. \begin{verbatim}
  587. Type
  588. EnumType = (one, two, three, forty := 40, thirty := 30);
  589. \end{verbatim}
  590. It is necessary to keep \var{forty} and \var{thirty} in the correct order.
  591. When using enumeration types it is important to keep the following points
  592. in mind:
  593. \begin{enumerate}
  594. \item You cannot use the \var{Pred} and \var{Succ} functions on
  595. this kind of enumeration types. If you try to do that, you'll get a compiler
  596. error.
  597. \item Enumeration types are by default stored in 4 bytes. You can change
  598. this behaviour with the \var{\{\$PACKENUM n\}} compiler directive, which
  599. tells the compiler the minimal number of bytes to be used for enumeration
  600. types.
  601. For instance
  602. \begin{verbatim}
  603. Type
  604. LargeEnum = ( BigOne, BigTwo, BigThree );
  605. {$PACKENUM 1}
  606. SmallEnum = ( one, two, three );
  607. Var S : SmallEnum;
  608. L : LargeEnum;
  609. begin
  610. WriteLn ('Small enum : ',SizeOf(S));
  611. WriteLn ('Large enum : ',SizeOf(L));
  612. end.
  613. \end{verbatim}
  614. will, when run, print the following:
  615. \begin{verbatim}
  616. Small enum : 1
  617. Large enum : 4
  618. \end{verbatim}
  619. \end{enumerate}
  620. More information can be found in the \progref, in the compiler directives
  621. section.
  622. \subsubsection{Subrange types}
  623. A subrange type is a range of values from an ordinal type (the {\em host}
  624. type). To define a subrange type, one must specify it's limiting values: the
  625. highest and lowest value of the type.
  626. \input{syntax/typesubr.syn}
  627. Some of the predefined \var{integer} types are defined as subrange types:
  628. \begin{verbatim}
  629. Type
  630. Longint = $80000000..$7fffffff;
  631. Integer = -32768..32767;
  632. shortint = -128..127;
  633. byte = 0..255;
  634. Word = 0..65535;
  635. \end{verbatim}
  636. But you can also define subrange types of enumeration types:
  637. \begin{verbatim}
  638. Type
  639. Days = (monday,tuesday,wednesday, thursday,friday,
  640. saturday,sunday);
  641. WorkDays = monday .. friday;
  642. WeekEnd = Saturday .. Sunday;
  643. \end{verbatim}
  644. \subsection{Real types}
  645. \fpc uses the math coprocessor (or an emulation) for all its floating-point
  646. calculations. The Real native type is processor dependant,
  647. but it is either Single or Double. Only the IEEE floating point types are
  648. supported, and these depend on the target processor and emulation options.
  649. The true Turbo Pascal compatible types are listed in
  650. \seet{Reals}.
  651. \begin{FPCltable}{lccr}{Supported Real types}{Reals}
  652. Type & Range & Significant digits & Size\footnote{In Turbo Pascal.} \\ \hline
  653. Single & 1.5E-45 .. 3.4E38 & 7-8 & 4 \\
  654. Real & 5.0E-324 .. 1.7E308 & 15-16 & 8 \\
  655. Double & 5.0E-324 .. 1.7E308 & 15-16 & 8 \\
  656. Extended & 1.9E-4951 .. 1.1E4932 & 19-20 & 10\\
  657. Comp\footnote{\var{Comp} only holds integer values.} & -2E64+1 .. 2E63-1 & 19-20 & 8 \\
  658. \end{FPCltable}
  659. Until version 0.9.1 of the compiler, all the \var{Real} types are mapped to
  660. type \var{Double}, meaning that they all have size 8. The \seef{SizeOf} function
  661. is your friend here. The \var{Real} type of turbo pascal is automatically
  662. mapped to Double. The \var{Comp} type is, in effect, a 64-bit integer.
  663. \section{Character types}
  664. \subsection{Char}
  665. \fpc supports the type \var{Char}. A \var{Char} is exactly 1 byte in
  666. size, and contains one character.
  667. You can specify a character constant by enclosing the character in single
  668. quotes, as follows : 'a' or 'A' are both character constants.
  669. You can also specify a character by their ASCII
  670. value, by preceding the ASCII value with the number symbol (\#). For example
  671. specifying \var{\#65} would be the same as \var{'A'}.
  672. Also, the caret character (\verb+^+) can be used in combination with a letter to
  673. specify a character with ASCII value less than 27. Thus \verb+^G+ equals
  674. \var{\#7} (G is the seventh letter in the alphabet.)
  675. If you want to represent the single quote character, type it two times
  676. successively, thus \var{''''} represents the single quote character.
  677. \subsection{Strings}
  678. \fpc supports the \var{String} type as it is defined in Turbo Pascal and
  679. it supports ansistrings as in Delphi.
  680. To declare a variable as a string, use the following type specification:
  681. \input{syntax/sstring.syn}
  682. The meaning of a string declaration statement is interpreted differently
  683. depending on the \var{\{\$H\}} switch. The above declaration can declare an
  684. ansistrng or a short string.
  685. Whatever the actual type, ansistrings and short strings can be used
  686. interchangeably. The compile always takes care of the necessary type
  687. coversions. Note, however, that the result of an expression that contains
  688. ansstrings snd short strings will always be an ansistring.
  689. \subsection{Short strings}
  690. A string declaration declares a short string in the following cases:
  691. \begin{enumerate}
  692. \item If the switch is off: \var{\{\$H-\}}, the string declaration
  693. will always be a short string declaration.
  694. \item If the switch is on \var{\{\$H+\}}, and there is a length
  695. specifier, the declaration is a short string declaration.
  696. \end{enumerate}
  697. The predefined type \var{ShortString} is defined as a string of length 255:
  698. \begin{verbatim}
  699. ShortString = String[255];
  700. \end{verbatim}
  701. For short strings \fpc reserves \var{Size+1} bytes for the string \var{S},
  702. and in the zeroeth element of the string (\var{S[0]}) it will store the
  703. length of the variable.
  704. If you don't specify the size of the string, \var{255} is taken as a
  705. default.
  706. For example in
  707. \begin{verbatim}
  708. {$H-}
  709. Type
  710. NameString = String[10];
  711. StreetString = String;
  712. \end{verbatim}
  713. \var{NameString} can contain maximum 10 characters. While
  714. \var{StreetString} can contain 255 characters. The sizes of these variables
  715. are, respectively, 11 and 256 bytes.
  716. \subsection{Ansistrings}
  717. If the \var{\{\$H\}} switch is on, then a string definition that doesn't
  718. contain a length specifier, will be regarded as an ansistring.
  719. Ansistrings are strings that have no length limit. They are reference
  720. counted. Internally, an ansistring is treated as a pointer.
  721. If the string is empty (\var{''}), then the pointer is nil.
  722. If the string is not empty, then the pointer points to a structure in
  723. heap memory that looks as in seet{ansistrings}.
  724. \begin{FPCltable}{rl}{AnsiString memory structure}{ansistrings}
  725. Offset & Contains \\ \hline
  726. -12 & Longint with maximum string size. \\
  727. -8 & Longint with actual string size.\\
  728. -4 & Longint with reference count.\\
  729. 0 & Actual string, null-terminated. \\ \hline
  730. \end{FPCltable}
  731. Because of this structure, it is possible to typecast an ansistring to a
  732. pchar. If the string is empty (so the pointer is nil) then the compiler
  733. makes sure that the typecasted pchar will point to a null byte.
  734. AnsiStrings can be unlimited in length. Since the length is stored,
  735. the length of an ansistring is available immediatly, providing for fast
  736. access.
  737. Assigning one ansistring to another doesn't involve moving the actual
  738. string. A statement
  739. \begin{verbatim}
  740. S2:=S1;
  741. \end{verbatim}
  742. results in the reference count of \var{S2} being decreased by one,
  743. The referece count of \var{S1} is increased by one, and finally \var{S1}
  744. (as a pointer) is copied to \var{S2}. This is a significant speed-up in
  745. your code.
  746. If a reference count reaches zero, then the memory occupied by the
  747. string is deallocated automatically, so no memory leaks arise.
  748. When an ansistring is declared, the \fpc compiler initially
  749. allocates just memory for a pointer, not more. This pinter is guaranteed
  750. to be nil, meaning that the string is initially empty. This is
  751. true for local, global or part of a structure (arrays, records or objects).
  752. This does introduce an overhead. For instance, declaring
  753. \begin{verbatim}
  754. Var
  755. A : Array[1..100000] of string;
  756. \end{verbatim}
  757. Will copy 1000000 times \var{nil} into A. When A goes out of scope, then
  758. the 100000 strings will be dereferenced one by one. All this happens
  759. invisibly for the programmer, but when considering performance issues,
  760. this is important.
  761. Memory will be allocated only when the string is assigned a value.
  762. If the string goes out of scope, then it is automatically dereferenced.
  763. If you assign a value to a character of a string that has a reference count
  764. greater than 1, such as in the following
  765. statements:
  766. \begin{verbatim}
  767. S:=T; { reference count for S and T is now 2 }
  768. S[I]:='@';
  769. \end{verbatim}
  770. then a copy of the string is created before the assignment. This is known
  771. as {\em copy-on-write} semantics.
  772. It is impossible to access the length of an ansistring by referring to
  773. the zeroeth character. The following statement will generate a compiler
  774. error if S is an ansistring:
  775. \begin{verbatim}
  776. Len:=S[0];
  777. \end{verbatim}
  778. Instead, you must use the \seefl{Length} function to get the length of a
  779. string.
  780. To set the length of an ansistring, you can use the \seepl{SetLength}
  781. function.
  782. Constant ansistrings have a reference count of -1 and are treated specially.
  783. Ansistrings are converted to short strings by the compiler if needed,
  784. this means that you can mix the use of ansistrings ans short strings
  785. without problems.
  786. You can typecast ansistrings to \var{PChar} or \var{Pointer} types:
  787. \begin{verbatim}
  788. Var P : Pointer;
  789. PC : PChar;
  790. S : AnsiString;
  791. begin
  792. S :='This is an ansistring';
  793. PC:=Pchar(S);
  794. P :=Pointer(S);
  795. \end{verbatim}
  796. There is a difference between the two typecasts. If you typecast an empty
  797. string to a pointer, the pointer wil be \var{Nil}. If you typecast an empty
  798. ansistring to a \var{PChar}, then the result will be a pointer to a zero
  799. byte (an empty string).
  800. The result of such a typecast must be use with care. In general, it is best
  801. to consider the result of such a typecast as read-only, i.e. suitable for
  802. passing to a procedure that needs a constant pchar argument.
  803. It is therefore NOT advisable to typecast one of the following:
  804. \begin{enumerate}
  805. \item expressions.
  806. \item strings that have reference count>0. (call uniquestring if you want to
  807. ensure a string has reference count 1)
  808. \end{enumerate}
  809. \subsection{Constant strings}
  810. To specify a constant string, you enclose the string in single-quotes, just
  811. as a \var{Char} type, only now you can have more than one character.
  812. Given that \var{S} is of type \var{String}, the following are valid assignments:
  813. \begin{verbatim}
  814. S := 'This is a string.';
  815. S := 'One'+', Two'+', Three';
  816. S := 'This isn''t difficult !';
  817. S := 'This is a weird character : '#145' !';
  818. \end{verbatim}
  819. As you can see, the single quote character is represented by 2 single-quote
  820. characters next to each other. Strange characters can be specified by their
  821. ASCII value.
  822. The example shows also that you can add two strings. The resulting string is
  823. just the concatenation of the first with the second string, without spaces in
  824. between them. Strings can not be substracted, however.
  825. Whether the constant string is stored as an ansistring or a short string
  826. depends on the settings of the \var{\{\$H\}} switch.
  827. \subsection{PChar}
  828. \fpc supports the Delphi implementation of the \var{PChar} type. \var{PChar}
  829. is defined as a pointer to a \var{Char} type, but allows additional
  830. operations.
  831. The \var{PChar} type can be understood best as the Pascal equivalent of a
  832. C-style null-terminated string, i.e. a variable of type \var{PChar} is a
  833. pointer that points to an array of type \var{Char}, which is ended by a
  834. null-character (\var{\#0}).
  835. \fpc supports initializing of \var{PChar} typed constants, or a direct
  836. assignment. For example, the following pieces of code are equivalent:
  837. \begin{verbatim}
  838. program one;
  839. var p : PChar;
  840. begin
  841. P := 'This is a null-terminated string.';
  842. WriteLn (P);
  843. end.
  844. \end{verbatim}
  845. Results in the same as
  846. \begin{verbatim}
  847. program two;
  848. const P : PChar = 'This is a null-terminated string.'
  849. begin
  850. WriteLn (P);
  851. end.
  852. \end{verbatim}
  853. These examples also show that it is possible to write {\em the contents} of
  854. the string to a file of type \var{Text}.
  855. The \seestrings unit contains procedures and functions that manipulate the
  856. \var{PChar} type as you can do it in C.
  857. Since it is equivalent to a pointer to a type \var{Char} variable, it is
  858. also possible to do the following:
  859. \begin{verbatim}
  860. Program three;
  861. Var S : String[30];
  862. P : PChar;
  863. begin
  864. S := 'This is a null-terminated string.'#0;
  865. P := @S[1];
  866. WriteLn (P);
  867. end.
  868. \end{verbatim}
  869. This will have the same result as the previous two examples.
  870. You cannot add null-terminated strings as you can do with normal Pascal
  871. strings. If you want to concatenate two \var{PChar} strings, you will need
  872. to use the unit \seestrings.
  873. However, it is possible to do some pointer arithmetic. You can use the
  874. operators \var{+} and \var{-} to do operations on \var{PChar} pointers.
  875. In \seet{PCharMath}, \var{P} and \var{Q} are of type \var{PChar}, and
  876. \var{I} is of type \var{Longint}.
  877. \begin{FPCltable}{lr}{\var{PChar} pointer arithmetic}{PCharMath}
  878. Operation & Result \\ \hline
  879. \var{P + I} & Adds \var{I} to the address pointed to by \var{P}. \\
  880. \var{I + P} & Adds \var{I} to the address pointed to by \var{P}. \\
  881. \var{P - I} & Substracts \var{I} from the address pointed to by \var{P}. \\
  882. \var{P - Q} & Returns, as an integer, the distance between 2 addresses \\
  883. & (or the number of characters between \var{P} and \var{Q}) \\
  884. \hline
  885. \end{FPCltable}
  886. \section{Structured Types}
  887. A structured type is a type that can hold multiple values in one variable.
  888. Stuctured types can be nested to unlimited levels.
  889. \input{syntax/typestru.syn}
  890. Unlike Delphi, \fpc does not support the keyword \var{Packed} for all
  891. structured types, as can be seen in the syntax diagram. It will be mentioned
  892. when a type supports the \var{packed} keyword.
  893. In the following, each of the possible structured types is discussed.
  894. \subsection{Arrays}
  895. \fpc supports arrays as in Turbo Pascal, multi-dimensional arrays
  896. and packed arrays are also supported:
  897. \input{syntax/typearr.syn}
  898. The following is a valid array declaration:
  899. \begin{verbatim}
  900. Type
  901. RealArray = Array [1..100] of Real;
  902. \end{verbatim}
  903. As in Turbo Pascal, if the array component type is in itself an array, it is
  904. possible to combine the two arrays into one multi-dimensional array. The
  905. following declaration:
  906. \begin{verbatim}
  907. Type
  908. APoints = array[1..100] of Array[1..3] of Real;
  909. \end{verbatim}
  910. is equivalent to the following declaration:
  911. \begin{verbatim}
  912. Type
  913. APoints = array[1..100,1..3] of Real;
  914. \end{verbatim}
  915. The functions \seef{High} and \seef{Low} return the high and low bounds of
  916. the leftmost index type of the array. In the above case, this would be 100
  917. and 1.
  918. \subsection{Record types}
  919. \fpc supports fixed records and records with variant parts.
  920. The syntax diagram for a record type is
  921. \input{syntax/typerec.syn}
  922. So the following are valid record types declarations:
  923. \begin{verbatim}
  924. Type
  925. Point = Record
  926. X,Y,Z : Real;
  927. end;
  928. RPoint = Record
  929. Case Boolean of
  930. False : (X,Y,Z : Real);
  931. True : (R,theta,phi : Real);
  932. end;
  933. BetterRPoint = Record
  934. Case UsePolar : Boolean of
  935. False : (X,Y,Z : Real);
  936. True : (R,theta,phi : Real);
  937. end;
  938. \end{verbatim}
  939. The variant part must be last in the record. The optional identifier in the
  940. case statement serves to access the tag field value, which otherwise would
  941. be invisible to the programmer. It can be used to see which variant is
  942. active at a certain time. In effect, it introduces a new field in the
  943. record.
  944. Remark that it is possible to nest variant parts, as in:
  945. \begin{verbatim}
  946. Type
  947. MyRec = Record
  948. X : Longint;
  949. Case byte of
  950. 2 : (Y : Longint;
  951. case byte of
  952. 3 : (Z : Longint);
  953. );
  954. end;
  955. \end{verbatim}
  956. The size of a record is the sum of the sizes of its fields, each size of a
  957. field is rounded up to two. If the record contains a variant part, the size
  958. of the variant part is the size of the biggest variant, plus the size of the
  959. tag field type {\em if an identifier was declared for it}. Here also, the size of
  960. each part is first rounded up to two. So in the above example,
  961. \seef{SizeOf} would return 24 for \var{Point}, 24 for \var{RPoint} and
  962. 26 for \var{BetterRPoint}. For \var{MyRec}, the value would be 12.
  963. If you want to read a typed file with records, produced by
  964. a Turbo Pascal program, then chances are that you will not succeed in
  965. reading that file correctly.
  966. The reason for this is that by default, elements of a record are aligned at
  967. 2-byte boundaries, for performance reasons. This default behaviour can be
  968. changed with the \var{\{\$PackRecords n\}} switch. Possible values for
  969. \var{n} are 1, 2, 4, 16 or \var{Default}.
  970. This switch tells the compiler to align elements of a record or object or
  971. class that have size larger than \var{n} on \var{n} byte boundaries.
  972. Elements that have size smaller or equal than \var{n} are aligned on
  973. natural boundaries, i.e. to the first power of two that is larger than or
  974. equal to the size of the record element.
  975. The keyword \var{Default} selects the default value for the platform
  976. you're working on (currently, this is 2 on all platforms)
  977. Take a look at the following program:
  978. \begin{verbatim}
  979. Program PackRecordsDemo;
  980. type
  981. {$PackRecords 2}
  982. Trec1 = Record
  983. A : byte;
  984. B : Word;
  985. end;
  986. {$PackRecords 1}
  987. Trec2 = Record
  988. A : Byte;
  989. B : Word;
  990. end;
  991. {$PackRecords 2}
  992. Trec3 = Record
  993. A,B : byte;
  994. end;
  995. {$PackRecords 1}
  996. Trec4 = Record
  997. A,B : Byte;
  998. end;
  999. {$PackRecords 4}
  1000. Trec5 = Record
  1001. A : Byte;
  1002. B : Array[1..3] of byte;
  1003. C : byte;
  1004. end;
  1005. {$PackRecords 8}
  1006. Trec6 = Record
  1007. A : Byte;
  1008. B : Array[1..3] of byte;
  1009. C : byte;
  1010. end;
  1011. {$PackRecords 4}
  1012. Trec7 = Record
  1013. A : Byte;
  1014. B : Array[1..7] of byte;
  1015. C : byte;
  1016. end;
  1017. {$PackRecords 8}
  1018. Trec8 = Record
  1019. A : Byte;
  1020. B : Array[1..7] of byte;
  1021. C : byte;
  1022. end;
  1023. Var rec1 : Trec1;
  1024. rec2 : Trec2;
  1025. rec3 : TRec3;
  1026. rec4 : TRec4;
  1027. rec5 : Trec5;
  1028. rec6 : TRec6;
  1029. rec7 : TRec7;
  1030. rec8 : TRec8;
  1031. begin
  1032. Write ('Size Trec1 : ',SizeOf(Trec1));
  1033. Writeln (' Offset B : ',Longint(@rec1.B)-Longint(@rec1));
  1034. Write ('Size Trec2 : ',SizeOf(Trec2));
  1035. Writeln (' Offset B : ',Longint(@rec2.B)-Longint(@rec2));
  1036. Write ('Size Trec3 : ',SizeOf(Trec3));
  1037. Writeln (' Offset B : ',Longint(@rec3.B)-Longint(@rec3));
  1038. Write ('Size Trec4 : ',SizeOf(Trec4));
  1039. Writeln (' Offset B : ',Longint(@rec4.B)-Longint(@rec4));
  1040. Write ('Size Trec5 : ',SizeOf(Trec5));
  1041. Writeln (' Offset B : ',Longint(@rec5.B)-Longint(@rec5),
  1042. ' Offset C : ',Longint(@rec5.C)-Longint(@rec5));
  1043. Write ('Size Trec6 : ',SizeOf(Trec6));
  1044. Writeln (' Offset B : ',Longint(@rec6.B)-Longint(@rec6),
  1045. ' Offset C : ',Longint(@rec6.C)-Longint(@rec6));
  1046. Write ('Size Trec7 : ',SizeOf(Trec7));
  1047. Writeln (' Offset B : ',Longint(@rec7.B)-Longint(@rec7),
  1048. ' Offset C : ',Longint(@rec7.C)-Longint(@rec7));
  1049. Write ('Size Trec8 : ',SizeOf(Trec8));
  1050. Writeln (' Offset B : ',Longint(@rec8.B)-Longint(@rec8),
  1051. ' Offset C : ',Longint(@rec8.C)-Longint(@rec8));
  1052. end.
  1053. \end{verbatim}
  1054. The output of this program will be :
  1055. \begin{verbatim}
  1056. Size Trec1 : 4 Offset B : 2
  1057. Size Trec2 : 3 Offset B : 1
  1058. Size Trec3 : 2 Offset B : 1
  1059. Size Trec4 : 2 Offset B : 1
  1060. Size Trec5 : 8 Offset B : 4 Offset C : 7
  1061. Size Trec6 : 8 Offset B : 4 Offset C : 7
  1062. Size Trec7 : 12 Offset B : 4 Offset C : 11
  1063. Size Trec8 : 16 Offset B : 8 Offset C : 15
  1064. \end{verbatim}
  1065. And this is as expected. In \var{Trec1}, since \var{B} has size 2, it is
  1066. aligned on a 2 byte boundary, thus leaving an empty byte between \var{A}
  1067. and \var{B}, and making the total size 4. In \var{Trec2}, \var{B} is aligned
  1068. on a 1-byte boundary, right after \var{A}, hence, the total size of the
  1069. record is 3.
  1070. For \var{Trec3}, the sizes of \var{A,B} are 1, and hence they are aligned on 1
  1071. byte boundaries. The same is true for \var{Trec4}.
  1072. For \var{Trec5}, since the size of B -- 3 -- is smaller than 4, \var{B} will
  1073. be on a 4-byte boundary, as this is the first power of two that is
  1074. larger than it's size. The same holds for \var{Trec6}.
  1075. For \var{Trec7}, \var{B} is aligned on a 4 byte boundary, since it's size --
  1076. 7 -- is larger than 4. However, in \var{Trec8}, it is aligned on a 8-byte
  1077. boundary, since 8 is the first power of two that is greater than 7, thus
  1078. making the total size of the record 16.
  1079. As from version 0.9.3, \fpc supports also the 'packed record', this is a
  1080. record where all the elements are byte-aligned.
  1081. Thus the two following declarations are equivalent:
  1082. \begin{verbatim}
  1083. {$PackRecords 1}
  1084. Trec2 = Record
  1085. A : Byte;
  1086. B : Word;
  1087. end;
  1088. {$PackRecords 2}
  1089. \end{verbatim}
  1090. and
  1091. \begin{verbatim}
  1092. Trec2 = Packed Record
  1093. A : Byte;
  1094. B : Word;
  1095. end;
  1096. \end{verbatim}
  1097. Note the \var{\{\$PackRecords 2\}} after the first declaration !
  1098. \subsection{Set types}
  1099. \fpc supports the set types as in Turbo Pascal. The prototype of a set
  1100. declaration is:
  1101. \input{syntax/typeset.syn}
  1102. Each of the elements of \var{SetType} must be of type \var{TargetType}.
  1103. \var{TargetType} can be any ordinal type with a range between \var{0} and
  1104. \var{255}. A set can contain maximally \var{255} elements.
  1105. The following are valid set declaration:
  1106. \begin{verbatim}
  1107. Type
  1108. Junk = Set of Char;
  1109. Days = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
  1110. WorkDays : Set of days;
  1111. \end{verbatim}
  1112. Given this set declarations, the following assignment is legal:
  1113. \begin{verbatim}
  1114. WorkDays := [ Mon, Tue, Wed, Thu, Fri];
  1115. \end{verbatim}
  1116. The operators and functions for manipulations of sets are listed in
  1117. \seet{SetOps}.
  1118. \begin{FPCltable}{lr}{Set Manipulation operators}{SetOps}
  1119. Operation & Operator \\ \hline
  1120. Union & + \\
  1121. Difference & - \\
  1122. Intersection & * \\
  1123. Add element & \var{include} \\
  1124. Delete element & \var{exclude} \\ \hline
  1125. \end{FPCltable}
  1126. You can compare two sets with the \var{<>} and \var{=} operators, but not
  1127. (yet) with the \var{<} and \var{>} operators.
  1128. As of compiler version 0.9.5, the compiler stores small sets (less than 32
  1129. elements) in a Longint, if the type range allows it. This allows for faster
  1130. processing and decreases program size. Otherwise, sets are stored in 32
  1131. bytes.
  1132. \subsection{File types}
  1133. File types are types that store a sequence of some base type, which can be
  1134. any type except another file type. It can contain (in principle) an infinite
  1135. number of elements.
  1136. File types are used commonly to store data on disk. Nothing stops you,
  1137. however, from writing a file driver that stores it's data in memory.
  1138. Here is the type declaration for a file type:
  1139. \input{syntax/typefil.syn}
  1140. If no type identifier is given, then the file is an untyped file; it can be
  1141. considered as equivalent to a file of bytes. Untyped files require special
  1142. commands to act on them (see \seep{Blockread}, \seep{Blockwrite}).
  1143. The following declaration declares a file of records:
  1144. \begin{verbatim}
  1145. Type
  1146. Point = Record
  1147. X,Y,Z : real;
  1148. end;
  1149. PointFile = File of Point;
  1150. \end{verbatim}
  1151. Internally, files are represented by the \var{FileRec} record.
  1152. See \seec{refchapter} for it's declaration.
  1153. A special file type is the \var{Text} file type, represented by the
  1154. \var{TextRec} record. A file of type \var{Text} uses special input-output
  1155. routines.
  1156. \section{Pointers}
  1157. \fpc supports the use of pointers. A variable of the pointer type
  1158. contains an address in memory, where the data of another variable may be
  1159. stored.
  1160. \input{syntax/typepoin.syn}
  1161. As can be seen from this diagram, pointers are typed, which means that
  1162. they point to a particular kind of data. The type of this data must be
  1163. known at compile time.
  1164. Dereferencing the pointer (denoted by adding \var{\^{}} after the variable
  1165. name) behaves then like a variable. This variable has the type declared in
  1166. the pointer declaration, and the variable is stored in the address that is
  1167. pointed to by the pointer variable.
  1168. Consider the following example:
  1169. \begin{verbatim}
  1170. Program pointers;
  1171. type
  1172. Buffer = String[255];
  1173. BufPtr = ^Buffer;
  1174. Var B : Buffer;
  1175. BP : BufPtr;
  1176. PP : Pointer;
  1177. etc..
  1178. \end{verbatim}
  1179. In this example, \var{BP} {\em is a pointer to} a \var{Buffer} type; while \var{B}
  1180. {\em is} a variable of type \var{Buffer}. \var{B} takes 256 bytes memory,
  1181. and \var{BP} only takes 4 bytes of memory (enough to keep an adress in
  1182. memory).
  1183. {\em Remark:} \fpc treats pointers much the same way as C does. This means
  1184. that you can treat a pointer to some type as being an array of this type.
  1185. The pointer then points to the zeroeth element of this array. Thus the
  1186. following pointer declaration
  1187. \begin{verbatim}
  1188. Var p : ^Longint;
  1189. \end{verbatim}
  1190. Can be considered equivalent to the following array declaration:
  1191. \begin{verbatim}
  1192. Var p : array[0..Infinity] of Longint;
  1193. \end{verbatim}
  1194. The difference is that the former declaration allocates memory for the
  1195. pointer only (not for the array), and the second declaration allocates
  1196. memory for the entire array. If you use the former, you must allocate memory
  1197. yourself, using the \seep{Getmem} function.
  1198. The reference \var{P\^{}} is then the same as \var{p[0]}. The following program
  1199. illustrates this maybe more clear:
  1200. \begin{verbatim}
  1201. program PointerArray;
  1202. var i : Longint;
  1203. p : ^Longint;
  1204. pp : array[0..100] of Longint;
  1205. begin
  1206. for i := 0 to 100 do pp[i] := i; { Fill array }
  1207. p := @pp[0]; { Let p point to pp }
  1208. for i := 0 to 100 do
  1209. if p[i]<>pp[i] then
  1210. WriteLn ('Ohoh, problem !')
  1211. end.
  1212. \end{verbatim}
  1213. \fpc supports pointer arithmetic as C does. This means that, if \var{P} is a
  1214. typed pointer, the instructions
  1215. \begin{verbatim}
  1216. Inc(P);
  1217. Dec(P);
  1218. \end{verbatim}
  1219. Will increase, respectively descrease the address the pointer points to
  1220. with the size of the type \var{P} is a pointer to. For example
  1221. \begin{verbatim}
  1222. Var P : ^Longint;
  1223. ...
  1224. Inc (p);
  1225. \end{verbatim}
  1226. will increase \var{P} with 4.
  1227. You can also use normal arithmetic operators on pointers, that is, the
  1228. following are valid pointer arithmetic operations:
  1229. \begin{verbatim}
  1230. var p1,p2 : ^Longint;
  1231. L : Longint;
  1232. begin
  1233. P1 := @P2;
  1234. P2 := @L;
  1235. L := P1-P2;
  1236. P1 := P1-4;
  1237. P2 := P2+4;
  1238. end.
  1239. \end{verbatim}
  1240. Here, the value that is added or substracted is {\em not} multiplied by the
  1241. size of the type the pointer points to.
  1242. \section{Procedural types}
  1243. \fpc has support for procedural types, although it differs a little from
  1244. the Turbo Pascal implementation of them. The type declaration remains the
  1245. same, as can be seen in the following syntax diagram:
  1246. \input{syntax/typeproc.syn}
  1247. For a description of formal parameter lists, see \seec{Procedures}.
  1248. The two following examples are valid type declarations:
  1249. \begin{verbatim}
  1250. Type TOneArg = Procedure (Var X : integer);
  1251. TNoArg = Function : Real;
  1252. var proc : TOneArg;
  1253. func : TNoArg;
  1254. \end{verbatim}
  1255. One can assign the following values to a procedural type variable:
  1256. \begin{enumerate}
  1257. \item \var{Nil}, for both normal procedure pointers and method pointers.
  1258. \item A variable reference of a procedural type, i.e. another variable of
  1259. the same type.
  1260. \item A global procedure or function address, with matching function or
  1261. procedure header and calling convention.
  1262. \item A method address.
  1263. \end{enumerate}
  1264. Given these declarations, the following assignments are valid:
  1265. \begin{verbatim}
  1266. Procedure printit (Var X : Integer);
  1267. begin
  1268. WriteLn (x);
  1269. end;
  1270. ...
  1271. P := @printit;
  1272. Func := @Pi;
  1273. \end{verbatim}
  1274. From this example, the difference with Turbo Pascal is clear: In Turbo
  1275. Pascal it isn't necessary to use the address operator (\var{@})
  1276. when assigning a procedural type variable, whereas in \fpc it is required
  1277. (unless you use the \var{-So} switch, in which case you can drop the address
  1278. operator.)
  1279. Remark that the modifiers concerning the calling conventions (\var{cdecl},
  1280. \var{pascal}, \var{stdcall} and \var{popstack} stick to the declaration;
  1281. i.e. the following code would give an error:
  1282. \begin{verbatim}
  1283. Type TOneArgCcall = Procedure (Var X : integer);cdecl;
  1284. var proc : TOneArgCcall;
  1285. Procedure printit (Var X : Integer);
  1286. begin
  1287. WriteLn (x);
  1288. end;
  1289. begin
  1290. P := @printit;
  1291. end.
  1292. \end{verbatim}
  1293. Because the \var{TOneArgCcall} type is a procedure that uses the cdecl
  1294. calling convention.
  1295. At the moment, the method procedural pointers (i.e. pointers that point to
  1296. methods of objects, distinguished by the \var{of object} keywords in the
  1297. declaration) are still in an experimental stage.
  1298. \chapter{Objects}
  1299. \section{Declaration}
  1300. \fpc supports object oriented programming. In fact, most of the compiler is
  1301. written using objects. Here we present some technical questions regarding
  1302. object oriented programming in \fpc.
  1303. Objects should be treated as a special kind of record. The record contains
  1304. all the fields that are declared in the objects definition, and pointers
  1305. to the methods that are associated to the objects' type.
  1306. An object is declared just as you would declare a record; except that you
  1307. can now declare procedures and fuctions as if they were part of the record.
  1308. Objects can ''inherit'' fields and methods from ''parent'' objects. This means
  1309. that you can use these fields and methods as if they were included in the
  1310. objects you declared as a ''child'' object.
  1311. Furthermore, you can declare fields, procedures and functions as \var{public}
  1312. or \var{private}. By default, fields and methods are \var{public}, and are
  1313. exported outside the current unit. Fields or methods that are declared
  1314. \var{private} are only accessible in the current unit.
  1315. The prototype declaration of an object is as follows:
  1316. \input{syntax/typeobj.syn}
  1317. As you can see, you can repeat as many \var{private} and \var{public}
  1318. blocks as you want.
  1319. \var{Method definitions} are normal function or procedure declarations.
  1320. You cannot put fields after methods in the same block, i.e. the following
  1321. will generate an error when compiling:
  1322. \begin{verbatim}
  1323. Type MyObj = Object
  1324. Procedure Doit;
  1325. Field : Longint;
  1326. end;
  1327. \end{verbatim}
  1328. But the following will be accepted:
  1329. \begin{verbatim}
  1330. Type MyObj = Object
  1331. Public
  1332. Procedure Doit;
  1333. Private
  1334. Field : Longint;
  1335. end;
  1336. \end{verbatim}
  1337. because the field is in a different section.
  1338. {\em Remark:}
  1339. \fpc also supports the packed object. This is the same as an object, only
  1340. the elements (fields) of the object are byte-aligned, just as in the packed
  1341. record.
  1342. The declaration of a packed object is similar to the declaration
  1343. of a packed record :
  1344. \begin{verbatim}
  1345. Type
  1346. TObj = packed object;
  1347. Constructor init;
  1348. ...
  1349. end;
  1350. Pobj = ^TObj;
  1351. Var PP : Pobj;
  1352. \end{verbatim}
  1353. Similarly, the \var{\{\$PackRecords \}} directive acts on objects as well.
  1354. \section{Fields}
  1355. Object Fields are like record fields. They are accessed in the same way as
  1356. you would access a record field : by using a qualified identifier. Given the
  1357. following declaration:
  1358. \begin{verbatim}
  1359. Type TAnObject = Object
  1360. AField : Longint;
  1361. Procedure AMethod;
  1362. end;
  1363. Var AnObject : TAnObject;
  1364. \end{verbatim}
  1365. then the following would be a valid assignment:
  1366. \begin{verbatim}
  1367. AnObject.AField := 0;
  1368. \end{verbatim}
  1369. Inside methods, fields can be accessed using the short identifier:
  1370. \begin{verbatim}
  1371. Procedure TAnObject.AMethod;
  1372. begin
  1373. ...
  1374. AField := 0;
  1375. ...
  1376. end;
  1377. \end{verbatim}
  1378. Or, one can use the \var{self} identifier. The \var{self} identifier refers
  1379. to the current instance of the object:
  1380. \begin{verbatim}
  1381. Procedure TAnObject.AMethod;
  1382. begin
  1383. ...
  1384. Self.AField := 0;
  1385. ...
  1386. end;
  1387. \end{verbatim}
  1388. You cannot access fields that are in a private section of an object from
  1389. outside the objects' methods. If you do, the compiler will complain about
  1390. an unknown identifier.
  1391. It is also possible to use the \var{with} statement with an object instance:
  1392. \begin{verbatim}
  1393. With AnObject do
  1394. begin
  1395. Afield := 12;
  1396. AMethod;
  1397. end;
  1398. \end{verbatim}
  1399. In this example, between the \var{begin} and \var{end}, it is as if
  1400. \var{AnObject} was prepended to the \var{Afield} and \var{Amethod}
  1401. identifiers. More about this in \sees{With}
  1402. \section{Constructors and destructors }
  1403. \label{se:constructdestruct}
  1404. As can be seen in the syntax diagram for an object declaration, \fpc supports
  1405. constructors and destructors. You are responsible for calling the
  1406. constructor and the destructor explicitly when using objects.
  1407. The declaration of a constructor or destructor is as follows:
  1408. \input{syntax/construct.syn}
  1409. A constructor/destructor pair is {\em required} if you use virtual methods.
  1410. In the declaration of the object type, you should use a simple identifier
  1411. for the name of the constuctor or destructor. When you implement the
  1412. constructor or destructor, you should use a qulified method identifier,
  1413. i.e. an identifier of the form \var{objectidentifier.methodidentifier}.
  1414. \fpc supports also the extended syntax of the \var{New} and \var{Dispose}
  1415. procedures. In case you want to allocate a dynamic variable of an object
  1416. type, you can specify the constructor's name in the call to \var{New}.
  1417. The \var{New} is implemented as a function which returns a pointer to the
  1418. instantiated object. Consider the following declarations:
  1419. \begin{verbatim}
  1420. Type
  1421. TObj = object;
  1422. Constructor init;
  1423. ...
  1424. end;
  1425. Pobj = ^TObj;
  1426. Var PP : Pobj;
  1427. \end{verbatim}
  1428. Then the following 3 calls are equivalent:
  1429. \begin{verbatim}
  1430. pp := new (Pobj,Init);
  1431. \end{verbatim}
  1432. and
  1433. \begin{verbatim}
  1434. new(pp,init);
  1435. \end{verbatim}
  1436. and also
  1437. \begin{verbatim}
  1438. new (pp);
  1439. pp^.init;
  1440. \end{verbatim}
  1441. In the last case, the compiler will issue a warning that you should use the
  1442. extended syntax of \var{new} and \var{dispose} to generate instances of an
  1443. object. You can ignore this warning, but it's better programming practice to
  1444. use the extended syntax to create instances of an object.
  1445. Similarly, the \var{Dispose} procedure accepts the name of a destructor. The
  1446. destructor will then be called, before removing the object from the heap.
  1447. In view of the compiler warning remark, the now following Delphi approach may
  1448. be considered a more natural way of object-oriented programming.
  1449. \section{Methods}
  1450. Object methods are just like ordinary procedures or functions, only they
  1451. have an implicit extra parameter : \var{self}. Self points to the object
  1452. with which the method was invoked.
  1453. When implementing methods, the fully qualified identifier must be given
  1454. in the function header. When declaring methods, a normal identifier must be
  1455. given.
  1456. \section{Method invocation}
  1457. Methods are called just as normal procedures are called, only they have a
  1458. object instance identifier prepended to them (see also \seec{Statements}).
  1459. To determine which method is called, it is necessary to know the type of
  1460. the method. We treat the different types in what follows.
  1461. \subsubsection{Static methods}
  1462. Static methods are methods that have been declared without a \var{abstract}
  1463. or \var{virtual} keyword. When calling a static method, the declared (i.e.
  1464. compile time) method of the object is used.
  1465. For example, consider the following declarations:
  1466. \begin{verbatim}
  1467. Type
  1468. TParent = Object
  1469. ...
  1470. procedure Doit;
  1471. ...
  1472. end;
  1473. PParent = ^TParent;
  1474. TChild = Object(TParent)
  1475. ...
  1476. procedure Doit;
  1477. ...
  1478. end;
  1479. PChild = ^TChild;
  1480. \end{verbatim}
  1481. As it is visible, both the parent and child objects have a method called
  1482. \var{Doit}. Consider now the following declarations and calls:
  1483. \begin{verbatim}
  1484. Var ParentA,ParentB : PParent;
  1485. Child : PChild;
  1486. ParentA := New(PParent,Init);
  1487. ParentB := New(PChild,Init);
  1488. Child := New(PChild,Init);
  1489. ParentA^.Doit;
  1490. ParentB^.Doit;
  1491. Child^.Doit;
  1492. \end{verbatim}
  1493. Of the three invocations of \var{Doit}, only the last one will call
  1494. \var{TChild.Doit}, the other two calls will call \var{TParent.Doit}.
  1495. This is because for static methods, the compiler determines at compile
  1496. time which method should be called. Since \var{ParentB} is of type
  1497. \var{TParent}, the compiler decides that it must be called with
  1498. \var{TParent.Doit}, even though it will be created as a \var{TChild}.
  1499. There may be times when you want the method that is actually called to
  1500. depend on the actual type of the object at run-time. If so, the method
  1501. cannot be a static method, but must be a virtual method.
  1502. \subsubsection{Virtual methods}
  1503. To remedy the situation in the previous section, \var{virtual} methods are
  1504. created. This is simply done by appending the method declaration with the
  1505. \var{virtual} modifier.
  1506. Going back to the previous example, consider the following alternative
  1507. declaration:
  1508. \begin{verbatim}
  1509. Type
  1510. TParent = Object
  1511. ...
  1512. procedure Doit;virtual;
  1513. ...
  1514. end;
  1515. PParent = ^TParent;
  1516. TChild = Object(TParent)
  1517. ...
  1518. procedure Doit;virtual;
  1519. ...
  1520. end;
  1521. PChild = ^TChild;
  1522. \end{verbatim}
  1523. As it is visible, both the parent and child objects have a method called
  1524. \var{Draw}. Consider now the following declarations and calls :
  1525. \begin{verbatim}
  1526. Var ParentA,ParentB : PParent;
  1527. Child : PChild;
  1528. ParentA := New(PParent,Init);
  1529. ParentB := New(PChild,Init);
  1530. Child := New(PChild,Init);
  1531. ParentA^.Doit;
  1532. ParentB^.Doit;
  1533. Child^.Doit;
  1534. \end{verbatim}
  1535. Now, different methods will be called, depending on the actual run-time type
  1536. of the object. For \var{ParentA}, nothing changes, since it is created as
  1537. a \var{TParent} instance. For \var{Child}, the situation also doesn't
  1538. change: it is again created as an instance of \var{TChild}.
  1539. For \var{ParentB} however, the situation does change: Even though it was
  1540. declared as a \var{TParent}, it is created as an instance of \var{TChild}.
  1541. Now, when the program runs, before calling \var{Doit}, the program
  1542. checks what the actual type of \var{ParentB} is, and only then decides which
  1543. method must be called. Seeing that \var{ParentB} is of type \var{TChild},
  1544. \var{TChild.Doit} will be called.
  1545. The code for this run-time checking of the actual type of an object is
  1546. inserted by the compiler at compile time.
  1547. The \var{TChild.Doit} is said to {\em override} the \var{TParent.Doit}.
  1548. It is possible to acces the \var{TParent.Doit} from within the
  1549. var{TChild.Doit}, with the \var{inherited} keyword:
  1550. \begin{verbatim}
  1551. Procedure TChild.Doit;
  1552. begin
  1553. inherited Doit;
  1554. ...
  1555. end;
  1556. \end{verbatim}
  1557. In the above example, when \var{TChild.Doit} is called, the first thing it
  1558. does is call \var{TParent.Doit}. You cannot use the inherited keyword on
  1559. static methods, only on virtual methods.
  1560. \subsubsection{Abstract methods}
  1561. An abstract method is a special kind of virtual method. A method can not be
  1562. abstract if it is not virtual (this is not obvious from the syntax diagram).
  1563. You cannot create an instance of an object that has an abstract method.
  1564. The reason is obvious: there is no method where the compiler could jump to !
  1565. A method that is declared \var{abstract} does not have an implementation for
  1566. this method. It is up to inherited objects to override and implement this
  1567. method. Continuing our example, take a look at this:
  1568. \begin{verbatim}
  1569. Type
  1570. TParent = Object
  1571. ...
  1572. procedure Doit;virtual;abstract;
  1573. ...
  1574. end;
  1575. PParent=^TParent;
  1576. TChild = Object(TParent)
  1577. ...
  1578. procedure Doit;virtual;
  1579. ...
  1580. end;
  1581. PChild = ^TChild;
  1582. \end{verbatim}
  1583. As it is visible, both the parent and child objects have a method called
  1584. \var{Draw}. Consider now the following declarations and calls :
  1585. \begin{verbatim}
  1586. Var ParentA,ParentB : PParent;
  1587. Child : PChild;
  1588. ParentA := New(PParent,Init);
  1589. ParentB := New(PChild,Init);
  1590. Child := New(PChild,Init);
  1591. ParentA^.Doit;
  1592. ParentB^.Doit;
  1593. Child^.Doit;
  1594. \end{verbatim}
  1595. First of all, Line 4 will generate a compiler error, stating that you cannot
  1596. generate instances of objects with abstract methods: The compiler has
  1597. detected that \var{PParent} points to an object which has an abstract
  1598. method. Commenting line 4 would allow compilation of the program.
  1599. Remark that if you override an abstract method, you cannot call the parent
  1600. method with \var{inherited}, since there is no parent method; The compiler
  1601. will detect this, and complain about it, like this:
  1602. \begin{verbatim}
  1603. testo.pp(32,3) Error: Abstract methods can't be called directly
  1604. \end{verbatim}
  1605. If, through some mechanism, an abstract method is called at run-time,
  1606. then a run-time error will occur. (run-time error 211, to be precise)
  1607. \section{Visibility}
  1608. For objects, only 2 visibility specifiers exist : \var{private} and
  1609. \var{public}. If you don't specify a visibility specifier, \var{public}
  1610. is assumed.
  1611. Both methods and fields can be hidden from a programmer by putting them
  1612. in a \var{private} section. The exact visibility rule is as follows:
  1613. \begin{description}
  1614. \item [Private\ ] All fields and methods that are in a \var{private} block,
  1615. can only be accessed in the module (i.e. unit or program) that contains
  1616. the object definition.
  1617. They can be accessed from inside the object's methods or from outside them
  1618. e.g. from other objects' methods, or global functions.
  1619. \item [Public\ ] sections are always accessible, from everywhere.
  1620. Fields and metods in a \var{public} section behave as though they were part
  1621. of an ordinary \var{record} type.
  1622. \end{description}
  1623. \chapter{Classes}
  1624. In the Delphi approach to Object Oriented Programming, everything revolves
  1625. around the concept of 'Classes'. A class can be seen as a pointer to an
  1626. object, or a pointer to a record.
  1627. In order to use classes, it is necessary to put the \file{objpas} unit in the
  1628. uses clause of your unit or program. This unit contains the basic
  1629. definitions of \var{TObject} and \var{TClass}, as well as some auxiliary
  1630. methods for using classes.
  1631. \section{Class definitions}
  1632. The prototype declaration of a class is as follows :
  1633. \input{syntax/typeclas.syn}
  1634. Again, You can repeat as many \var{private}, \var{protected}, \var{published}
  1635. and \var{public} blocks as you want.
  1636. Methods are normal function or procedure declarations.
  1637. As you can see, the declaration of a class is almost identical to the
  1638. declaration of an object. The real difference between objects and classes
  1639. is in the way they are created (see further in this chapter).
  1640. The visibility of the different sections is as follows:
  1641. \begin{description}
  1642. \item [Private\ ] All fields and methods that are in a \var{private} block, can
  1643. only be accessed in the module (i.e. unit) that contains the class definition.
  1644. They can be accessed from inside the classes' methods or from outside them
  1645. (e.g. from other classes' methods)
  1646. \item [Protected\ ] Is the same as \var{Private}, except that the members of
  1647. a \var{Protected} section are also accessible to descendent types, even if
  1648. they are implemented in other modules.
  1649. \item [Public\ ] sections are always accessible.
  1650. \item [Published\ ] Is the same as a \var{Public} section, but the compiler
  1651. generates also type information that is needed for automatic streaming of
  1652. these classes. Fields defined in a \var{published} section must be of class type.
  1653. Array peroperties cannot be in a \var{published} section.
  1654. \end{description}
  1655. \section{Class instantiation}
  1656. Classes must be created using their constructor. Remember that a class is a
  1657. pointer to an object, so when you declare a variable of some class, the
  1658. compiler just allocates a pointer, not the entire object. The constructor of
  1659. a class returns a pointer to an initialized instance of the object.
  1660. So, to initialize an instance of some class, you would do the following :
  1661. \begin{verbatim}
  1662. ClassVar := ClassType.ConstructorName;
  1663. \end{verbatim}
  1664. You cannot use the extended syntax of \var{new} and \var{dispose} to
  1665. instantiate and destroy class instances.
  1666. That construct is reserved for use with objects only.
  1667. Calling the constructor will provoke a call to \var{getmem}, to allocate
  1668. enough space to hold the class instance data.
  1669. After that, the constuctor's code is executed.
  1670. The constructor has a pointer to it's data, in \var{self}.
  1671. {\em Remark :}
  1672. \begin{itemize}
  1673. \item The \var{\{\$PackRecords \}} directive also affects classes.
  1674. i.e. the alignment in memory of the different fields depends on the
  1675. value of the \var{\{\$PackRecords \}} directive.
  1676. \item Just as for objects and records, you can declare a packed class.
  1677. This has the same effect as on an object, or record, namely that the
  1678. elements are aligned on 1-byte boundaries. i.e. as close as possible.
  1679. \item \var{SizeOf(class)} will return 4, since a class is but a pointer to
  1680. an object. To get the size of the class instance data, use the
  1681. \var{TObject.InstanceSize} method.
  1682. \end{itemize}
  1683. \section{Methods}
  1684. Method invocation for classes is no different than for objects. The
  1685. following is a valid method invocation:
  1686. \begin{verbatim}
  1687. Var AnObject : TAnObject;
  1688. begin
  1689. AnObject := TAnObject.Create;
  1690. ANobject.AMethod;
  1691. \end{verbatim}
  1692. \section{Properties}
  1693. Classes can contain properties as part of their fields list. A property
  1694. acts like a normal field, i.e. you can get or set it's value, but
  1695. allows to redirect the access of the field through functions and
  1696. procedures. They provide a means to assiciate an action with an assignment
  1697. of or a reading from a class 'field'. This allows for e.g. checking that a
  1698. value is valid when assigning, or, when reading, it allows to construct the
  1699. value on the fly. Moreover, properties can be read-only or write only.
  1700. The prototype declaration of a property is as follows:
  1701. \input{syntax/property.syn}
  1702. A \var{read specifier} is either the name of a field that contains the
  1703. property, or the name of a method function that has the same return type as
  1704. the property type. In the case of a simple type, this
  1705. function must not accept an argument. A read specifier is optional, making
  1706. the property write-only.
  1707. A \var{write specifier} is optional: If there is no write specifier, the
  1708. property is read-only. A write specifier is either the name of a field, or
  1709. the name of a method procedure that accepts as a sole argument a variable of
  1710. the same type as the property.
  1711. The section (\var{private}, \var{published} in which the specified function or
  1712. procedure resides is irrelevant. Usually, however, this will be a protected
  1713. or private method.
  1714. Example:
  1715. Given the following declaration:
  1716. \begin{verbatim}
  1717. Type
  1718. MyClass = Class
  1719. Private
  1720. Field1 : Longint;
  1721. Field2 : Longint;
  1722. Field3 : Longint;
  1723. Procedure Sety (value : Longint);
  1724. Function Gety : Longint;
  1725. Function Getz : Longint;
  1726. Public
  1727. Property X : Longint Read Field1 write Field2;
  1728. Property Y : Longint Read GetY Write Sety;
  1729. Property Z : Longint Read GetZ;
  1730. end;
  1731. Var MyClass : TMyClass;
  1732. \end{verbatim}
  1733. The following are valid statements:
  1734. \begin{verbatim}
  1735. WriteLn ('X : ',MyClass.X);
  1736. WriteLn ('Y : ',MyClass.Y);
  1737. WriteLn ('Z : ',MyClass.Z);
  1738. MyClass.X := 0;
  1739. MyClass.Y := 0;
  1740. \end{verbatim}
  1741. But the following would generate an error:
  1742. \begin{verbatim}
  1743. MyClass.Z := 0;
  1744. \end{verbatim}
  1745. because Z is a read-only property.
  1746. What happens in the above statements is that when a value needs to be read,
  1747. the compiler inserts a call to the various \var{getNNN} methods of the
  1748. object, and the result of this call is used. When an assignment is made,
  1749. the compiler passes the value that must be assigned as a paramater to
  1750. the various \var{setNNN} methods.
  1751. Because of this mechanism, properties cannot be passed as var arguments to a
  1752. function or procedure, since there is no known address of the property (at
  1753. least, not always).
  1754. If the property definition contains an index, then the read and write
  1755. specifiers must be a function and a procedure. Moreover, these functions
  1756. require an additional parameter : An integer parameter. This allows to read
  1757. or write several properties with the same function. For this, the properties
  1758. must have the same type.
  1759. The following is an example of a property with an index:
  1760. \begin{verbatim}
  1761. uses objpas;
  1762. Type TPoint = Class(TObject)
  1763. Private
  1764. FX,FY : Longint;
  1765. Function GetCoord (Index : Integer): Longint;
  1766. Procedure SetCoord (Index : Integer; Value : longint);
  1767. Public
  1768. Property X : Longint index 1 read GetCoord Write SetCoord;
  1769. Property Y : Longint index 2 read GetCoord Write SetCoord;
  1770. Property Coords[Index : Integer] Read GetCoord;
  1771. end;
  1772. Procedure TPoint.SetCoord (Index : Integer; Value : Longint);
  1773. begin
  1774. Case Index of
  1775. 1 : FX := Value;
  1776. 2 : FY := Value;
  1777. end;
  1778. end;
  1779. Function TPoint.GetCoord (INdex : Integer) : Longint;
  1780. begin
  1781. Case Index of
  1782. 1 : Result := FX;
  1783. 2 : Result := FY;
  1784. end;
  1785. end;
  1786. Var P : TPoint;
  1787. begin
  1788. P := TPoint.create;
  1789. P.X := 2;
  1790. P.Y := 3;
  1791. With P do
  1792. WriteLn ('X=',X,' Y=',Y);
  1793. end.
  1794. \end{verbatim}
  1795. When the compiler encounters an assignment to \var{X}, then \var{SetCoord}
  1796. is called with as first parameter the index (1 in the above case) and with
  1797. as a second parameter the value to be set.
  1798. Conversely, when reading the value of \var{X}, the compiler calls
  1799. \var{GetCoord} and passes it index 1.
  1800. Indexes can only be integer values.
  1801. You can also have array properties. These are properties that accept an
  1802. index, just as an array does. Only now the index doesn't have to be an
  1803. ordinal type, but can be any type.
  1804. A \var{read specifier} for an array property is the name method function
  1805. that has the same return type as the property type.
  1806. The function must accept as a sole arguent a variable of the same type as
  1807. the index type. For an array property, you cannot specify fields as read
  1808. specifiers.
  1809. A \var{write specifier} for an array property is the name of a method
  1810. procedure that accepts two arguments: The first argument has the same
  1811. type as the index, and the second argument is a parameter of the same
  1812. type as the property type.
  1813. As an example, see the following declaration:
  1814. \begin{verbatim}
  1815. Type TIntList = Class
  1816. Private
  1817. Function GetInt (I : Longint) : longint;
  1818. Function GetAsString (A : String) : String;
  1819. Procedure SetInt (I : Longint; Value : Longint;);
  1820. Procedure SetAsString (A : String; Value : String);
  1821. Public
  1822. Property Items [i : Longint] : Longint Read GetInt
  1823. Write SetInt;
  1824. Property StrItems [S : String] : String Read GetAsString
  1825. Write SetAsstring;
  1826. end;
  1827. Var AIntList : TIntList;
  1828. \end{verbatim}
  1829. Then the following statements would be valid:
  1830. \begin{verbatim}
  1831. AIntList.Items[26] := 1;
  1832. AIntList.StrItems['twenty-five'] := 'zero';
  1833. WriteLn ('Item 26 : ',AIntList.Items[26]);
  1834. WriteLn ('Item 25 : ',AIntList.StrItems['twenty-five']);
  1835. \end{verbatim}
  1836. While the following statements would generate errors:
  1837. \begin{verbatim}
  1838. AIntList.Items['twenty-five'] := 1;
  1839. AIntList.StrItems[26] := 'zero';
  1840. \end{verbatim}
  1841. Because the index types are wrong.
  1842. Array properties can be declared as \var{default} properties. This means that
  1843. it is not necessary to specify the property name when assigning or reading
  1844. it. If, in the previous example, the definition of the items property would
  1845. have been
  1846. \begin{verbatim}
  1847. Property Items[i : Longint]: Longint Read GetInt
  1848. Write SetInt; Default;
  1849. \end{verbatim}
  1850. Then the assignment
  1851. \begin{verbatim}
  1852. AIntList.Items[26] := 1;
  1853. \end{verbatim}
  1854. Would be equivalent to the following abbreviation.
  1855. \begin{verbatim}
  1856. AIntList[26] := 1;
  1857. \end{verbatim}
  1858. You can have only one default property per class, and descendent classes
  1859. cannot redeclare the default property.
  1860. \chapter{Expressions}
  1861. \label{ch:Expressions}
  1862. Expressions occur in assignments or in tests. Expressions produce a value,
  1863. of a certain type.
  1864. Expressions are built with two components: Operators and their operands.
  1865. Usually an operator is binary, i.e. it requires 2 operands. Binary operators
  1866. occur always between the operands (as in \var{X/Y}). Sometimes an
  1867. operator is unary, i.e. it requires only one argument. A unary operator
  1868. occurs always before the operand, as in \var{-X}.
  1869. When using multiple operands in an expression, the precedence rules of
  1870. \seet{OperatorPrecedence} are used.
  1871. \begin{FPCltable}{lll}{Precedence of operators}{OperatorPrecedence}
  1872. Operator & Precedence & Category \\ \hline
  1873. \var{Not, @} & Highest & Unary operators\\
  1874. \var{* / div mod and shl shr as} & Second & Multiplying operators\\
  1875. \var{+ - or xor} & Third & Adding operators \\
  1876. \var{< <> < > <= >= in is} & Lowest (Fourth) & relational operators \\
  1877. \hline
  1878. \end{FPCltable}
  1879. When determining the precedence, te compiler uses the following rules:
  1880. \begin{enumerate}
  1881. \item Operations with equal precedence are executed from left to right.
  1882. \item In operations with unequal precedence the operands belong to the
  1883. operater with the highest precedence. For example, in \var{5*3+7}, the
  1884. multiplication is higher in precedence than the addition, so it is
  1885. executed first. The result would be 22.
  1886. \item If parentheses are used in an epression, their contents is evaluated
  1887. first. Thus, \var {5*(3+7)} would result in 50.
  1888. \end{enumerate}
  1889. An expression is a sequence of terms and factors. A factor is an operand of
  1890. a multiplication operator. A term is an operand of an adding operator.
  1891. \section{Expression syntax}
  1892. An expression applies relational operators to simple expressions. Simple
  1893. expressions are a series of terms, joined by adding operators.
  1894. \input{syntax/expsimpl.syn}
  1895. The following are valid expressions:
  1896. \begin{verbatim}
  1897. GraphResult<>grError
  1898. (DoItToday=Yes) and (DoItTomorrow=No);
  1899. Day in Weekend
  1900. \end{verbatim}
  1901. And here are some simple expressions:
  1902. \begin{verbatim}
  1903. A + B
  1904. -Pi
  1905. ToBe or Not ToBe
  1906. \end{verbatim}
  1907. Terms consist of factors, connected by multiplication operators.
  1908. \input{syntax/expterm.syn}
  1909. Here are some valid terms:
  1910. \begin{verbatim}
  1911. 2 * Pi
  1912. A Div B
  1913. (DoItToday=Yes) and (DoItTomorrow=No);
  1914. \end{verbatim}
  1915. Factors are all other constructions:
  1916. \input{syntax/expfact.syn}
  1917. \section{Function calls}
  1918. Function calls are part of expressions (although, using extended syntax,
  1919. they can be statements too). They are constructed as follows:
  1920. \input{syntax/fcall.syn}
  1921. The \synt{variable reference} must be a procedural type variable referce.
  1922. A method designator can only be used in side the method of an object. A
  1923. qualified method designator can be used outside object methods too.
  1924. The function that will get called is the function with a declared parameter
  1925. list that matches the actual parameter list. This means that
  1926. \begin{enumerate}
  1927. \item The number of actual parameters must equal the number of declared
  1928. parameters.
  1929. \item The types of the parameters must be compatible. For varriable
  1930. reference parameters, the parameter types must be exactly the same.
  1931. \end{enumerate}
  1932. If no matching function is found, then the compiler will generate an error.
  1933. Depending on the fact of the function is overloaded (i.e. multiple functions
  1934. with the same name, but different parameter lists) the error will be
  1935. different.
  1936. There are cases when the compiler will not execute the function call in an
  1937. expression. This is the case when you are assigning a value to a procedural
  1938. type variable, as in the following example:
  1939. \begin{verbatim}
  1940. Type
  1941. FuncType = Function: Integer;
  1942. Var A : Integer;
  1943. Function AddOne : Integer;
  1944. begin
  1945. A := A+1;
  1946. AddOne := A;
  1947. end;
  1948. Var F : FuncType;
  1949. N : Integer;
  1950. begin
  1951. A := 0;
  1952. F := AddOne; { Assign AddOne to F, Don't call AddOne}
  1953. N := AddOne; { N := 1 !!}
  1954. end.
  1955. \end{verbatim}
  1956. In the above listing, the assigment to F will not cause the function AddOne
  1957. to be called. The assignment to N, however, will call AddOne.
  1958. A problem with this syntax is the following construction:
  1959. \begin{verbatim}
  1960. If F = AddOne Then
  1961. DoSomethingHorrible;
  1962. \end{verbatim}
  1963. Should the compiler compare the addresses of \var{F} and \var{AddOne},
  1964. or should it call both functions, and compare the result ? \fpc solves this
  1965. by deciding that a procedural variable is equivalent to a pointer. Thus the
  1966. compiler will give a type mismatch error, since AddOne is considered a
  1967. call to a function with integer result, and F is a pointer, Hence a type
  1968. mismatch occurs.
  1969. How then, should one compare whether \var{F} points to the function
  1970. \var{AddOne} ? To do this, one should use the address operator \var{@}:
  1971. \begin{verbatim}
  1972. If F = @AddOne Then
  1973. WriteLn ('Functions are equal');
  1974. \end{verbatim}
  1975. The left hand side of the boolean expression is an address. The right hand
  1976. side also, and so the compiler compares 2 addresses.
  1977. How to compare the values that both functions return ? By adding an empty
  1978. parameter list:
  1979. \begin{verbatim}
  1980. If F()=Addone then
  1981. WriteLn ('Functions return same values ');
  1982. \end{verbatim}
  1983. Remark that this behaviour is not compatible with Delphi syntax.
  1984. \section{Set constructors}
  1985. When you want to enter a set-type constant in an expression, you must give a
  1986. set constructor. In essence this is the same thing as when you define a set
  1987. type, only you have no identifier to identify the set with.
  1988. A set constructor is a comma separated list of expressions, enclosed in
  1989. square brackets.
  1990. \input{syntax/setconst.syn}
  1991. All set groups and set elements must be of the same ordinal type.
  1992. The empty set is denoted by \var{[]}, and it can be assigned to any type of
  1993. set. A set group with a range \var{[A..Z]} makes all values in the range a
  1994. set element. If the first range specifier has a bigger ordinal value than
  1995. the second the set is empty, e.g., \var{[Z..A]} denotes an empty set.
  1996. The following are valid set constructors:
  1997. \begin{verbatim}
  1998. [today,tomorrow]
  1999. [Monday..Friday,Sunday]
  2000. [ 2, 3*2, 6*2, 9*2 ]
  2001. ['A'..'Z','a'..'z','0'..'9']
  2002. \end{verbatim}
  2003. \section{Value typecasts}
  2004. Sometimes it is necessary to change the type of an expression, or a part of
  2005. the expression, to be able to be assignment compatible. This is done through
  2006. a value typecast. The syntax diagram for a value typecast is as follows:
  2007. \input{syntax/tcast.syn}
  2008. Value typecasts cannot be used on the left side of assignments, as variable
  2009. typecasts.
  2010. Here are some valid typecasts:
  2011. \begin{verbatim}
  2012. Byte('A')
  2013. Char(48)
  2014. boolean(1)
  2015. longint(@Buffer)
  2016. \end{verbatim}
  2017. The type size of the expression and the size of the type cast must be the
  2018. same. That is, the following doesn't work:
  2019. \begin{verbatim}
  2020. Integer('A')
  2021. Char(4875)
  2022. boolean(100)
  2023. Word(@Buffer)
  2024. \end{verbatim}
  2025. \section{The @ operator}
  2026. The address operator \var{@} returns the address of a variable, procedure
  2027. or function. It is used as follows:
  2028. \input{syntax/address.syn}
  2029. The \var{@} operator returns a typed pointer if the \var{\$T} switch is on.
  2030. If the \var{\$T} switch is off then the address operator returns an untyped
  2031. pointer, which is assigment compatible with all pointer types. The type of
  2032. the pointer is \var{\^{}T}, where \var{T} is the type of the variable
  2033. reference.
  2034. For example, the following will compile
  2035. \begin{verbatim}
  2036. Program tcast;
  2037. {$T-} { @ returns untyped pointer }
  2038. Type art = Array[1..100] of byte;
  2039. Var Buffer : longint;
  2040. PLargeBuffer : ^art;
  2041. begin
  2042. PLargeBuffer := @Buffer;
  2043. end.
  2044. \end{verbatim}
  2045. Changing the \var{\{\$T-\}} to \var{\{\$T+\}} will prevent the compiler from
  2046. compiling this. It will give a type mismatch error.
  2047. By default, the address operator returns an untyped pointer.
  2048. Applying the address operator to a function, method, or procedure identifier
  2049. will give a pointer to the entry point of that function. The result is an
  2050. untyped pointer.
  2051. By default, you must use the address operator if you want to assign a value
  2052. to a procedural type variable. This behaviour can be avoided by using the
  2053. \var{-So} or \var{-S2} switches, which result in a more compatible Delphi or
  2054. Turbo Pascal syntax.
  2055. \section{Operators}
  2056. Operators can be classified according to the type of expression they
  2057. operate on. We will discuss them type by type.
  2058. \subsection{Arithmetic operators}
  2059. Arithmetic operators occur in arithmetic operations, i.e. in expressions
  2060. that contain integers or reals. There are 2 kinds of operators : Binary and
  2061. unary arithmetic operators.
  2062. Binary operators are listed in \seet{binaroperators}, unary operators are
  2063. listed in \seet{unaroperators}.
  2064. \begin{FPCltable}{ll}{Binary arithmetic operators}{binaroperators}
  2065. Operator & Operation \\ \hline
  2066. \var{+} & Addition\\
  2067. \var{-} & Subtraction\\
  2068. \var{*} & Multiplication \\
  2069. \var{/} & Division \\
  2070. \var{Div} & Integer division \\
  2071. \var{Mod} & Remainder \\ \hline
  2072. \end{FPCltable}
  2073. With the exception of \var{Div} and \var{Mod}, which accept only integer
  2074. expressions as operands, all operators accept real and integer expressions as
  2075. operands.
  2076. For binary operators, the result type will be integer if both operands are
  2077. integer type expressions. If one of the operands is a real type expression,
  2078. then the result is real.
  2079. As an exception : division (\var{/}) results always in real values.
  2080. \begin{FPCltable}{ll}{Unary arithmetic operators}{unaroperators}
  2081. Operator & Operation \\ \hline
  2082. \var{+} & Sign identity\\
  2083. \var{-} & Sign inversion \\ \hline
  2084. \end{FPCltable}
  2085. For unary operators, the result type is always equal to the expression type.
  2086. The division (\var{/}) and \var{Mod} operator will cause run-time errors if
  2087. the second argument is zero.
  2088. The sign of the result of a \var{Mod} operator is the same as the sign of
  2089. the left side operand of the \var{Mod} operator. In fact, the \var{Mod}
  2090. operator is equivalent to the following operation :
  2091. \begin{verbatim}
  2092. I mod J = I - (I div J) * J
  2093. \end{verbatim}
  2094. but it executes faster than the right hand side expression.
  2095. \subsection{Logical operators}
  2096. Logical operators act on the individual bits of ordinal expressions.
  2097. Logical operators require operands that are of an integer type, and produce
  2098. an integer type result. The possible logical operators are listed in
  2099. \seet{logicoperations}.
  2100. \begin{FPCltable}{ll}{Logical operators}{logicoperations}
  2101. Operator & Operation \\ \hline
  2102. \var{not} & Bitwise negation (unary) \\
  2103. \var{and} & Bitwise and \\
  2104. \var{or} & Bitwise or \\
  2105. \var{xor} & Bitwise xor \\
  2106. \var{shl} & Bitwise shift to the left \\
  2107. \var{shr} & Bitwise shift to the right \\ \hline
  2108. \end{FPCltable}
  2109. The following are valid logical expressions:
  2110. \begin{verbatim}
  2111. A shr 1 { same as A div 2, but faster}
  2112. Not 1 { equals -2 }
  2113. Not 0 { equals -1 }
  2114. Not -1 { equals 0 }
  2115. B shl 2 { same as B * 2 for integers }
  2116. 1 or 2 { equals 3 }
  2117. 3 xor 1 { equals 2 }
  2118. \end{verbatim}
  2119. \subsection{Boolean operators}
  2120. Boolean operators can be considered logical operations on a type with 1 bit
  2121. size. Therefore the \var{shl} and \var{shr} operations have little sense.
  2122. Boolean operators can only have boolean type operands, and the resulting
  2123. type is always boolean. The possible operators are listed in
  2124. \seet{booleanoperators}
  2125. \begin{FPCltable}{ll}{Boolean operators}{booleanoperators}
  2126. Operator & Operation \\ \hline
  2127. \var{not} & logical negation (unary) \\
  2128. \var{and} & logical and \\
  2129. \var{or} & logical or \\
  2130. \var{xor} & logical xor \\ \hline
  2131. \end{FPCltable}
  2132. Remark that boolean expressions are ALWAYS evaluated with short-circuit
  2133. evaluation. This means that from the moment the result of the complete
  2134. expression is known, evaluation is stopped and the result is returned.
  2135. For instance, in the following expression:
  2136. \begin{verbatim}
  2137. B := True or MaybeTrue;
  2138. \end{verbatim}
  2139. The compiler will never look at the value of \var{MaybeTrue}, since it is
  2140. obvious that the expression will always be true. As a result of this
  2141. strategy, if \var{MaybeTrue} is a function, it will not get called !
  2142. (This can have surprising effects when used in conjunction with properties)
  2143. \subsection{String operators}
  2144. There is only one string operator : \var{+}. It's action is to concatenate
  2145. the contents of the two strings (or characters) it stands between.
  2146. You cannot use \var{+} to concatenate null-terminated (\var{PChar}) strings.
  2147. The following are valid string operations:
  2148. \begin{verbatim}
  2149. 'This is ' + 'VERY ' + 'easy !'
  2150. Dirname+'\'
  2151. \end{verbatim}
  2152. The following is not:
  2153. \begin{verbatim}
  2154. Var Dirname = Pchar;
  2155. ...
  2156. Dirname := Dirname+'\';
  2157. \end{verbatim}
  2158. Because \var{Dirname} is a null-terminated string.
  2159. \subsection{Set operators}
  2160. The following operations on sets can be performed with operators:
  2161. Union, difference and intersection. The operators needed for this are listed
  2162. in \seet{setoperators}.
  2163. \begin{FPCltable}{ll}{Set operators}{setoperators}
  2164. Operator & Action \\ \hline
  2165. \var{+} & Union \\
  2166. \var{-} & Difference \\
  2167. \var{*} & Intersection \\ \hline
  2168. \end{FPCltable}
  2169. The set type of the operands must be the same, or an error will be
  2170. generated by the compiler.
  2171. \subsection{Relational operators}
  2172. The relational operators are listed in \seet{relationoperators}
  2173. \begin{FPCltable}{ll}{Relational operators}{relationoperators}
  2174. Operator & Action \\ \hline
  2175. \var{=} & Equal \\
  2176. \var{<>} & Not equal \\
  2177. \var{<} & Stricty less than\\
  2178. \var{>} & Strictly greater than\\
  2179. \var{<=} & Less than or equal \\
  2180. \var{>=} & Greater than or equal \\
  2181. \var{in} & Element of \\ \hline
  2182. \end{FPCltable}
  2183. Left and right operands must be of the same type. You can only mix integer
  2184. and real types in relational expressions.
  2185. Comparing strings is done on the basis of their ASCII code representation.
  2186. When comparing pointers, the addresses to which they point are compared.
  2187. This also is true for \var{PChar} type pointers. If you want to compare the
  2188. strings the \var{Pchar} points to, you must use the \var{StrComp} function
  2189. from the \file{strings} unit.
  2190. The \var{in} returns \var{True} if the left operand (which must have the same
  2191. ordinal type as the set type) is an element of the set which is the right
  2192. operand, otherwise it returns \var{False}
  2193. \chapter{Statements}
  2194. \label{ch:Statements}
  2195. The heart of each algorithm are the actions it takes. These actions are
  2196. contained in the statements of your program or unit. You can label your
  2197. statements, and jump to them (within certain limits) with \var{Goto}
  2198. statements.
  2199. This can be seen in the following syntax diagram:
  2200. \input{syntax/statement.syn}
  2201. A label can be an identifier or an integer digit.
  2202. \section{Simple statements}
  2203. A simple statement cannot be decomposed in separate statements. There are
  2204. basically 4 kinds of simple statements:
  2205. \input{syntax/simstate.syn}
  2206. Of these statements, the {\em raise statement} will be explained in the
  2207. chapter on Exceptions (\seec{Exceptions})
  2208. \subsection{Assignments}
  2209. Assignments give a value to a variable, replacing any previous value the
  2210. observable might have had:
  2211. \input{syntax/assign.syn}
  2212. In addition to the standard Pascal assignment operator (\var{ := }), which
  2213. simply replaces the value of the varable with the value resulting from the
  2214. expression on the right of the { := } operator, \fpc
  2215. supports some c-style constructions. All available constructs are listed in
  2216. \seet{assignments}.
  2217. \begin{FPCltable}{lr}{Allowed C constructs in \fpc}{assignments}
  2218. Assignment & Result \\ \hline
  2219. a += b & Adds \var{b} to \var{a}, and stores the result in \var{a}.\\
  2220. a -= b & Substracts \var{b} from \var{a}, and stores the result in
  2221. \var{a}. \\
  2222. a *= b & Multiplies \var{a} with \var{b}, and stores the result in
  2223. \var{a}. \\
  2224. a /= b & Divides \var{a} through \var{b}, and stores the result in
  2225. \var{a}. \\ \hline
  2226. \end{FPCltable}
  2227. For these constructs to work, you should specify the \var{-Sc}
  2228. command-line switch.
  2229. {\em Remark:} These constructions are just for typing convenience, they
  2230. don't generate different code.
  2231. Here are some examples of valid assignment statements:
  2232. \begin{verbatim}
  2233. X := X+Y;
  2234. X+=Y; { Same as X := X+Y, needs -Sc command line switch}
  2235. X/=2; { Same as X := X/2, needs -Sc command line switch}
  2236. Done := False;
  2237. Weather := Good;
  2238. MyPi := 4* Tan(1);
  2239. \end{verbatim}
  2240. \subsection{Procedure statements}
  2241. Procedure statements are calls to subroutines. There are
  2242. different possibilities for procedure calls: A normal procedure call, an
  2243. object method call (qualified or not) , or even a call to a procedural
  2244. type variable. All types are present in the following diagram.
  2245. \input{syntax/procedure.syn}
  2246. The \fpc compiler will look for a procedure with the same name as given in
  2247. the procedure statement, and with a declared parameter list that matches the
  2248. actual parameter list.
  2249. The following are valid procedure statements:
  2250. \begin{verbatim}
  2251. Usage;
  2252. WriteLn('Pascal is an easy language !');
  2253. Doit();
  2254. \end{verbatim}
  2255. \subsection{Goto statements}
  2256. \fpc supports the \var{goto} jump statement. Its prototype syntax is
  2257. \input{syntax/goto.syn}
  2258. When using \var{goto} statements, you must keep the following in mind:
  2259. \begin{enumerate}
  2260. \item The jump label must be defined in the same block as the \var{Goto}
  2261. statement.
  2262. \item Jumping from outside a loop to the inside of a loop or vice versa can
  2263. have strange effects.
  2264. \item To be able to use the \var{Goto} statement, you need to specify the
  2265. \var{-Sg} compiler switch.
  2266. \end{enumerate}
  2267. \var{Goto} statements are considered bad practice and should be avoided as
  2268. much as possible. It is always possible to replace a \var{goto} statement by a
  2269. construction that doesn't need a \var{goto}, although this construction may
  2270. not be as clear as a goto statement.
  2271. For instance, the following is an allowed goto statement:
  2272. \begin{verbatim}
  2273. label
  2274. jumpto;
  2275. ...
  2276. Jumpto :
  2277. Statement;
  2278. ...
  2279. Goto jumpto;
  2280. ...
  2281. \end{verbatim}
  2282. \section{Structured statements}
  2283. Structured statements can be broken into smaller simple statements, which
  2284. should be executed repeatedly, conditionally or sequentially:
  2285. \input{syntax/struct.syn}
  2286. Conditional statements come in 2 flavours :
  2287. \input{syntax/conditio.syn}
  2288. Repetitive statements come in 3 flavours:
  2289. \input{syntax/repetiti.syn}
  2290. The following sections deal with each of these statements.
  2291. \subsection{Compound statements}
  2292. Compound statements are a group of statements, separated by semicolons,
  2293. that are surrounded by the keywords \var{Begin} and \var{End}. The
  2294. Last statement doesn't need to be followed by a semicolon, although it is
  2295. allowed. A compound statement is a way of grouping statements together,
  2296. executing the statements sequentially. They are treated as one statement
  2297. in cases where Pascal syntax expects 1 statement, such as in
  2298. \var{if ... then} statements.
  2299. \input{syntax/compound.syn}
  2300. \subsection{The \var{Case} statement}
  2301. \fpc supports the \var{case} statement. Its syntax diagram is
  2302. \input{syntax/case.syn}
  2303. The constants appearing in the various case parts must be known at
  2304. compile-time, and can be of the following types : enumeration types,
  2305. Ordinal types (except boolean), and chars. The expression must be also of
  2306. this type, or an compiler error will occur. All case constants must
  2307. have the same type.
  2308. The compiler will evaluate the expression. If one of the case constants
  2309. values matches the value of the expression, the statement that containing
  2310. this constant is executed. After that, the program continues after the final
  2311. \var{end}.
  2312. If none of the case constants match the expression value, the statement
  2313. after the \var{else} keyword is executed. This can be an empty statement.
  2314. If no else part is present, and no case constant matches the expression
  2315. value, program flow continues after the final \var{end}.
  2316. The case statements can be compound statements
  2317. (i.e. a \var{begin..End} block).
  2318. {\em Remark:} Contrary to Turbo Pascal, duplicate case labels are not
  2319. allowed in \fpc, so the following code will generate an error when
  2320. compiling:
  2321. \begin{verbatim}
  2322. Var i : integer;
  2323. ...
  2324. Case i of
  2325. 3 : DoSomething;
  2326. 1..5 : DoSomethingElse;
  2327. end;
  2328. \end{verbatim}
  2329. The compiler will generate a \var{Duplicate case label} error when compiling
  2330. this, because the 3 also appears (implicitly) in the range \var{1..5}. This
  2331. is similar to Delhpi syntax.
  2332. The following are valid case statements:
  2333. 'b' : WriteLn ('B pressed');
  2334. \begin{verbatim}
  2335. Case C of
  2336. 'a' : WriteLn ('A pressed');
  2337. 'c' : WriteLn ('C pressed');
  2338. else
  2339. WriteLn ('unknown letter pressed : ',C);
  2340. end;
  2341. \end{verbatim}
  2342. Or
  2343. 'b' : WriteLn ('B pressed');
  2344. \begin{verbatim}
  2345. Case C of
  2346. 'a','e','i','o','u' : WriteLn ('vowel pressed');
  2347. 'y' : WriteLn ('This one depends on the language');
  2348. else
  2349. WriteLn ('Consonant pressed');
  2350. end;
  2351. \end{verbatim}
  2352. \begin{verbatim}
  2353. Case Number of
  2354. 1..10 : WriteLn ('Small number');
  2355. 11..100 : WriteLn ('Normal, medium number');
  2356. else
  2357. WriteLn ('HUGE number');
  2358. end;
  2359. \end{verbatim}
  2360. \subsection{The \var{If..then..else} statement}
  2361. The \var{If .. then .. else..} protottype syntax is
  2362. \input{syntax/ifthen.syn}
  2363. The expression between the \var{if} and \var{then} keywords must have a
  2364. boolean return type. If the expression evaluates to \var{True} then the
  2365. statement following{then} is executed. If the expression evaluates to
  2366. \var{False}, then the statement following \var{else} is executed, if it is
  2367. present.
  2368. Be aware of the fact that the boolean expression will be short-cut evaluated.
  2369. (Meaning that the evaluation will be stopped at the point where the
  2370. outcome is known with certainty)
  2371. Also, before the \var {else} keyword, no semicolon (\var{;}) is allowed,
  2372. but all statements can be compound statements.
  2373. In nested \var{If.. then .. else} constructs, some ambiguity may araise as
  2374. to which \var{else} statement paits with which \var{if} statement. The rule
  2375. is that the \var{else } keyword matches the first \var{if} keyword not
  2376. already matched by an \var{else} keyword.
  2377. For example:
  2378. \begin{verbatim}
  2379. If exp1 Then
  2380. If exp2 then
  2381. Stat1
  2382. else
  2383. stat2;
  2384. \end{verbatim}
  2385. Despite it's appreance, the statement is syntactically equivalent to
  2386. \begin{verbatim}
  2387. If exp1 Then
  2388. begin
  2389. If exp2 then
  2390. Stat1
  2391. else
  2392. stat2
  2393. end;
  2394. \end{verbatim}
  2395. and not to
  2396. \begin{verbatim}
  2397. { NOT EQUIVALENT }
  2398. If exp1 Then
  2399. begin
  2400. If exp2 then
  2401. Stat1
  2402. end
  2403. else
  2404. stat2
  2405. \end{verbatim}
  2406. If it is this latter construct you want, you must explicitly put the
  2407. \var{begin} and \var{end} keywords. When in doubt, add them, they don't
  2408. hurt.
  2409. The following is a valid statement:
  2410. \begin{verbatim}
  2411. If Today in [Monday..Friday] then
  2412. WriteLn ('Must work harder')
  2413. else
  2414. WriteLn ('Take a day off.');
  2415. \end{verbatim}
  2416. \subsection{The \var{For..to/downto..do} statement}
  2417. \fpc supports the \var{For} loop construction. A for loop is used in case
  2418. one wants to calculated something a fixed number of times.
  2419. The prototype syntax is as follows:
  2420. \input{syntax/for.syn}
  2421. \var{Statement} can be a compound statement.
  2422. When this statement is encountered, the control variable is initialized with
  2423. the initial value, and is compared with the final value.
  2424. What happens next depends on whether \var{to} or \var{downto} is used:
  2425. \begin{enumerate}
  2426. \item In the case \var{To} is used, if the initial value larger than the final
  2427. value then \var{Statement} will never be executed.
  2428. \item In the case \var{DownTo} is used, if the initial value larger than the final
  2429. value then \var{Statement} will never be executed.
  2430. \end{enumerate}
  2431. After this check, the statement after \var{Do} is executed. After the
  2432. execution of the statement, the control variable is increased or decreased
  2433. with 1, depending on whether \var{To} or \var{Downto} is used.
  2434. The control variable must be an ordinal type, no other
  2435. types can be used as counters in a loop.
  2436. {\em Remark:} Contrary to ANSI pascal specifications, \fpc first initializes
  2437. the counter variable, and only then calculates the upper bound.
  2438. The following are valid loops:
  2439. \begin{verbatim}
  2440. For Day := Monday to Friday do Work;
  2441. For I := 100 downto 1 do
  2442. WriteLn ('Counting down : ',i);
  2443. For I := 1 to 7*dwarfs do KissDwarf(i);
  2444. \end{verbatim}
  2445. \subsection{The \var{Repeat..until} statement}
  2446. The \var{repeat} statement is used to execute a statement until a certain
  2447. condition is reached. The statement will be executed at least once.
  2448. The prototype syntax of the \var{Repeat..until} statement is
  2449. \input{syntax/repeat.syn}
  2450. This will execute the statements between \var{repeat} and {until} up to
  2451. the moment when \var{Expression} evaluates to \var{True}.
  2452. Since the \var{expression} is evaluated {\em after} the execution of the
  2453. statements, they are executed at least once.
  2454. Be aware of the fact that the boolean expression \var{Expression} will be
  2455. short-cut evaluated. (Meaning that the evaluation will be stopped at the
  2456. point where the outcome is known with certainty)
  2457. The following are valid \var{repeat} statements
  2458. \begin{verbatim}
  2459. repeat
  2460. WriteLn ('I =',i);
  2461. I := I+2;
  2462. until I>100;
  2463. repeat
  2464. X := X/2
  2465. until x<10e-3
  2466. \end{verbatim}
  2467. \subsection{The \var{While..do} statement}
  2468. A \var{while} statement is used to execute a statement as long as a certain
  2469. condition holds. This may imply that the statement is never executed.
  2470. The prototype syntax of the \var{While..do} statement is
  2471. \input{syntax/while.syn}
  2472. This will execute \var{Statement} as long as \var{Expression} evaluates to
  2473. \var{True}. Since \var{Expression} is evaluated {\em before} the execution
  2474. of \var{Statement}, it is possible that \var{Statement} isn't executed at
  2475. all. \var{Statement} can be a compound statement.
  2476. Be aware of the fact that the boolean expression \var{Expression} will be
  2477. short-cut evaluated. (Meaning that the evaluation will be stopped at the
  2478. point where the outcome is known with certainty)
  2479. The following are valid \var{while} statements:
  2480. \begin{verbatim}
  2481. I := I+2;
  2482. while i<=100 do
  2483. begin
  2484. WriteLn ('I =',i);
  2485. I := I+2;
  2486. end;
  2487. X := X/2;
  2488. while x>=10e-3 do
  2489. X := X/2;
  2490. \end{verbatim}
  2491. They correspond to the example loops for the \var{repeat} statements.
  2492. \subsection{The \var{With} statement}
  2493. \label{se:With}
  2494. The \var{with} statement serves to access the elements of a record\footnote{
  2495. The \var{with} statement does not work correctly when used with
  2496. objects or classes until version 0.99.6}
  2497. or object or class, without having to specify the name of the each time.
  2498. The syntax for a \var{with} statement is
  2499. \input{syntax/with.syn}
  2500. The variable reference must be a variable of a record, object or class type.
  2501. In the \var{with} statement, any variable reference, or method reference is
  2502. checked to see if it is a field or method of the record or object or class.
  2503. If so, then that field is accessed, or that method is called.
  2504. Given the declaration:
  2505. \begin{verbatim}
  2506. Type Passenger = Record
  2507. Name : String[30];
  2508. Flight : String[10];
  2509. end;
  2510. Var TheCustomer : Passenger;
  2511. \end{verbatim}
  2512. The following statements are completely equivalent:
  2513. \begin{verbatim}
  2514. TheCustomer.Name := 'Michael';
  2515. TheCustomer.Flight := 'PS901';
  2516. \end{verbatim}
  2517. and
  2518. \begin{verbatim}
  2519. With TheCustomer do
  2520. begin
  2521. Name := 'Michael';
  2522. Flight := 'PS901';
  2523. end;
  2524. \end{verbatim}
  2525. The statement
  2526. \begin{verbatim}
  2527. With A,B,C,D do Statement;
  2528. \end{verbatim}
  2529. is equivalent to
  2530. \begin{verbatim}
  2531. With A do
  2532. With B do
  2533. With C do
  2534. With D do Statement;
  2535. \end{verbatim}
  2536. This also is a clear example of the fact that the variables are tried {\em last
  2537. to first}, i.e., when the compiler encounters a variable reference, it will
  2538. first check if it is a field or method of the last variable. If not, then it
  2539. will check the last-but-one, and so on.
  2540. The following example shows this;
  2541. \begin{verbatim}
  2542. Program testw;
  2543. Type AR = record
  2544. X,Y : Longint;
  2545. end;
  2546. Var S,T : Ar;
  2547. begin
  2548. S.X := 1;S.Y := 1;
  2549. T.X := 2;T.Y := 2;
  2550. With S,T do
  2551. WriteLn (X,' ',Y);
  2552. end.
  2553. \end{verbatim}
  2554. The output of this program is
  2555. \begin{verbatim}
  2556. 2 2
  2557. \end{verbatim}
  2558. Showing thus that the \var{X,Y} in the \var{WriteLn} statement match the
  2559. \var{T} record variable.
  2560. \subsection{Exception Statements}
  2561. As of version 0.99.7, \fpc supports exceptions. Exceptions provide a
  2562. convenient way to program error and error-recovery mechanisms, and are
  2563. closely related to classes.
  2564. Exception support is explained in \seec{Exceptions}
  2565. \section{Assembler statements}
  2566. An assembler statement allows you to insert assembler code right in your
  2567. pascal code.
  2568. \input{syntax/statasm.syn}
  2569. More information about assembler blocks can be found in the \progref.
  2570. The register list is used to indicate the registers that are modified by an
  2571. assembler statement in your code. The compiler stores certain results in the
  2572. registers. If you modify the registers in an assembler statement, the compiler
  2573. should, sometimes, be told about it. The registers are denoted with their
  2574. Intel names for the I386 processor, i.e., \var{'EAX'}, \var{'ESI'} etc...
  2575. As an example, consider the following assembler code:
  2576. \begin{verbatim}
  2577. asm
  2578. Movl $1,%ebx
  2579. Movl $0,%eax
  2580. addl %eax,%ebx
  2581. end; ['EAX','EBX'];
  2582. \end{verbatim}
  2583. This will tell the compiler that it should save and restore the contents of
  2584. the \var{EAX} and \var{EBX} registers when it encounters this asm statement.
  2585. \chapter{Using functions and procedures}
  2586. \label{ch:Procedures}
  2587. \fpc supports the use of functions and procedures, but with some extras:
  2588. Function overloading is supported, as well as \var{Const} parameters and
  2589. open arrays.
  2590. {\em remark:} In many of the subsequent paragraphs the word \var{procedure}
  2591. and \var{function} will be used interchangeably. The statements made are
  2592. valid for both, except when indicated otherwise.
  2593. \section{Procedure declaration}
  2594. A procedure declaration defines an identifier and associates it with a
  2595. block of code. The procedure can then be called with a procedure statement.
  2596. \input{syntax/procedur.syn}
  2597. \sees{Parameters} for the list of parameters.
  2598. A procedure declaration that is followed by a block implements the action of
  2599. the procedure in that block.
  2600. The following is a valid procedure :
  2601. \begin{verbatim}
  2602. Procedure DoSomething (Para : String);
  2603. begin
  2604. Writeln ('Got parameter : ',Para);
  2605. Writeln ('Parameter in upper case : ',Upper(Para));
  2606. end;
  2607. \end{verbatim}
  2608. Note that it is possible that a procedure calls itself.
  2609. \section{Function declaration}
  2610. A function declaration defines an identifier and associates it with a
  2611. block of code. The block of code will return a result.
  2612. The function can then be called inside an expression, or with a procedure
  2613. statement.
  2614. \input{syntax/function.syn}
  2615. \section{Parameter lists}
  2616. \label{se:Parameters}
  2617. When you need to pass arguments to a function or procedure, these parameters
  2618. must be declared in the formal parameter list of that function or procedure.
  2619. The parameter list is a declaration of identifiers that can be referred to
  2620. only in that procedure or function's block.
  2621. \input{syntax/params.syn}
  2622. \var{const} parameters and \var{var} parameters can also be \var{untyped}
  2623. parameters if they have no type identifier.
  2624. \subsection{Value parameters}
  2625. Value parameters are declared as follows:
  2626. \input{syntax/paramval.syn}
  2627. When you declare parameters as value parameters, the procedure gets {\em
  2628. a copy} of the parameters that the calling block passes. Any modifications
  2629. to these parameters are purely local to the procedure's block, and do not
  2630. propagate back to the calling block.
  2631. A block that wishes to call a procedure with value parameters must pass
  2632. assignment compatible parameters to the procedure. This means that the types
  2633. should not match exactly, but can be converted (conversion code is inserted
  2634. by the compiler itself)
  2635. Take care that using value parameters makes heavy use of the stack,
  2636. especially if you pass large parameters. The total size of all parameters in
  2637. the formal parameter list should be below 32K for portability's sake (the
  2638. Intel version limits this to 64K).
  2639. You can pass open arrays as value parameters. See \sees{openarray} for
  2640. more information on using open arrays.
  2641. \subsection{\var{var} parameters}
  2642. \label{se:varparams}
  2643. Variable parameters are declared as follows:
  2644. \input{syntax/paramvar.syn}
  2645. When you declare parameters as variable parameters, the procedure or
  2646. function accesses immediatly the variable that the calling block passed in
  2647. its parameter list. The procedure gets a pointer to the variable that was
  2648. passed, and uses this pointer to access the variable's value.
  2649. From this, it follows that any changes that you make to the parameter, will
  2650. proagate back to the calling block. This mechanism can be used to pass
  2651. values back in procedures.
  2652. Because of this, the calling block must pass a parameter of {\em exactly}
  2653. the same type as the declared parameter's type. If it does not, the compiler
  2654. will generate an error.
  2655. Variable parameters can be untyped. In that case the variable has no type,
  2656. and hence is incompatible with all othertypes. However, you can use the
  2657. address operator on it, or you can pass it to a function that has also an
  2658. untyped parameter. If you want to use an untyped parameter in an assigment,
  2659. or you want to assign to it, you must use a typecast.
  2660. File type variables must always be passed as variable parameters.
  2661. You can pass open arrays as variable parameters. See \sees{openarray} for
  2662. more information on using open arrays.
  2663. \subsection{\var{Const} parameters}
  2664. In addition to variable parameters and value parameters \fpc also supports
  2665. \var{Const} parameters. You can specify a \var{Const} parameter as follows:
  2666. \input{syntax/paramcon.syn}
  2667. A constant argument is passed by reference if it's size is larger than a
  2668. longint. It is passed by value if the size equals 4 or less.
  2669. This means that the function or procedure receives a pointer to the passed
  2670. argument, but you are not allowed to assign to it, this will result in a
  2671. compiler error. Likewise, you cannot pass a const parameter on to another
  2672. function that requires a variable parameter.
  2673. The main use for this is reducing the stack size, hence improving
  2674. performance, and still retaining the semantics of passing by value...
  2675. Constant parameters can also be untyped. See \sees{varparams} for more
  2676. information about untyped parameters.
  2677. You can pass open arrays as constant parameters. See \sees{openarray} for
  2678. more information on using open arrays.
  2679. \subsection{Open array parameters}
  2680. \label{se:openarray}
  2681. \fpc supports the passing of open arrays, i.e. you can declare a procedure
  2682. with an array of unspecified length as a parameter, as in Delphi.
  2683. Open array parameters can be accessed in the procedure or function as an
  2684. array that is declared with starting starting index 0, and last element
  2685. index \var{High(paremeter)}.
  2686. For example, the parameter
  2687. \begin{verbatim}
  2688. Row : Array of Integer;
  2689. \end{verbatim}
  2690. would be equivalent to
  2691. \begin{verbatim}
  2692. Row : Array[1..N-1] of Integer;
  2693. \end{verbatim}
  2694. Where \var{N} would be the actual size of the array that is passed to the
  2695. function. \var{N-1} can be calculated as \var{High(Row)}.
  2696. Open parameters can be passed by value, by reference or as a constant
  2697. parameter. In the latter cases the procedure receives a pointer to the
  2698. actual array. In the former case,it receives a copy of the array.
  2699. In a function or procedure, you can pass open arrays only to functions which
  2700. are also declared with open arrays as parameters, {\em not} to functions or
  2701. procedures which accept arrays of fixed length.
  2702. The following is an example of a function using an open array:
  2703. \begin{verbatim}
  2704. Function Average (Row : Array of integer) : Real;
  2705. Var I : longint;
  2706. Temp : Real;
  2707. begin
  2708. Temp := Row[0];
  2709. For I := 1 to High(Row) do
  2710. Temp := Temp + Row[i];
  2711. Average := Temp / (High(Row)+1);
  2712. end;
  2713. \end{verbatim}
  2714. \section{Function overloading}
  2715. Function overloading simply means that you can define the same function more
  2716. than once, but each time with a different formal parameter list.
  2717. The parameter lists must differ at least in one of it's elements type.
  2718. When the compiler encounters a function call, it will look at the function
  2719. parameters to decide which od the defined function
  2720. This can be useful if you want to define the same function for different
  2721. types. For example, if the RTL, the \var{Dec} procedure is
  2722. is defined as:
  2723. \begin{verbatim}
  2724. ...
  2725. Dec(Var I : Longint;decrement : Longint);
  2726. Dec(Var I : Longint);
  2727. Dec(Var I : Byte;decrement : Longint);
  2728. Dec(Var I : Byte);
  2729. ...
  2730. \end{verbatim}
  2731. When the compiler encounters a call to the dec function, it wil first search
  2732. which function it should use. It therefore checks the parameters in your
  2733. function call, and looks if there is a function definition which maches the
  2734. specified parameter list. If the compiler finds such a function, a call is
  2735. inserted to that function. If no such function is found, a compiler error is
  2736. generated.
  2737. You cannot have overloaded functions that have a \var{cdecl} or \var{export}
  2738. modifier (Technically, because these two modifiers prevent the mangling of
  2739. the function name by the compiler)
  2740. \section{forward defined functions}
  2741. You can define a function without having it followed by it's implementation,
  2742. by having it followed by the \var{forward} procedure. The effective
  2743. implementation of that function must follow later in the module.
  2744. The function can be used after a \var{forward} declaration as if it had been
  2745. implemented already.
  2746. The following is an example of a forward declaration.
  2747. \begin{verbatim}
  2748. Program testforward;
  2749. Procedure First (n : longint); forward;
  2750. Procedure Second;
  2751. begin
  2752. WriteLn ('In second. Calling first...');
  2753. First (1);
  2754. end;
  2755. Procedure First (n : longint);
  2756. begin
  2757. WriteLn ('First received : ',n);
  2758. end;
  2759. begin
  2760. Second;
  2761. end.
  2762. \end{verbatim}
  2763. You cannot define a function twice as forward (nor is there any reason why
  2764. you would want to do that).
  2765. Likewise, in units, you cannot have a forward declared function of a
  2766. function that has been declared in the interface part. The interface
  2767. declaration counts as a \var{forward} declaration.
  2768. The following unit will give an error when compiled:
  2769. \begin{verbatim}
  2770. Unit testforward;
  2771. interface
  2772. Procedure First (n : longint);
  2773. Procedure Second;
  2774. implementation
  2775. Procedure First (n : longint); forward;
  2776. Procedure Second;
  2777. begin
  2778. WriteLn ('In second. Calling first...');
  2779. First (1);
  2780. end;
  2781. Procedure First (n : longint);
  2782. begin
  2783. WriteLn ('First received : ',n);
  2784. end;
  2785. end.
  2786. \end{verbatim}
  2787. \section{External functions}
  2788. \label{se:external}
  2789. The \var{external} modifier can be used to declare a function that resides in
  2790. an external object file. It allows you to use the function in
  2791. your code, and at linking time, you must link the object file containing the
  2792. implementation of the function or procedure.
  2793. \input{syntax/external.syn}
  2794. It replaces, in effect, the function or procedure code block. As such, it
  2795. can be present only in an implementation block of a unit, or in a program.
  2796. As an example:
  2797. \begin{verbatim}
  2798. program CmodDemo;
  2799. {$Linklib c}
  2800. Const P : PChar = 'This is fun !';
  2801. Function strlen (P : PChar) : Longint; cdecl; external;
  2802. begin
  2803. WriteLn ('Length of (',p,') : ',strlen(p))
  2804. end.
  2805. \end{verbatim}
  2806. {\em Remark} The parameters in our declaration of the \var{external} function
  2807. should match exactly the ones in the declaration in the object file.
  2808. If the \var{external} modifier is followed by a string constant:
  2809. \begin{verbatim}
  2810. external 'lname';
  2811. \end{verbatim}
  2812. Then this tells the compiler that the function resides in library
  2813. 'lname'. The compiler will the automatically link this library to
  2814. your program.
  2815. You can also specify the name that the function has in the library:
  2816. \begin{verbatim}
  2817. external 'lname' name Fname;
  2818. \end{verbatim}
  2819. This tells the compiler that the function resides in library 'lname',
  2820. but with name 'Fname'. The compiler will the automatically link this
  2821. library to your program, and use the correct name for the function.
  2822. Under \windows and \ostwo, you can also use the following form:
  2823. \begin{verbatim}
  2824. external 'lname' Index Ind;
  2825. \end{verbatim}
  2826. This tells the compiler that the function resides in library 'lname',
  2827. but with index \var{Ind}. The compiler will the automatically
  2828. link this library to your program, and use the correct index for the
  2829. function.
  2830. \section{Assembler functions}
  2831. Functions and procedures can be completely implemented in assembly
  2832. language. To indicate this, you use the \var{assembler} keyword:
  2833. \input{syntax/asm.syn}
  2834. Contrary to Delphi, the assembler keyword must be present to indicate an
  2835. assembler function.
  2836. For more information about assembler functions, see the chapter on using
  2837. assembler in the \progref.
  2838. \section{Modifiers}
  2839. A function or procedure declaration can contain modifiers. Here we list the
  2840. various possibilities:
  2841. \input{syntax/modifiers.syn}
  2842. \fpc doesn't support all Turbo Pascal modifiers, but
  2843. does support a number of additional modifiers. They are used mainly for assembler and
  2844. reference to C object files. More on the use of modifiers can be found in
  2845. \progref.
  2846. \subsection{Public}
  2847. The \var{Public} keyword is used to declare a function globally in a unit.
  2848. This is useful if you don't want a function to be accessible from the unit
  2849. file, but you do want the function to be accessible from the object file.
  2850. as an example:
  2851. \begin{verbatim}
  2852. Unit someunit;
  2853. interface
  2854. Function First : Real;
  2855. Implementation
  2856. Function First : Real;
  2857. begin
  2858. First := 0;
  2859. end;
  2860. Function Second : Real; [Public];
  2861. begin
  2862. Second := 1;
  2863. end;
  2864. end.
  2865. \end{verbatim}
  2866. If another program or unit uses this unit, it will not be able to use the
  2867. function \var{Second}, since it isn't declared in the interface part.
  2868. However, it will be possible to access the function \var{Second} at the
  2869. assembly-language level, by using it's mangled name (\progref).
  2870. \subsection{cdecl}
  2871. \label{se:cdecl}
  2872. The \var{cdecl} modifier can be used to declare a function that uses a C
  2873. type calling convention. This must be used if you wish to acces functions in
  2874. an object file generated by a C compiler. It allows you to use the function in
  2875. your code, and at linking time, you must link the object file containing the
  2876. \var{C} implementation of the function or procedure.
  2877. As an example:
  2878. \begin{verbatim}
  2879. program CmodDemo;
  2880. {$LINKLIB c}
  2881. Const P : PChar = 'This is fun !';
  2882. Function strlen (P : PChar) : Longint; cdecl; external;
  2883. begin
  2884. WriteLn ('Length of (',p,') : ',strlen(p))
  2885. end.
  2886. \end{verbatim}
  2887. When compiling this, and linking to the C-library, you will be able to call
  2888. the \var{strlen} function throughout your program. The \var{external}
  2889. directive tells the compiler that the function resides in an external
  2890. object filebrary (see \ref{se:external}).
  2891. {\em Remark} The parameters in our declaration of the \var{C} function should
  2892. match exactly the ones in the declaration in \var{C}. Since \var{C} is case
  2893. sensitive, this means also that the name of the
  2894. function must be exactly the same. the \fpc compiler will use the name {\em
  2895. exactly} as it is typed in the declaration.
  2896. \subsection{popstack}
  2897. \label{se:popstack}
  2898. Popstack does the same as \var{cdecl}, namely it tells the \fpc compiler
  2899. that a function uses the C calling convention. In difference with the
  2900. \var{cdecl} modifier, it still mangles the name of the function as it would
  2901. for a normal pascal function.
  2902. With \var{popstack} you could access functions by their pascal names in a
  2903. library.
  2904. \subsection{Export}
  2905. Sometimes you must provide a callback function for a C library, or you want
  2906. your routines to be callable from a C program. Since \fpc and C use
  2907. different calling schemes for functions and procedures\footnote{More
  2908. techically: In C the calling procedure must clear the stack. In \fpc, the
  2909. subroutine clears the stack.}, the compiler must be told to generate code
  2910. that can be called from a C routine. This is where the \var{Export} modifier
  2911. comes in. Contrary to the other modifiers, it must be specified separately,
  2912. as follows:
  2913. \begin{verbatim}
  2914. function DoSquare (X : Longint) : Longint; export;
  2915. begin
  2916. ...
  2917. end;
  2918. \end{verbatim}
  2919. The square brackets around the modifier are not allowed in this case.
  2920. {\em Remark:}
  2921. as of version 0.9.8, \fpc supports the Delphi \var{cdecl} modifier.
  2922. This modifier works in the same way as the \var{export} modifier.
  2923. More information about these modifiers can be found in the \progref, in the
  2924. section on the calling mechanism and the chapter on linking.
  2925. \subsection{StdCall}
  2926. As of version 0.9.8, \fpc supports the Delphi \var{stdcall} modifier.
  2927. This modifier does actually nothing, since the \fpc compiler by default
  2928. pushes parameters from right to left on the stack, which is what the
  2929. modifier does under Delphi (which pushes parameters on the stack from left to
  2930. right).
  2931. More information about this modifier can be found in the \progref, in the
  2932. section on the calling mechanism and the chapter on linking.
  2933. \subsection{Alias}
  2934. The \var{Alias} modifier allows you to specify a different name for a
  2935. procedure or function. This is mostly useful for referring to this procedure
  2936. from assembly language constructs. As an example, consider the following
  2937. program:
  2938. \begin{verbatim}
  2939. Program Aliases;
  2940. Procedure Printit; [Alias : 'DOIT'];
  2941. begin
  2942. WriteLn ('In Printit (alias : "DOIT")');
  2943. end;
  2944. begin
  2945. asm
  2946. call DOIT
  2947. end;
  2948. end.
  2949. \end{verbatim}
  2950. {\rm Remark:} the specified alias is inserted straight into the assembly
  2951. code, thus it is case sensitive.
  2952. The \var{Alias} modifier, combined with the \var{Public} modifier, make a
  2953. powerful tool for making externally accessible object files.
  2954. \section{Unsupported Turbo Pascal modifiers}
  2955. The modifiers that exist in Turbo pascal, but aren't supported by \fpc, are
  2956. listed in \seet{Modifs}.
  2957. \begin{FPCltable}{lr}{Unsupported modifiers}{Modifs}
  2958. Modifier & Why not supported ? \\ \hline
  2959. Near & \fpc is a 32-bit compiler.\\
  2960. Far & \fpc is a 32-bit compiler. \\
  2961. %External & Replaced by \var{C} modifier. \\ \hline
  2962. \end{FPCltable}
  2963. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2964. % Programs, Units, Blocks
  2965. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2966. \chapter{Programs, units, blocks}
  2967. A Pascal program consists of modules called \var{units}. A unit can be used
  2968. to group pieces of code together, or to give someone code without giving
  2969. the sources.
  2970. Both programs and units consist of code blocks, which are mixtures of
  2971. statements, procedures, and variable or type declarations.
  2972. \section{Programs}
  2973. A pascal program consists of the program header, followed possibly by a
  2974. 'uses' clause, and a block.
  2975. \input{syntax/program.syn}
  2976. The program header is provided for backwards compatibility, and is ignored
  2977. by the compiler.
  2978. The uses clause serves to identify all units that are needed by the program.
  2979. The system unit doesn't have to be in this list, since it is always loaded
  2980. by the compiler.
  2981. The order in which the units appear is significant, it determines in
  2982. which order they are initialized. Units are initialized in the same order
  2983. as they appear in the uses clause. Identifiers are searched in the opposite
  2984. order, i.e. when the compiler searches for an identifier, then it looks
  2985. first in the last unit in the uses clause, then the last but one, and so on.
  2986. This is important in case two units declare different types with the same
  2987. identifier.
  2988. When the compiler looks for unit files, it adds the extension \file{.ppu}
  2989. (\file{.ppw} for \windowsnt) to the name of the unit. On \linux, unit names
  2990. are converted to all lowercase when looking for a unit.
  2991. If a unit name is longer than 8 characters, the compiler will first look for
  2992. a unit name with this length, and then it will truncate the name to 8
  2993. characters and look for it again.
  2994. \section{Units}
  2995. A unit contains a set of declarations, procedures and functions that can be
  2996. used by a program or another unit.
  2997. The syntax for a unit is as follows:
  2998. \input{syntax/unit.syn}
  2999. The interface part declares all identifiers that must be exported from the
  3000. unit. This can be constant, type or variable identifiers, and also procedure
  3001. or function identifier declarations. Declarations inside the
  3002. implementationpart are {\em not} accessible outside the unit. The
  3003. implementation must contain a function declaration for each function or
  3004. procedure that is declared in the interface part. If a function is declared
  3005. in the interface part, but no declaration of that function is present in the
  3006. implementation section is present, then the compiler will give an error.
  3007. When a program uses a unit (say \file{unitA}) and this units uses a second
  3008. unit, say \file{unitB}, then the program depends indirectly also on
  3009. \var{unitB}. This means that the compiler must have access to \file{unitB} when
  3010. trying to compile the program. If the unit is not present at compile time,
  3011. an error occurs.
  3012. Note that the identifiers from a unit on which a program depends indirectly,
  3013. are not accessible to the program. To have access to the identifiers of a
  3014. unit, you must put that unit in the uses clause of the program or unit where
  3015. you want to yuse the identifier.
  3016. Units can be mutually dependent, that is, they can reference each other in
  3017. their uses clauses. This is allowed, on the condition that at least one of
  3018. the references is in the implementation section of the unit. This also holds
  3019. for indirect mutually dependent units.
  3020. If it is possible to start from one interface uses clause of a unit, and to return
  3021. there via uses clauses of interfaces only, then there is circular unit
  3022. dependence, and the compiler will generate an error.
  3023. As and example : the following is not allowed:
  3024. \begin{verbatim}
  3025. Unit UnitA;
  3026. interface
  3027. Uses UnitB;
  3028. implementation
  3029. end.
  3030. Unit UnitB
  3031. Uses UnitA;
  3032. implementation
  3033. end.
  3034. \end{verbatim}
  3035. But this is allowed :
  3036. \begin{verbatim}
  3037. Unit UnitA;
  3038. interface
  3039. Uses UnitB;
  3040. implementation
  3041. end.
  3042. Unit UnitB
  3043. implementation
  3044. Uses UnitA;
  3045. end.
  3046. \end{verbatim}
  3047. Because \file{UnitB} uses \file{UnitA} only in it's implentation section.
  3048. In general, it is a bad idea to have circular unit dependencies, even if it is
  3049. only in implementation sections.
  3050. \section{Blocks}
  3051. Units and programs are made of blocks. A block is made of declarations of
  3052. labels, constants, types variables and functions or procedures. Blocks can
  3053. be nested in certain ways, i.e., a procedure or function declaration can
  3054. have blocks in themselves.
  3055. A block looks like the following:
  3056. \input{syntax/block.syn}
  3057. Labels that can be used to identify statements in a block are declared in
  3058. the label declaration part of that block. Each label can only identify one
  3059. statement.
  3060. Constants that are to be used only in one block should be declared in that
  3061. block's constant declaration part.
  3062. Variables that are to be used only in one block should be declared in that
  3063. block's constant declaration part.
  3064. Types that are to be used only in one block should be declared in that
  3065. block's constant declaration part.
  3066. Lastly, functions and procedures that will be used in that block can be
  3067. declared in the procedure/function declaration part.
  3068. After the different declaration parts comes the statement part. This
  3069. contains any actions that the block should execute.
  3070. All identifiers declared before the statement part can be used in that
  3071. statement part.
  3072. \section{Scope}
  3073. Identifiers are valid from the point of their declaration until the end of
  3074. the block in which the declaration occurred. The range where the identifier
  3075. is known is the {\em scope} of the identifier. The exact scope of an
  3076. identifier depends on the way it was defined.
  3077. \subsection{Block scope}
  3078. The {\em scope} of a variable declared in the declaration part of a block,
  3079. is valid from the point of declaration until the end of the block.
  3080. If a block contains a second block, in which the identfier is
  3081. redeclared, then inside this block, the second declaration will be valid.
  3082. Upon leaving the inner block, the first declaration is valid again.
  3083. Consider the following example:
  3084. \begin{verbatim}
  3085. Program Demo;
  3086. Var X : Real;
  3087. { X is real variable }
  3088. Procedure NewDeclaration
  3089. Var X : Integer; { Redeclare X as integer}
  3090. begin
  3091. // X := 1.234; {would give an error when trying to compile}
  3092. X := 10; { Correct assigment}
  3093. end;
  3094. { From here on, X is Real again}
  3095. begin
  3096. X := 2.468;
  3097. end.
  3098. \end{verbatim}
  3099. In this example, inside the procedure, X denotes an integer variable.
  3100. It has it's own storage space, independent of the variable \var{X} outside
  3101. the procedure.
  3102. \subsection{Record scope}
  3103. The field identifiers inside a record definition are valid in the following
  3104. places:
  3105. \begin{enumerate}
  3106. \item to the end of the record definition.
  3107. \item field designators of a variable of the given record type.
  3108. \item identifiers inside a \var{With} statement that operates on a variable
  3109. of the given record type.
  3110. \end{enumerate}
  3111. \subsection{Class scope}
  3112. A component identifier is valid in the following places:
  3113. \begin{enumerate}
  3114. \item From the point of declaration to the end of the class definition.
  3115. \item In all descendent types of this class.
  3116. \item In all method declaration blocks of this class and descendent classes.
  3117. \item In a with statement that operators on a variable of the given class's
  3118. definition.
  3119. \end{enumerate}
  3120. Note that method designators are also considered identifiers.
  3121. \subsection{Unit scope}
  3122. All identifiers in the interface part of a unit are valid from the point of
  3123. declaration, until the end of the unit. Furthermore, the identifiers are
  3124. known in programs or units that have the unit in their uses clause.
  3125. Identifiers from indirectly dependent units are {\em not} available.
  3126. Identifiers declared in the implementation part of a unit are valid from the
  3127. point of declaration to the end of the unit.
  3128. The system unit is automatically used in all units and programs.
  3129. It's identifiers are therefore always known, in each program or unit
  3130. you make.
  3131. The rules of unit scope implie that you can redefine an identifier of a
  3132. unit. To have access to an identifier of another unit that was redeclared in
  3133. the current unit, precede it with that other units name, as in the following
  3134. example:
  3135. \begin{verbatim}
  3136. unit unitA;
  3137. interface
  3138. Type
  3139. MyType = Real;
  3140. implementation
  3141. end.
  3142. Program prog;
  3143. Uses UnitA;
  3144. { Redeclaration of MyType}
  3145. Type MyType = Integer;
  3146. Var A : Mytype; { Will be Integer }
  3147. B : UnitA.MyType { Will be real }
  3148. begin
  3149. end.
  3150. \end{verbatim}
  3151. This is especially useful if you redeclare the system unit's identifiers.
  3152. \section{Libraries}
  3153. \fpc supports making of dynamic libraries (DLLs under Windows) trough
  3154. the use of the \var{Library} keyword.
  3155. A Library is just like a unit or a program:
  3156. \input{syntax/library.syn}
  3157. By default, functions and procedures that are declared and implemented in
  3158. library are not available to a programmer that wishes to use your library.
  3159. In order to make functions or procedures available from the library,
  3160. you must export them in an export clause:
  3161. \input{syntax/exports.syn}
  3162. Under \windowsnt, an index clause can be added to an exports entry.
  3163. an index entry must be a positive number larger or equal than 1.
  3164. It is best to use low index values, although nothing forces you to
  3165. do this.
  3166. Optionally, an exports entry can have a name specifier. If present, the name
  3167. specifier gives the exavt name (case sensitive) of the function in the
  3168. library.
  3169. If neither of these constructs is present, the functions or procedures
  3170. are exported with the exact names as specified in the exports clause.
  3171. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3172. % Exceptions
  3173. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3174. \chapter{Exceptions}
  3175. \label{ch:Exceptions}
  3176. As of version 0.99.7, \fpc supports exceptions. Exceptions provide a
  3177. convenient way to program error and error-recovery mechanisms, and are
  3178. closely related to classes.
  3179. Exception support is based on 3 constructs:
  3180. \begin{description}
  3181. \item [Raise\ ] statements. To raise an exeption. This is usually done to signal an
  3182. error condition.
  3183. \item [Try ... Except\ ] blocks. These block serve to catch exceptions
  3184. raised within the scope of the block, and to provide exception-recovery
  3185. code.
  3186. \item [Try ... Finally\ ] blocks. These block serve to force code to be
  3187. executed irrespective of an exception occurrence or not. They generally
  3188. serve to clean up memory or close files in case an exception occurs.
  3189. code.
  3190. \end{description}
  3191. \section{The raise statement}
  3192. The \var{raise} statement is as follows:
  3193. \input{syntax/raise.syn}
  3194. This statement will raise an exception. If it is specified, the exception
  3195. instance must be an initialized instance of a class, which is the raise
  3196. type. The address exception is optional. If itis not specified, the compiler
  3197. will provide the address by itself.
  3198. If the exception instance is omitted, then the current exception is
  3199. re-raised. This construct can only be used in an exception handling
  3200. block (see further).
  3201. Remark that control {\em never} returns after an exception block. The
  3202. control is transferred to the first \var{try...finally} or
  3203. \var{try...except} statement that is encountered when unwinding the stack.
  3204. If no such statement is found, the \fpc Run-Time Library will generate a
  3205. run-time error 217 (see also \sees{exceptclasses}).
  3206. As an example: The following division checks whether the denominator is
  3207. zero, and if so, raises an exception of type \var{EDivException}
  3208. \begin{verbatim}
  3209. Type EDivException = Class(Exception);
  3210. Function DoDiv (X,Y : Longint) : Integer;
  3211. begin
  3212. If Y=0 then
  3213. Raise EDivException.Create ('Division by Zero would occur');
  3214. Result := X Div Y;
  3215. end;
  3216. \end{verbatim}
  3217. The class \var{Exception} is defined in the \file{Sysutils} unit of the rtl.
  3218. (\sees{exceptclasses})
  3219. \section{The try...except statement}
  3220. A \var{try...except} exception handling block is of the following form :
  3221. \input{syntax/try.syn}
  3222. If no exception is raised during the execution of the \var{statement list},
  3223. then all statements in the list will be executed sequentially, and the
  3224. except block will be skipped, transferring program flow to the statement
  3225. after the final \var{end}.
  3226. If an exception occurs during the execution of the \var{statement list}, the
  3227. program flow fill be transferred to the except block. Statements in the
  3228. statement list between the place where the exception was raised and the
  3229. exception block are ignored.
  3230. In the exception handling block, the type of the exception is checked,
  3231. and if there is an exception handler where the class type matches the
  3232. exception object type, or is a parent type of
  3233. the exception object type, then the statement following the corresponding
  3234. \var{Do} will be executed. The first matching type is used. After the
  3235. \var{Do} block was executed, the program continues after the \var{End}
  3236. statement.
  3237. The identifier in an exception handling statement is optional, and declares
  3238. an exception object. It can be used to manipulate the exception object in
  3239. the exception handling code. The scope of this declaration is the statement
  3240. block foillowing the \var{Do} keyword.
  3241. If none of the \var{On} handlers matches the exception object type, then the
  3242. \var{Default exception handler} is executed. If no such default handler is
  3243. found, then the exception is automatically re-raised. This process allows
  3244. to nest \var{try...except} blocks.
  3245. If, on the other hand, the exception was caught, then the exception object is
  3246. destroyed at the end of the exception handling block, before program flow
  3247. continues. The exception is destroyed through a call to the object's
  3248. \var{Destroy} destructor.
  3249. As an example, given the previous declaration of the \var{DoDiv} function,
  3250. consider the following
  3251. \begin{verbatim}
  3252. Try
  3253. Z := DoDiv (X,Y);
  3254. Except
  3255. On EDivException do Z := 0;
  3256. end;
  3257. \end{verbatim}
  3258. If \var{Y} happens to be zero, then the DoDiv function code will raise an
  3259. exception. When this happens, program flow is transferred to the except
  3260. statement, where the Exception handler will set the value of \var{Z} to
  3261. zero. If no exception is raised, then program flow continues past the last
  3262. \var{end} statement.
  3263. To allow error recovery, the \var{Try ... Finally} block is supported.
  3264. A \var{Try...Finally} block ensures that the statements following the
  3265. \var{Finally} keyword are guaranteed to be executed, even if an exception
  3266. occurs.
  3267. \section{The try...finally statement}
  3268. A \var{Try..Finally} statement has the following form:
  3269. \input{syntax/finally.syn}
  3270. If no exception occurs inside the \var{statement List}, then the program
  3271. runs as if the \var{Try}, \var{Finally} and \var{End} keywords were not
  3272. present.
  3273. If, however, an exception occurs, the program flow is immediatly
  3274. transferred from the point where the excepion was raised to the first
  3275. statement of the \var{Finally statements}.
  3276. All statements after the finally kayword will be executed, and then
  3277. the exception will be automatically re-raised. Any statements between the
  3278. place where the exception was raised and the first statement of the
  3279. \var{Finally Statements} are skipped.
  3280. As an example consider the following routine:
  3281. \begin{verbatim}
  3282. Procedure Doit (Name : string);
  3283. Var F : Text;
  3284. begin
  3285. Try
  3286. Assign (F,Name);
  3287. Rewrite (name);
  3288. ... File handling ...
  3289. Finally
  3290. Close(F);
  3291. end;
  3292. \end{verbatim}
  3293. If during the execution of the file handling an excption occurs, then
  3294. program flow will continue at the \var{close(F)} statement, skipping any
  3295. file operations that might follow between the place where the exception
  3296. was raised, and the \var{Close} statement.
  3297. If no exception occurred, all file operations will be executed, and the file
  3298. will be closed at the end.
  3299. \section{Exception handling nesting}
  3300. It is possible to nest \var{Try...Except} blocks with \var{Try...Finally}
  3301. blocks. Program flow will be done according to a \var{lifo} (last in, first
  3302. out) principle: The code of the last encountered \var{Try...Except} or
  3303. \var{Try...Finally} block will be executed first. If the exception is not
  3304. caught, or it was a finally statement, program flow will we transferred to
  3305. the last but-one block, {\em ad infinitum}.
  3306. If an exception occurs, and there is no exception handler present, then a
  3307. runerror 217 will be generated. If you use the \file{sysutils} unit, a default
  3308. handler is installed which ioll show the exception object message, and the
  3309. address where the exception occurred, after which the program will exit with
  3310. a \var{Halt} instruction.
  3311. \section{Exception classes}
  3312. \label{se:exceptclasses}
  3313. The \file{sysutils} unit contains a great deal of exception handling.
  3314. It defines the following exception types:
  3315. \begin{verbatim}
  3316. Exception = class(TObject)
  3317. private
  3318. fmessage : string;
  3319. fhelpcontext : longint;
  3320. public
  3321. constructor create(const msg : string);
  3322. constructor createres(indent : longint);
  3323. property helpcontext : longint read fhelpcontext write fhelpcontext;
  3324. property message : string read fmessage write fmessage;
  3325. end;
  3326. ExceptClass = Class of Exception;
  3327. { mathematical exceptions }
  3328. EIntError = class(Exception);
  3329. EDivByZero = class(EIntError);
  3330. ERangeError = class(EIntError);
  3331. EIntOverflow = class(EIntError);
  3332. EMathError = class(Exception);
  3333. \end{verbatim}
  3334. The sysutils unit also installs an exception handler. If an exception is
  3335. unhandled by any exception handling block, this handler is called by the
  3336. Run-Time library. Basically, it prints the exception address, and it prints
  3337. the message of the Exception object, and exits with a exit code of 217.
  3338. If the exception object is not a descendent object of the \var{Exception}
  3339. object, then the class name is printed instead of the exception message.
  3340. It is recommended to use the Exception object or a descendant class for
  3341. all raise statemnts, since then you can use the message field of the
  3342. exception object.
  3343. \chapter{Using assembler}
  3344. \fpc supports the use of assembler in your code, but not inline
  3345. assembler macros. To have more information on the processor
  3346. specific assembler syntax and its limitations, see the \progref.
  3347. \section{Assembler statements }
  3348. The following is an example of assembler inclusion in your code.
  3349. \begin{verbatim}
  3350. ...
  3351. Statements;
  3352. ...
  3353. Asm
  3354. your asm code here
  3355. ...
  3356. end;
  3357. ...
  3358. Statements;
  3359. \end{verbatim}
  3360. The assembler instructions between the \var{Asm} and \var{end} keywords will
  3361. be inserted in the assembler generated by the compiler.
  3362. You can still use conditionals in your assembler, the compiler will
  3363. recognise it, and treat it as any other conditionals.
  3364. \emph{ Remark: } Before version 0.99.1, \fpc did not support
  3365. reference to variables by their names in the assembler parts of your code.
  3366. \section{Assembler procedures and functions}
  3367. Assembler procedures and functions are declared using the
  3368. \var{Assembler} directive. The \var{Assembler} keyword is supported
  3369. as of version 0.9.7. This permits the code generator to make a number
  3370. of code generation optimizations.
  3371. The code generator does not generate any stack frame (entry and exit
  3372. code for the routine) if it contains no local variables and no
  3373. parameters. In the case of functions, ordinal values must be returned
  3374. in the accumulator. In the case of floating point values, these depend
  3375. on the target processor and emulation options.
  3376. \emph{ Remark: } Before version 0.99.1, \fpc did not support
  3377. reference to variables by their names in the assembler parts of your code.
  3378. \emph{ Remark: } From version 0.99.1 to 0.99.5 (\emph{excluding}
  3379. FPC 0.99.5a), the \var{Assembler} directive did not have the
  3380. same effect as in Turbo Pascal, so beware! The stack frame would be
  3381. omitted if there were no local variables, in this case if the assembly
  3382. routine had any parameters, they would be referenced directly via the stack
  3383. pointer. This was \emph{ NOT} like Turbo Pascal where the stack frame is only
  3384. omitted if there are no parameters \emph{ and } no local variables. As
  3385. stated earlier, starting from version 0.99.5a, \fpc now has the same
  3386. behaviour as Turbo Pascal.
  3387. %
  3388. % System unit reference guide.
  3389. %
  3390. %\end{document}
  3391. \part{Reference : The System unit}
  3392. \chapter{The system unit}
  3393. \label{ch:refchapter}
  3394. The system unit contains the standard supported functions of \fpc. It is the
  3395. same for all platforms. Basically it is the same as the system unit provided
  3396. with Borland or Turbo Pascal.
  3397. Functions are listed in alphabetical order.
  3398. Arguments to functions or procedures that are optional are put between
  3399. square brackets.
  3400. The pre-defined constants and variables are listed in the first section. The
  3401. second section contains the supported functions and procedures.
  3402. \section{Types, Constants and Variables}
  3403. \subsection{Types}
  3404. The following integer types are defined in the System unit:
  3405. \begin{verbatim}
  3406. shortint = -128..127;
  3407. Longint = $80000000..$7fffffff;
  3408. integer = -32768..32767;
  3409. byte = 0..255;
  3410. word = 0..65535;
  3411. \end{verbatim}
  3412. And the following pointer types:
  3413. \begin{verbatim}
  3414. PChar = ^char;
  3415. pPChar = ^PChar;
  3416. \end{verbatim}
  3417. For the \seef{SetJmp} and \seep{LongJmp} calls, the following jump bufer
  3418. type is defined (for the I386 processor):
  3419. \begin{verbatim}
  3420. jmp_buf = record
  3421. ebx,esi,edi : Longint;
  3422. bp,sp,pc : Pointer;
  3423. end;
  3424. PJmp_buf = ^jmp_buf;
  3425. \end{verbatim}
  3426. \subsection{Constants}
  3427. The following constants for file-handling are defined in the system unit:
  3428. \begin{verbatim}
  3429. Const
  3430. fmclosed = $D7B0;
  3431. fminput = $D7B1;
  3432. fmoutput = $D7B2;
  3433. fminout = $D7B3;
  3434. fmappend = $D7B4;
  3435. filemode : byte = 2;
  3436. \end{verbatim}
  3437. Further, the following non processor specific general-purpose constants
  3438. are also defined:
  3439. \begin{listing}
  3440. const
  3441. erroraddr : pointer = nil;
  3442. errorcode : word = 0;
  3443. { max level in dumping on error }
  3444. max_frame_dump : word = 20;
  3445. \end{verbatim}
  3446. \emph{ Remark: } Processor specific global constants are named Testxxxx
  3447. where xxxx represents the processor number (such as Test8086, Test68000),
  3448. and are used to determine on what generation of processor the program
  3449. is running on.
  3450. \subsection{Variables}
  3451. The following variables are defined and initialized in the system unit:
  3452. \begin{verbatim}
  3453. var
  3454. output,input,stderr : text;
  3455. exitproc : pointer;
  3456. exitcode : word;
  3457. stackbottom : Longint;
  3458. loweststack : Longint;
  3459. \end{verbatim}
  3460. The variables \var{ExitProc}, \var{exitcode} are used in the \fpc exit
  3461. scheme. It works similarly to the on in Turbo Pascal:
  3462. When a program halts (be it through the call of the \var{Halt} function or
  3463. \var{Exit} or through a run-time error), the exit mechanism checks the value
  3464. of \var{ExitProc}. If this one is non-\var{Nil}, it is set to \var{Nil}, and
  3465. the procedure is called. If the exit procedure exits, the value of ExitProc
  3466. is checked again. If it is non-\var{Nil} then the above steps are repeated.
  3467. So if you want to install your exit procedure, you should save the old value
  3468. of \var{ExitProc} (may be non-\var{Nil}, since other units could have set it before
  3469. you did). In your exit procedure you then restore the value of
  3470. \var{ExitProc}, such that if it was non-\var{Nil} the exit-procedure can be
  3471. called.
  3472. The \var{ErrorAddr} and \var{ExitCode} can be used to check for
  3473. error-conditions. If \var{ErrorAddr} is non-\var{Nil}, a run-time error has
  3474. occurred. If so, \var{ExitCode} contains the error code. If \var{ErrorAddr} is
  3475. \var{Nil}, then {ExitCode} contains the argument to \var{Halt} or 0 if the
  3476. program terminated normally.
  3477. \var{ExitCode} is always passed to the operating system as the exit-code of
  3478. your process.
  3479. Under \file{GO32}, the following constants are also defined :
  3480. \begin{verbatim}
  3481. const
  3482. seg0040 = $0040;
  3483. segA000 = $A000;
  3484. segB000 = $B000;
  3485. segB800 = $B800;
  3486. \end{verbatim}
  3487. These constants allow easy access to the bios/screen segment via mem/absolute.
  3488. \section{Functions and Procedures}
  3489. \begin{function}{Abs}
  3490. \Declaration
  3491. Function Abs (X : Every numerical type) : Every numerical type;
  3492. \Description
  3493. \var{Abs} returns the absolute value of a variable. The result of the
  3494. function has the same type as its argument, which can be any numerical
  3495. type.
  3496. \Errors
  3497. None.
  3498. \SeeAlso
  3499. \seef{Round}
  3500. \end{function}
  3501. \latex{\inputlisting{refex/ex1.pp}}
  3502. \html{\input{refex/ex1.tex}}
  3503. \begin{function}{Addr}
  3504. \Declaration
  3505. Function Addr (X : Any type) : Pointer;
  3506. \Description
  3507. \var{Addr} returns a pointer to its argument, which can be any type, or a
  3508. function or procedure name. The returned pointer isn't typed.
  3509. The same result can be obtained by the \var{@} operator, which can return a
  3510. typed pointer (\progref).
  3511. \Errors
  3512. None
  3513. \SeeAlso
  3514. \seef{SizeOf}
  3515. \end{function}
  3516. \latex{\inputlisting{refex/ex2.pp}}
  3517. \html{\input{refex/ex2.tex}}
  3518. \begin{procedure}{Append}
  3519. \Declaration
  3520. Procedure Append (Var F : Text);
  3521. \Description
  3522. \var{Append} opens an existing file in append mode. Any data written to
  3523. \var{F} will be appended to the file. If the file didn't exist, it will be
  3524. created, contrary to the Turbo Pascal implementation of \var{Append}, where
  3525. a file needed to exist in order to be opened by
  3526. append.
  3527. Only text files can be opened in append mode.
  3528. \Errors
  3529. If the file can't be created, a run-time error will be generated.
  3530. \SeeAlso
  3531. \seep{Rewrite},\seep{Append}, \seep{Reset}
  3532. \end{procedure}
  3533. \latex{\inputlisting{refex/ex3.pp}}
  3534. \html{\input{refex/ex3.tex}}
  3535. \begin{function}{Arctan}
  3536. \Declaration
  3537. Function Arctan (X : Real) : Real;
  3538. \Description
  3539. \var{Arctan} returns the Arctangent of \var{X}, which can be any Real type.
  3540. The resulting angle is in radial units.
  3541. \Errors
  3542. None
  3543. \SeeAlso
  3544. \seef{Sin}, \seef{Cos}
  3545. \end{function}
  3546. \latex{\inputlisting{refex/ex4.pp}}
  3547. \html{\input{refex/ex4.tex}}
  3548. \begin{procedure}{Assign}
  3549. \Declaration
  3550. Procedure Assign (Var F; Name : String);
  3551. \Description
  3552. \var{Assign} assigns a name to \var{F}, which can be any file type.
  3553. This call doesn't open the file, it just assigns a name to a file variable,
  3554. and marks the file as closed.
  3555. \Errors
  3556. None.
  3557. \SeeAlso
  3558. \seep{Reset}, \seep{Rewrite}, \seep{Append}
  3559. \end{procedure}
  3560. \latex{\inputlisting{refex/ex5.pp}}
  3561. \html{\input{refex/ex5.tex}}
  3562. \begin{function}{Assigned}
  3563. \Declaration
  3564. Function Assigned (P : Pointer) : Boolean;
  3565. \Description
  3566. \var{Assigned} returns \var{True} if \var{P} is non-nil
  3567. and retuns \var{False} of \var{P} is nil.
  3568. The main use of Assigned it that Procedural variables and
  3569. class-type variables also can be passed to \var{Assigned}.
  3570. \Errors
  3571. None
  3572. \SeeAlso
  3573. \end{function}
  3574. \begin{function}{BinStr}
  3575. \Declaration
  3576. Function BinStr Value : longint; cnt : byte) : String;
  3577. \Description
  3578. \var{BinStr} returns a string with the binary representation
  3579. of \var{Value}. The string has at most \var{cnt} characters.
  3580. (i.e. only the \var{cnt} rightmost bits are taken into account)
  3581. To have a complete representation of any longint-type value, you need 32
  3582. bits, i.e. \var{cnt=32}
  3583. \Errors
  3584. None.
  3585. \SeeAlso
  3586. \seep{Str},seep{Val},\seef{HexStr}
  3587. \end{function}
  3588. \latex{\inputlisting{refex/ex82.pp}}
  3589. \html{\input{refex/ex82.tex}}
  3590. \begin{procedure}{Blockread}
  3591. \Declaration
  3592. Procedure Blockread (Var F : File; Var Buffer; Var Count : Longint [; var
  3593. Result : Longint]);
  3594. \Description
  3595. \var{Blockread} reads \var{count} or less records from file \var{F}. The
  3596. result is placed in \var{Buffer}, which must contain enough room for
  3597. \var{Count} records. The function cannot read partial records.
  3598. If \var{Result} is specified, it contains the number of records actually
  3599. read. If \var{Result} isn't specified, and less than \var{Count} records were
  3600. read, a run-time error is generated. This behavior can be controlled by the
  3601. \var{\{\$i\}} switch.
  3602. \Errors
  3603. If \var{Result} isn't specified, then a run-time error is generated if less
  3604. than \var{count} records were read.
  3605. \SeeAlso
  3606. \seep{Blockwrite}, \seep{Close}, \seep{Reset}, \seep{Assign}
  3607. \end{procedure}
  3608. \latex{\inputlisting{refex/ex6.pp}}
  3609. \html{\input{refex/ex6.tex}}
  3610. \begin{procedure}{Blockwrite}
  3611. \Declaration
  3612. Procedure Blockwrite (Var F : File; Var Buffer; Var Count : Longint);
  3613. \Description
  3614. \var{BlockWrite} writes \var{count} records from \var{buffer} to the file
  3615. \var{F}.
  3616. If the records couldn't be written to disk, a run-time error is generated.
  3617. This behavior can be controlled by the \var{\{\$i\}} switch.
  3618. \Errors
  3619. A run-time error is generated if, for some reason, the records couldn't be
  3620. written to disk.
  3621. \SeeAlso
  3622. \seep{Blockread},\seep{Close}, \seep{Rewrite}, \seep{Assign}
  3623. \end{procedure}
  3624. For the example, see \seep{Blockread}.
  3625. \begin{procedure}{Chdir}
  3626. \Declaration
  3627. Procedure Chdir (const S : string);
  3628. \Description
  3629. \var{Chdir} changes the working directory of the process to \var{S}.
  3630. \Errors
  3631. If the directory \var{S} doesn't exist, a run-time error is generated.
  3632. \SeeAlso
  3633. \seep{Mkdir}, \seep{Rmdir}
  3634. \end{procedure}
  3635. \latex{\inputlisting{refex/ex7.pp}}
  3636. \html{\input{refex/ex7.tex}}
  3637. \begin{function}{Chr}
  3638. \Declaration
  3639. Function Chr (X : byte) : Char;
  3640. \Description
  3641. \var{Chr} returns the character which has ASCII value \var{X}.
  3642. \Errors
  3643. None.
  3644. \SeeAlso
  3645. \seef{Ord},\seep{Str}
  3646. \end{function}
  3647. \latex{\inputlisting{refex/ex8.pp}}
  3648. \html{\input{refex/ex8.tex}}
  3649. \begin{procedure}{Close}
  3650. \Declaration
  3651. Procedure Close (Var F : Anyfiletype);
  3652. \Description
  3653. \var{Close} flushes the buffer of the file \var{F} and closes \var{F}.
  3654. After a call to \var{Close}, data can no longer be read from or written to
  3655. \var{F}.
  3656. To reopen a file closed with \var{Close}, it isn't necessary to assign the
  3657. file again. A call to \seep{Reset} or \seep{Rewrite} is sufficient.
  3658. \Errors
  3659. None.
  3660. \SeeAlso
  3661. \seep{Assign}, \seep{Reset}, \seep{Rewrite}
  3662. \end{procedure}
  3663. \latex{\inputlisting{refex/ex9.pp}}
  3664. \html{\input{refex/ex9.tex}}
  3665. \begin{function}{Concat}
  3666. \Declaration
  3667. Function Concat (S1,S2 [,S3, ... ,Sn]) : String;
  3668. \Description
  3669. \var{Concat} concatenates the strings \var{S1},\var{S2} etc. to one long
  3670. string. The resulting string is truncated at a length of 255 bytes.
  3671. The same operation can be performed with the \var{+} operation.
  3672. \Errors
  3673. None.
  3674. \SeeAlso
  3675. \seef{Copy}, \seep{Delete}, \seep{Insert}, \seef{Pos}, \seef{Length}
  3676. \end{function}
  3677. \latex{\inputlisting{refex/ex10.pp}}
  3678. \html{\input{refex/ex10.tex}}
  3679. \begin{function}{Copy}
  3680. \Declaration
  3681. Function Copy (Const S : String;Index : Integer;Count : Byte) : String;
  3682. \Description
  3683. \var{Copy} returns a string which is a copy if the \var{Count} characters
  3684. in \var{S}, starting at position \var{Index}. If \var{Count} is larger than
  3685. the length of the string \var{S}, the result is truncated.
  3686. If \var{Index} is larger than the length of the string \var{S}, then an
  3687. empty string is returned.
  3688. \Errors
  3689. None.
  3690. \SeeAlso
  3691. \seep{Delete}, \seep{Insert}, \seef{Pos}
  3692. \end{function}
  3693. \latex{\inputlisting{refex/ex11.pp}}
  3694. \html{\input{refex/ex11.tex}}
  3695. \begin{function}{Cos}
  3696. \Declaration
  3697. Function Cos (X : Real) : Real;
  3698. \Description
  3699. \var{Cos} returns the cosine of \var{X}, where X is an angle, in radians.
  3700. \Errors
  3701. None.
  3702. \SeeAlso
  3703. \seef{Arctan}, \seef{Sin}
  3704. \end{function}
  3705. \latex{\inputlisting{refex/ex12.pp}}
  3706. \html{\input{refex/ex12.tex}}
  3707. \begin{function}{CSeg}
  3708. \Declaration
  3709. Function CSeg : Word;
  3710. \Description
  3711. \var{CSeg} returns the Code segment register. In \fpc, it returns always a
  3712. zero, since \fpc is a 32 bit compiler.
  3713. \Errors
  3714. None.
  3715. \SeeAlso
  3716. \seef{DSeg}, \seef{Seg}, \seef{Ofs}, \seef{Ptr}
  3717. \end{function}
  3718. \latex{\inputlisting{refex/ex13.pp}}
  3719. \html{\input{refex/ex13.tex}}
  3720. \begin{procedure}{Dec}
  3721. \Declaration
  3722. Procedure Dec (Var X : Any ordinal type[; Decrement : Longint]);
  3723. \Description
  3724. \var{Dec} decreases the value of \var{X} with \var{Decrement}.
  3725. If \var{Decrement} isn't specified, then 1 is taken as a default.
  3726. \Errors
  3727. A range check can occur, or an underflow error, if you try to decrease \var{X}
  3728. below its minimum value.
  3729. \SeeAlso
  3730. \seep{Inc}
  3731. \end{procedure}
  3732. \latex{\inputlisting{refex/ex14.pp}}
  3733. \html{\input{refex/ex14.tex}}
  3734. \begin{procedure}{Delete}
  3735. \Declaration
  3736. Procedure Delete (var S : string;Index : Integer;Count : Integer);
  3737. \Description
  3738. \var{Delete} removes \var{Count} characters from string \var{S}, starting
  3739. at position \var{Index}. All remaining characters are shifted \var{Count}
  3740. positions to the left, and the length of the string is adjusted.
  3741. \Errors
  3742. None.
  3743. \SeeAlso
  3744. \seef{Copy},\seef{Pos},\seep{Insert}
  3745. \end{procedure}
  3746. \latex{\inputlisting{refex/ex15.pp}}
  3747. \html{\input{refex/ex15.tex}}
  3748. \begin{procedure}{Dispose}
  3749. \Declaration
  3750. Procedure Dispose (P : pointer);
  3751. \Description
  3752. \var{Dispose} releases the memory allocated with a call to \seep{New}.
  3753. The pointer \var{P} must be typed. The released memory is returned to the
  3754. heap.
  3755. \Errors
  3756. An error will occur if the pointer doesn't point to a location in the
  3757. heap.
  3758. \SeeAlso
  3759. \seep{New}, \seep{Getmem}, \seep{Freemem}
  3760. \end{procedure}
  3761. \latex{\inputlisting{refex/ex16.pp}}
  3762. \html{\input{refex/ex16.tex}}
  3763. \begin{function}{DSeg}
  3764. \Declaration
  3765. Function DSeg : Word;
  3766. \Description
  3767. \var{DSeg} returns the data segment register. In \fpc, it returns always a
  3768. zero, since \fpc is a 32 bit compiler.
  3769. \Errors
  3770. None.
  3771. \SeeAlso
  3772. \seef{CSeg}, \seef{Seg}, \seef{Ofs}, \seef{Ptr}
  3773. \end{function}
  3774. \latex{\inputlisting{refex/ex17.pp}}
  3775. \html{\input{refex/ex17.tex}}
  3776. \begin{function}{Eof}
  3777. \Declaration
  3778. Function Eof [(F : Any file type)] : Boolean;
  3779. \Description
  3780. \var{Eof} returns \var{True} if the file-pointer has reached the end of the
  3781. file, or if the file is empty. In all other cases \var{Eof} returns
  3782. \var{False}.
  3783. If no file \var{F} is specified, standard input is assumed.
  3784. \Errors
  3785. None.
  3786. \SeeAlso
  3787. \seef{Eoln}, \seep{Assign}, \seep{Reset}, \seep{Rewrite}
  3788. \end{function}
  3789. \latex{\inputlisting{refex/ex18.pp}}
  3790. \html{\input{refex/ex18.tex}}
  3791. \begin{function}{Eoln}
  3792. \Declaration
  3793. Function Eoln [(F : Text)] : Boolean;
  3794. \Description
  3795. \var{Eof} returns \var{True} if the file pointer has reached the end of a
  3796. line, which is demarcated by a line-feed character (ASCII value 10), or if
  3797. the end of the file is reached.
  3798. In all other cases \var{Eof} returns \var{False}.
  3799. If no file \var{F} is specified, standard input is assumed.
  3800. It can only be used on files of type \var{Text}.
  3801. \Errors
  3802. None.
  3803. \SeeAlso
  3804. \seef{Eof}, \seep{Assign}, \seep{Reset}, \seep{Rewrite}
  3805. \end{function}
  3806. \latex{\inputlisting{refex/ex19.pp}}
  3807. \html{\input{refex/ex19.tex}}
  3808. \begin{procedure}{Erase}
  3809. \Declaration
  3810. Procedure Erase (Var F : Any file type);
  3811. \Description
  3812. \var{Erase} removes an unopened file from disk. The file should be
  3813. assigned with \var{Assign}, but not opened with \var{Reset} or \var{Rewrite}
  3814. \Errors
  3815. A run-time error will be generated if the specified file doesn't exist.
  3816. \SeeAlso
  3817. \seep{Assign}
  3818. \end{procedure}
  3819. \latex{\inputlisting{refex/ex20.pp}}
  3820. \html{\input{refex/ex20.tex}}
  3821. \begin{procedure}{Exit}
  3822. \Declaration
  3823. Procedure Exit ([Var X : return type )];
  3824. \Description
  3825. \var{Exit} exits the current subroutine, and returns control to the calling
  3826. routine. If invoked in the main program routine, exit stops the program.
  3827. The optional argument \var{X} allows to specify a return value, in the case
  3828. \var{Exit} is invoked in a function. The function result will then be
  3829. equal to \var{X}.
  3830. \Errors
  3831. None.
  3832. \SeeAlso
  3833. \seep{Halt}
  3834. \end{procedure}
  3835. \latex{\inputlisting{refex/ex21.pp}}
  3836. \html{\input{refex/ex21.tex}}
  3837. \begin{function}{Exp}
  3838. \Declaration
  3839. Function Exp (Var X : Real) : Real;
  3840. \Description
  3841. \var{Exp} returns the exponent of \var{X}, i.e. the number \var{e} to the
  3842. power \var{X}.
  3843. \Errors
  3844. None.
  3845. \SeeAlso
  3846. \seef{Ln}, \seef{Power}
  3847. \end{function}
  3848. \latex{\inputlisting{refex/ex22.pp}}
  3849. \html{\input{refex/ex22.tex}}
  3850. \begin{function}{Filepos}
  3851. \Declaration
  3852. Function Filepos (Var F : Any file type) : Longint;
  3853. \Description
  3854. \var{Filepos} returns the current record position of the file-pointer in file
  3855. \var{F}. It cannot be invoked with a file of type \var{Text}.
  3856. \Errors
  3857. None.
  3858. \SeeAlso
  3859. \seef{Filesize}
  3860. \end{function}
  3861. \latex{\inputlisting{refex/ex23.pp}}
  3862. \html{\input{refex/ex23.tex}}
  3863. \begin{function}{Filesize}
  3864. \Declaration
  3865. Function Filesize (Var F : Any file type) : Longint;
  3866. \Description
  3867. \var{Filepos} returns the total number of records in file \var{F}.
  3868. It cannot be invoked with a file of type \var{Text}. (under \linux, this
  3869. also means that it cannot be invoked on pipes.)
  3870. If \var{F} is empty, 0 is returned.
  3871. \Errors
  3872. None.
  3873. \SeeAlso
  3874. \seef{Filepos}
  3875. \end{function}
  3876. \latex{\inputlisting{refex/ex24.pp}}
  3877. \html{\input{refex/ex24.tex}}
  3878. \begin{procedure}{Fillchar}
  3879. \Declaration
  3880. Procedure Fillchar (Var X;Count : Longint;Value : char or byte);;
  3881. \Description
  3882. \var{Fillchar} fills the memory starting at \var{X} with \var{Count} bytes
  3883. or characters with value equal to \var{Value}.
  3884. \Errors
  3885. No checking on the size of \var{X} is done.
  3886. \SeeAlso
  3887. \seep{Fillword}, \seep{Move}
  3888. \end{procedure}
  3889. \latex{\inputlisting{refex/ex25.pp}}
  3890. \html{\input{refex/ex25.tex}}
  3891. \begin{procedure}{Fillword}
  3892. \Declaration
  3893. Procedure Fillword (Var X;Count : Longint;Value : Word);;
  3894. \Description
  3895. \var{Fillword} fills the memory starting at \var{X} with \var{Count} words
  3896. with value equal to \var{Value}.
  3897. \Errors
  3898. No checking on the size of \var{X} is done.
  3899. \SeeAlso
  3900. \seep{Fillword}, \seep{Move}
  3901. \end{procedure}
  3902. \latex{\inputlisting{refex/ex76.pp}}
  3903. \html{\input{refex/ex76.tex}}
  3904. \begin{procedure}{Flush}
  3905. \Declaration
  3906. Procedure Flush (Var F : Text);
  3907. \Description
  3908. \var{Flush} empties the internal buffer of file \var{F} and writes the
  3909. contents to disk. The file is \textit{not} closed as a result of this call.
  3910. \Errors
  3911. If the disk is full, a run-time error will be generated.
  3912. \SeeAlso
  3913. \seep{Close}
  3914. \end{procedure}
  3915. \latex{\inputlisting{refex/ex26.pp}}
  3916. \html{\input{refex/ex26.tex}}
  3917. \begin{function}{Frac}
  3918. \Declaration
  3919. Function Frac (X : Real) : Real;
  3920. \Description
  3921. \var{Frac} returns the non-integer part of \var{X}.
  3922. \Errors
  3923. None.
  3924. \SeeAlso
  3925. \seef{Round}, \seef{Int}
  3926. \end{function}
  3927. \latex{\inputlisting{refex/ex27.pp}}
  3928. \html{\input{refex/ex27.tex}}
  3929. \begin{procedure}{Freemem}
  3930. \Declaration
  3931. Procedure Freemem (Var P : pointer; Count : Longint);
  3932. \Description
  3933. \var{Freemem} releases the memory occupied by the pointer \var{P}, of size
  3934. \var{Count}, and returns it to the heap. \var{P} should point to the memory
  3935. allocated to a dynamical variable.
  3936. \Errors
  3937. An error will occur when \var{P} doesn't point to the heap.
  3938. \SeeAlso
  3939. \seep{Getmem}, \seep{New}, \seep{Dispose}
  3940. \end{procedure}
  3941. \latex{\inputlisting{refex/ex28.pp}}
  3942. \html{\input{refex/ex28.tex}}
  3943. \begin{procedure}{Getdir}
  3944. \Declaration
  3945. Procedure Getdir (drivenr : byte;var dir : string);
  3946. \Description
  3947. \var{Getdir} returns in \var{dir} the current directory on the drive
  3948. \var{drivenr}, where {drivenr} is 1 for the first floppy drive, 3 for the
  3949. first hard disk etc. A value of 0 returns the directory on the current disk.
  3950. On \linux, \var{drivenr} is ignored, as there is only one directory tree.
  3951. \Errors
  3952. An error is returned under \dos, if the drive requested isn't ready.
  3953. \SeeAlso
  3954. \seep{Chdir}
  3955. \end{procedure}
  3956. \latex{\inputlisting{refex/ex29.pp}}
  3957. \html{\input{refex/ex29.tex}}
  3958. \begin{procedure}{Getmem}
  3959. \Declaration
  3960. Procedure Getmem (var p : pointer;size : Longint);
  3961. \Description
  3962. \var{Getmem} reserves \var{Size} bytes memory on the heap, and returns a
  3963. pointer to this memory in \var{p}. If no more memory is available, nil is
  3964. returned.
  3965. \Errors
  3966. None.
  3967. \SeeAlso
  3968. \seep{Freemem}, \seep{Dispose}, \seep{New}
  3969. \end{procedure}
  3970. For an example, see \seep{Freemem}.
  3971. \begin{procedure}{Halt}
  3972. \Declaration
  3973. Procedure Halt [(Errnum : byte];
  3974. \Description
  3975. \var{Halt} stops program execution and returns control to the calling
  3976. program. The optional argument \var{Errnum} specifies an exit value. If
  3977. omitted, zero is returned.
  3978. \Errors
  3979. None.
  3980. \SeeAlso
  3981. \seep{Exit}
  3982. \end{procedure}
  3983. \latex{\inputlisting{refex/ex30.pp}}
  3984. \html{\input{refex/ex30.tex}}
  3985. \begin{function}{HexStr}
  3986. \Declaration
  3987. Function HexStr Value : longint; cnt : byte) : String;
  3988. \Description
  3989. \var{HexStr} returns a string with the hexadecimal representation
  3990. of \var{Value}. The string has at most \var{cnt} charaters.
  3991. (i.e. only the \var{cnt} rightmost nibbles are taken into account)
  3992. To have a complete representation of a Longint-type value, you need 8
  3993. nibbles, i.e. \var{cnt=8}.
  3994. \Errors
  3995. None.
  3996. \SeeAlso
  3997. \seep{Str},seep{Val},\seef{BinStr}
  3998. \end{function}
  3999. \latex{\inputlisting{refex/ex81.pp}}
  4000. \html{\input{refex/ex81.tex}}
  4001. \begin{function}{Hi}
  4002. \Declaration
  4003. Function Hi (X : Ordinal type) : Word or byte;
  4004. \Description
  4005. \var{Hi} returns the high byte or word from \var{X}, depending on the size
  4006. of X. If the size of X is 4, then the high word is returned. If the size is
  4007. 2 then the high byte is retuned.
  4008. \var{hi} cannot be invoked on types of size 1, such as byte or char.
  4009. \Errors
  4010. None
  4011. \SeeAlso
  4012. \seef{Lo}
  4013. \end{function}
  4014. \latex{\inputlisting{refex/ex31.pp}}
  4015. \html{\input{refex/ex31.tex}}
  4016. \begin{function}{High}
  4017. \Declaration
  4018. Function High (Type identifier or variable reference) : Longint;
  4019. \Description
  4020. The return value of \var{High} depends on it's argument:
  4021. \begin{enumerate}
  4022. \item If the argument is an ordinal type, \var{High} returns the lowest value in the range of the given ordinal
  4023. type when it gets.
  4024. \item If the argument is an array type or an array type variable then
  4025. \var{High} returns the highest possible value of it's index.
  4026. \item If the argument is an open array identifier in a function or
  4027. procedure, then \var{High} returns the highest index of the array, as if the
  4028. array has a zero-based index.
  4029. \end{enumerate}
  4030. \Errors
  4031. None.
  4032. \SeeAlso
  4033. \seef{High}, \seef{Ord}, \seef{Pred}, \seef{Succ}
  4034. \end{function}
  4035. \latex{\inputlisting{refex/ex80.pp}}
  4036. \html{\input{refex/ex80.tex}}
  4037. \begin{procedure}{Inc}
  4038. \Declaration
  4039. Procedure Inc (Var X : Any ordinal type[; Increment : Longint]);
  4040. \Description
  4041. \var{Inc} increases the value of \var{X} with \var{Increment}.
  4042. If \var{Increment} isn't specified, then 1 is taken as a default.
  4043. \Errors
  4044. A range check can occur, or an overflow error, if you try to increase \var{X}
  4045. over its maximum value.
  4046. \SeeAlso
  4047. \seep{Dec}
  4048. \end{procedure}
  4049. \latex{\inputlisting{refex/ex32.pp}}
  4050. \html{\input{refex/ex32.tex}}
  4051. \begin{procedure}{Insert}
  4052. \Declaration
  4053. Procedure Insert (Const Source : String;var S : String;Index : Longint);
  4054. \Description
  4055. \var{Insert} inserts string \var{Source} in string \var{S}, at position
  4056. \var{Index}, shifting all characters after \var{Index} to the right. The
  4057. resulting string is truncated at 255 characters, if needed.
  4058. \Errors
  4059. None.
  4060. \SeeAlso
  4061. \seep{Delete}, \seef{Copy}, \seef{Pos}
  4062. \end{procedure}
  4063. \latex{\inputlisting{refex/ex33.pp}}
  4064. \html{\input{refex/ex33.tex}}
  4065. \begin{function}{Int}
  4066. \Declaration
  4067. Function Int (X : Real) : Real;
  4068. \Description
  4069. \var{Int} returns the integer part of any Real \var{X}, as a Real.
  4070. \Errors
  4071. None.
  4072. \SeeAlso
  4073. \seef{Frac}, \seef{Round}
  4074. \end{function}
  4075. \latex{\inputlisting{refex/ex34.pp}}
  4076. \html{\input{refex/ex34.tex}}
  4077. \begin{function}{IOresult}
  4078. \Declaration
  4079. Function IOresult : Word;
  4080. \Description
  4081. IOresult contains the result of any input/output call, when the
  4082. \var{\{\$i-\}} compiler directive is active, and IO checking is disabled. When the
  4083. flag is read, it is reset to zero.
  4084. If \var{IOresult} is zero, the operation completed successfully. If
  4085. non-zero, an error occurred. The following errors can occur:
  4086. \dos errors :
  4087. \begin{description}
  4088. \item [2\ ] File not found.
  4089. \item [3\ ] Path not found.
  4090. \item [4\ ] Too many open files.
  4091. \item [5\ ] Access denied.
  4092. \item [6\ ] Invalid file handle.
  4093. \item [12\ ] Invalid file-access mode.
  4094. \item [15\ ] Invalid disk number.
  4095. \item [16\ ] Cannot remove current directory.
  4096. \item [17\ ] Cannot rename across volumes.
  4097. \end{description}
  4098. I/O errors :
  4099. \begin{description}
  4100. \item [100\ ] Error when reading from disk.
  4101. \item [101\ ] Error when writing to disk.
  4102. \item [102\ ] File not assigned.
  4103. \item [103\ ] File not open.
  4104. \item [104\ ] File not opened for input.
  4105. \item [105\ ] File not opened for output.
  4106. \item [106\ ] Invalid number.
  4107. \end{description}
  4108. Fatal errors :
  4109. \begin{description}
  4110. \item [150\ ] Disk is write protected.
  4111. \item [151\ ] Unknown device.
  4112. \item [152\ ] Drive not ready.
  4113. \item [153\ ] Unknown command.
  4114. \item [154\ ] CRC check failed.
  4115. \item [155\ ] Invalid drive specified..
  4116. \item [156\ ] Seek error on disk.
  4117. \item [157\ ] Invalid media type.
  4118. \item [158\ ] Sector not found.
  4119. \item [159\ ] Printer out of paper.
  4120. \item [160\ ] Error when writing to device.
  4121. \item [161\ ] Error when reading from device.
  4122. \item [162\ ] Hardware failure.
  4123. \end{description}
  4124. \Errors
  4125. None.
  4126. \SeeAlso
  4127. All I/O functions.
  4128. \end{function}
  4129. \latex{\inputlisting{refex/ex35.pp}}
  4130. \html{\input{refex/ex35.tex}}
  4131. \begin{function}{Length}
  4132. \Declaration
  4133. Function Length (S : String) : Byte;
  4134. \Description
  4135. \var{Length} returns the length of the string \var{S},
  4136. which is limited to 255. If the strings \var{S} is empty, 0 is returned.
  4137. {\em Note:} The length of the string \var{S} is stored in \var{S[0]}.
  4138. \Errors
  4139. None.
  4140. \SeeAlso
  4141. \seef{Pos}
  4142. \end{function}
  4143. \latex{\inputlisting{refex/ex36.pp}}
  4144. \html{\input{refex/ex36.tex}}
  4145. \begin{function}{Ln}
  4146. \Declaration
  4147. Function Ln (X : Real) : Real;
  4148. \Description
  4149. \var{Ln} returns the natural logarithm of the Real parameter \var{X}.
  4150. \var{X} must be positive.
  4151. \Errors
  4152. An run-time error will occur when \var{X} is negative.
  4153. \SeeAlso
  4154. \seef{Exp}, \seef{Power}
  4155. \end{function}
  4156. \latex{\inputlisting{refex/ex37.pp}}
  4157. \html{\input{refex/ex37.tex}}
  4158. \begin{function}{Lo}
  4159. \Declaration
  4160. Function Lo (O : Word or Longint) : Byte or Word;
  4161. \Description
  4162. \var{Lo} returns the low byte of its argument if this is of type
  4163. \var{Integer} or
  4164. \var{Word}. It returns the low word of its argument if this is of type
  4165. \var{Longint} or \var{Cardinal}.
  4166. \Errors
  4167. None.
  4168. \SeeAlso
  4169. \seef{Ord}, \seef{Chr}
  4170. \end{function}
  4171. \latex{\inputlisting{refex/ex38.pp}}
  4172. \html{\input{refex/ex38.tex}}
  4173. \begin{procedure}{LongJmp}
  4174. \Declaration
  4175. Procedure LongJmp (Var env : Jmp\_Buf; Value : Longint);
  4176. \Description
  4177. \var{LongJmp} jumps to the adress in the \var{env} \var{jmp\_buf},
  4178. and resores the registers that were stored in it at the corresponding
  4179. \seef{SetJmp} call.
  4180. In effect, program flow will continue at the \var{SetJmp} call, which will
  4181. return \var{value} instead of 0. If you pas a \var{value} equal to zero, it will be
  4182. converted to 1 before passing it on. The call will not return, so it must be
  4183. used with extreme care.
  4184. This can be used for error recovery, for instance when a segmentation fault
  4185. occurred.
  4186. \Errors
  4187. None.
  4188. \SeeAlso
  4189. \seef{SetJmp}
  4190. \end{procedure}
  4191. For an example, see \seef{SetJmp}
  4192. \begin{function}{Low}
  4193. \Declaration
  4194. Function Low (Type identifier or variable reference) : Longint;
  4195. \Description
  4196. The return value of \var{Low} depends on it's argument:
  4197. \begin{enumerate}
  4198. \item If the argument is an ordinal type, \var{Low} returns the lowest value in the range of the given ordinal
  4199. type when it gets.
  4200. \item If the argument is an array type or an array type variable then
  4201. \var{Low} returns the lowest possible value of it's index.
  4202. \end{enumerate}
  4203. \Errors
  4204. None.
  4205. \SeeAlso
  4206. \seef{High}, \seef{Ord}, \seef{Pred}, \seef{Succ}
  4207. \end{function}
  4208. for an example, see \seef{High}.
  4209. \begin{function}{Lowercase}
  4210. \Declaration
  4211. Function Lowercase (C : Char or String) : Char or String;
  4212. \Description
  4213. \var{Lowercase} returns the lowercase version of its argument \var{C}.
  4214. If its argument is a string, then the complete string is converted to
  4215. lowercase. The type of the returned value is the same as the type of the
  4216. argument.
  4217. \Errors
  4218. None.
  4219. \SeeAlso
  4220. \seef{Upcase}
  4221. \end{function}
  4222. \latex{\inputlisting{refex/ex73.pp}}
  4223. \html{\input{refex/ex73.tex}}
  4224. \begin{procedure}{Mark}
  4225. \Declaration
  4226. Procedure Mark (Var P : Pointer);
  4227. \Description
  4228. \var{Mark} copies the current heap-pointer to \var{P}.
  4229. \Errors
  4230. None.
  4231. \SeeAlso
  4232. \seep{Getmem}, \seep{Freemem}, \seep{New}, \seep{Dispose}, \seef{Maxavail}
  4233. \end{procedure}
  4234. \latex{\inputlisting{refex/ex39.pp}}
  4235. \html{\input{refex/ex39.tex}}
  4236. \begin{function}{Maxavail}
  4237. \Declaration
  4238. Function Maxavail : Longint;
  4239. \Description
  4240. \var{Maxavail} returns the size, in bytes, of the biggest free memory block in
  4241. the heap.
  4242. {\em Remark:} The heap grows dynamically if more memory is needed than is
  4243. available.
  4244. \Errors
  4245. None.
  4246. \SeeAlso
  4247. \seep{Release}, \seef{Memavail},\seep{Freemem}, \seep{Getmem}
  4248. \end{function}
  4249. \latex{\inputlisting{refex/ex40.pp}}
  4250. \html{\input{refex/ex40.tex}}
  4251. \begin{function}{Memavail}
  4252. \Declaration
  4253. Function Memavail : Longint;
  4254. \Description
  4255. \var{Memavail} returns the size, in bytes, of the free heap memory.
  4256. {\em Remark:} The heap grows dynamically if more memory is needed than is
  4257. available.
  4258. \Errors
  4259. None.
  4260. \SeeAlso
  4261. \seef{Maxavail},\seep{Freemem}, \seep{Getmem}
  4262. \end{function}
  4263. \latex{\inputlisting{refex/ex41.pp}}
  4264. \html{\input{refex/ex41.tex}}
  4265. \begin{procedure}{Mkdir}
  4266. \Declaration
  4267. Procedure Mkdir (const S : string);
  4268. \Description
  4269. \var{Chdir} creates a new directory \var{S}.
  4270. \Errors
  4271. If a parent-directory of directory \var{S} doesn't exist, a run-time error is generated.
  4272. \SeeAlso
  4273. \seep{Chdir}, \seep{Rmdir}
  4274. \end{procedure}
  4275. For an example, see \seep{Rmdir}.
  4276. \begin{procedure}{Move}
  4277. \Declaration
  4278. Procedure Move (var Source,Dest;Count : Longint);
  4279. \Description
  4280. \var{Move} moves \var{Count} bytes from \var{Source} to \var{Dest}.
  4281. \Errors
  4282. If either \var{Dest} or \var{Source} is outside the accessible memory for
  4283. the process, then a run-time error will be generated. With older versions of
  4284. the compiler, a segmentation-fault will occur.
  4285. \SeeAlso
  4286. \seep{Fillword}, \seep{Fillchar}
  4287. \end{procedure}
  4288. \latex{\inputlisting{refex/ex42.pp}}
  4289. \html{\input{refex/ex42.tex}}
  4290. \begin{procedure}{New}
  4291. \Declaration
  4292. Procedure New (Var P : Pointer[, Constructor]);
  4293. \Description
  4294. \var{New} allocates a new instance of the type pointed to by \var{P}, and
  4295. puts the address in \var{P}.
  4296. If P is an object, then it is possible to
  4297. specify the name of the constructor with which the instance will be created.
  4298. \Errors
  4299. If not enough memory is available, \var{Nil} will be returned.
  4300. \SeeAlso
  4301. \seep{Dispose}, \seep{Freemem}, \seep{Getmem}, \seef{Memavail},
  4302. \seef{Maxavail}
  4303. \end{procedure}
  4304. For an example, see \seep{Dispose}.
  4305. \begin{function}{Odd}
  4306. \Declaration
  4307. Function Odd (X : Longint) : Boolean;
  4308. \Description
  4309. \var{Odd} returns \var{True} if \var{X} is odd, or \var{False} otherwise.
  4310. \Errors
  4311. None.
  4312. \SeeAlso
  4313. \seef{Abs}, \seef{Ord}
  4314. \end{function}
  4315. \latex{\inputlisting{refex/ex43.pp}}
  4316. \html{\input{refex/ex43.tex}}
  4317. \begin{function}{Ofs}
  4318. \Declaration
  4319. Function Ofs Var X : Longint;
  4320. \Description
  4321. \var{Ofs} returns the offset of the address of a variable.
  4322. This function is only supported for compatibility. In \fpc, it
  4323. returns always the complete address of the variable, since \fpc is a 32 bit
  4324. compiler.
  4325. \Errors
  4326. None.
  4327. \SeeAlso
  4328. \seef{DSeg}, \seef{CSeg}, \seef{Seg}, \seef{Ptr}
  4329. \end{function}
  4330. \latex{\inputlisting{refex/ex44.pp}}
  4331. \html{\input{refex/ex44.tex}}
  4332. \begin{function}{Ord}
  4333. \Declaration
  4334. Function Ord (X : Any ordinal type) : Longint;
  4335. \Description
  4336. \var{Ord} returns the Ordinal value of a ordinal-type variable \var{X}.
  4337. \Errors
  4338. None.
  4339. \SeeAlso
  4340. \seef{Chr}, \seef{Ord}, \seef{Pred}, \seef{High}, \seef{Low}
  4341. \end{function}
  4342. \latex{\inputlisting{refex/ex45.pp}}
  4343. \html{\input{refex/ex45.tex}}
  4344. \begin{function}{Paramcount}
  4345. \Declaration
  4346. Function Paramcount : Longint;
  4347. \Description
  4348. \var{Paramcount} returns the number of command-line arguments. If no
  4349. arguments were given to the running program, \var{0} is returned.
  4350. \Errors
  4351. None.
  4352. \SeeAlso
  4353. \seef{Paramstr}
  4354. \end{function}
  4355. \latex{\inputlisting{refex/ex46.pp}}
  4356. \html{\input{refex/ex46.tex}}
  4357. \begin{function}{Paramstr}
  4358. \Declaration
  4359. Function Paramstr (L : Longint) : String;
  4360. \Description
  4361. \var{Paramstr} returns the \var{L}-th command-line argument. \var{L} must
  4362. be between \var{0} and \var{Paramcount}, these values included.
  4363. The zeroth argument is the name with which the program was started.
  4364. \Errors
  4365. In all cases, the command-line will be truncated to a length of 255,
  4366. even though the operating system may support bigger command-lines. If you
  4367. want to access the complete command-line, you must use the \var{argv} pointer
  4368. to access the Real values of the command-line parameters.
  4369. \SeeAlso
  4370. \seef{Paramcount}
  4371. \end{function}
  4372. For an example, see \seef{Paramcount}.
  4373. \begin{function}{Pi}
  4374. \Declaration
  4375. Function Pi : Real;
  4376. \Description
  4377. \var{Pi} returns the value of Pi (3.1415926535897932385).
  4378. \Errors
  4379. None.
  4380. \SeeAlso
  4381. \seef{Cos}, \seef{Sin}
  4382. \end{function}
  4383. \latex{\inputlisting{refex/ex47.pp}}
  4384. \html{\input{refex/ex47.tex}}
  4385. \begin{function}{Pos}
  4386. \Declaration
  4387. Function Pos (Const Substr : String;Const S : String) : Byte;
  4388. \Description
  4389. \var{Pos} returns the index of \var{Substr} in \var{S}, if \var{S} contains
  4390. \var{Substr}. In case \var{Substr} isn't found, \var{0} is returned.
  4391. The search is case-sensitive.
  4392. \Errors
  4393. None
  4394. \SeeAlso
  4395. \seef{Length}, \seef{Copy}, \seep{Delete}, \seep{Insert}
  4396. \end{function}
  4397. \latex{\inputlisting{refex/ex48.pp}}
  4398. \html{\input{refex/ex48.tex}}
  4399. \begin{function}{Power}
  4400. \Declaration
  4401. Function Power (base,expon : Real) : Real;
  4402. \Description
  4403. \var{Power} returns the value of \var{base} to the power \var{expon}.
  4404. \var{Base} and \var{expon} can be of type Longint, in which case the
  4405. result will also be a Longint.
  4406. The function actually returns \var{Exp(expon*Ln(base))}
  4407. \Errors
  4408. None.
  4409. \SeeAlso
  4410. \seef{Exp}, \seef{Ln}
  4411. \end{function}
  4412. \latex{\inputlisting{refex/ex78.pp}}
  4413. \html{\input{refex/ex78.tex}}
  4414. \begin{function}{Pred}
  4415. \Declaration
  4416. Function Pred (X : Any ordinal type) : Same type;
  4417. \Description
  4418. \var{Pred} returns the element that precedes the element that was passed
  4419. to it. If it is applied to the first value of the ordinal type, and the
  4420. program was compiled with range checking on (\var{\{\$R+\}}, then a run-time
  4421. error will be generated.
  4422. \Errors
  4423. Run-time error 201 is generated when the result is out of
  4424. range.
  4425. \SeeAlso
  4426. \seef{Ord}, \seef{Pred}, \seef{High}, \seef{Low}
  4427. \end{function}
  4428. for an example, see \seef{Ord}
  4429. \latex{\inputlisting{refex/ex80.pp}}
  4430. \html{\input{refex/ex80.tex}}
  4431. \begin{function}{Ptr}
  4432. \Declaration
  4433. Function Ptr (Sel,Off : Longint) : Pointer;
  4434. \Description
  4435. \var{Ptr} returns a pointer, pointing to the address specified by
  4436. segment \var{Sel} and offset \var{Off}.
  4437. {\em Remark 1:} In the 32-bit flat-memory model supported by \fpc, this
  4438. function is obsolete.
  4439. {\em Remark 2:} The returned address is simply the offset. If you recompile
  4440. the RTL with \var{-dDoMapping} defined, then the compiler returns the
  4441. following : \var{ptr := pointer(\$e0000000+sel shl 4+off)} under \dos, or
  4442. \var{ptr := pointer(sel shl 4+off)} on other OSes.
  4443. \Errors
  4444. None.
  4445. \SeeAlso
  4446. \seef{Addr}
  4447. \end{function}
  4448. \latex{\inputlisting{refex/ex59.pp}}
  4449. \html{\input{refex/ex59.tex}}
  4450. \begin{function}{Random}
  4451. \Declaration
  4452. Function Random [(L : Longint)] : Longint or Real;
  4453. \Description
  4454. \var{Random} returns a random number larger or equal to \var{0} and
  4455. strictly less than \var{L}.
  4456. If the argument \var{L} is omitted, a Real number between 0 and 1 is returned.
  4457. (0 included, 1 excluded)
  4458. \Errors
  4459. None.
  4460. \SeeAlso
  4461. \seep{Randomize}
  4462. \end{function}
  4463. \latex{\inputlisting{refex/ex49.pp}}
  4464. \html{\input{refex/ex49.tex}}
  4465. \begin{procedure}{Randomize}
  4466. \Declaration
  4467. Procedure Randomize ;
  4468. \Description
  4469. \var{Randomize} initializes the random number generator of \fpc, by giving
  4470. a value to \var{Randseed}, calculated with the system clock.
  4471. \Errors
  4472. None.
  4473. \SeeAlso
  4474. \seef{Random}
  4475. \end{procedure}
  4476. For an example, see \seef{Random}.
  4477. \begin{procedure}{Read}
  4478. \Declaration
  4479. Procedure Read ([Var F : Any file type], V1 [, V2, ... , Vn]);
  4480. \Description
  4481. \var{Read} reads one or more values from a file \var{F}, and stores the
  4482. result in \var{V1}, \var{V2}, etc.; If no file \var{F} is specified, then
  4483. standard input is read.
  4484. If \var{F} is of type \var{Text}, then the variables \var{V1, V2} etc. must be
  4485. of type \var{Char}, \var{Integer}, \var{Real} or \var{String}.
  4486. If \var{F} is a typed file, then each of the variables must be of the type
  4487. specified in the declaration of \var{F}. Untyped files are not allowed as an
  4488. argument.
  4489. \Errors
  4490. If no data is available, a run-time error is generated. This behavior can
  4491. be controlled with the \var{\{\$i\}} compiler switch.
  4492. \SeeAlso
  4493. \seep{Readln}, \seep{Blockread}, \seep{Write}, \seep{Blockwrite}
  4494. \end{procedure}
  4495. \latex{\inputlisting{refex/ex50.pp}}
  4496. \html{\input{refex/ex50.tex}}
  4497. \begin{procedure}{Readln}
  4498. \Declaration
  4499. Procedure Readln [Var F : Text], V1 [, V2, ... , Vn]);
  4500. \Description
  4501. \var{Read} reads one or more values from a file \var{F}, and stores the
  4502. result in \var{V1}, \var{V2}, etc. After that it goes to the next line in
  4503. the file (defined by the \var{LineFeed (\#10)} character).
  4504. If no file \var{F} is specified, then standard input is read.
  4505. The variables \var{V1, V2} etc. must be of type \var{Char}, \var{Integer},
  4506. \var{Real}, \var{String} or \var{PChar}.
  4507. \Errors
  4508. If no data is available, a run-time error is generated. This behavior can
  4509. be controlled with the \var{\{\$i\}} compiler switch.
  4510. \SeeAlso
  4511. \seep{Read}, \seep{Blockread}, \seep{Write}, \seep{Blockwrite}
  4512. \end{procedure}
  4513. For an example, see \seep{Read}.
  4514. \begin{procedure}{Release}
  4515. \Declaration
  4516. Procedure Release (Var P : pointer);
  4517. \Description
  4518. \var{Release} sets the top of the Heap to the location pointed to by
  4519. \var{P}. All memory at a location higher than \var{P} is marked empty.
  4520. \Errors
  4521. A run-time error will be generated if \var{P} points to memory outside the
  4522. heap.
  4523. \SeeAlso
  4524. \seep{Mark}, \seef{Memavail}, \seef{Maxavail}, \seep{Getmem}, \seep{Freemem}
  4525. \seep{New}, \seep{Dispose}
  4526. \end{procedure}
  4527. For an example, see \seep{Mark}.
  4528. \begin{procedure}{Rename}
  4529. \Declaration
  4530. Procedure Rename (Var F : Any Filetype; Const S : String);
  4531. \Description
  4532. \var{Rename} changes the name of the assigned file \var{F} to \var{S}.
  4533. \var{F}
  4534. must be assigned, but not opened.
  4535. \Errors
  4536. A run-time error will be generated if \var{F} isn't assigned,
  4537. or doesn't exist.
  4538. \SeeAlso
  4539. \seep{Erase}
  4540. \end{procedure}
  4541. \latex{\inputlisting{refex/ex77.pp}}
  4542. \html{\input{refex/ex77.tex}}
  4543. \begin{procedure}{Reset}
  4544. \Declaration
  4545. Procedure Reset (Var F : Any File Type[; L : Longint]);
  4546. \Description
  4547. \var{Reset} opens a file \var{F} for reading. \var{F} can be any file type.
  4548. If \var{F} is an untyped or typed file, then it is opened for reading and
  4549. writing. If \var{F} is an untyped file, the record size can be specified in
  4550. the optional parameter \var{L}. Default a value of 128 is used.
  4551. \Errors
  4552. If the file cannot be opened for reading, then a run-time error is
  4553. generated. This behavior can be changed by the \var{\{\$i\} } compiler switch.
  4554. \SeeAlso
  4555. \seep{Rewrite}, \seep{Assign}, \seep{Close}
  4556. \end{procedure}
  4557. \latex{\inputlisting{refex/ex51.pp}}
  4558. \html{\input{refex/ex51.tex}}
  4559. \begin{procedure}{Rewrite}
  4560. \Declaration
  4561. Procedure Rewrite (Var F : Any File Type[; L : Longint]);
  4562. \Description
  4563. \var{Rewrite} opens a file \var{F} for writing. \var{F} can be any file type.
  4564. If \var{F} is an untyped or typed file, then it is opened for reading and
  4565. writing. If \var{F} is an untyped file, the record size can be specified in
  4566. the optional parameter \var{L}. Default a value of 128 is used.
  4567. if \var{Rewrite} finds a file with the same name as \var{F}, this file is
  4568. truncated to length \var{0}. If it doesn't find such a file, a new file is
  4569. created.
  4570. \Errors
  4571. If the file cannot be opened for writing, then a run-time error is
  4572. generated. This behavior can be changed by the \var{\{\$i\} } compiler switch.
  4573. \SeeAlso
  4574. \seep{Reset}, \seep{Assign}, \seep{Close}
  4575. \end{procedure}
  4576. \latex{\inputlisting{refex/ex52.pp}}
  4577. \html{\input{refex/ex52.tex}}
  4578. \begin{procedure}{Rmdir}
  4579. \Declaration
  4580. Procedure Rmdir (const S : string);
  4581. \Description
  4582. \var{Rmdir} removes the directory \var{S}.
  4583. \Errors
  4584. If \var{S} doesn't exist, or isn't empty, a run-time error is generated.
  4585. \SeeAlso
  4586. \seep{Chdir}, \seep{Rmdir}
  4587. \end{procedure}
  4588. \latex{\inputlisting{refex/ex53.pp}}
  4589. \html{\input{refex/ex53.tex}}
  4590. \begin{function}{Round}
  4591. \Declaration
  4592. Function Round (X : Real) : Longint;
  4593. \Description
  4594. \var{Round} rounds \var{X} to the closest integer, which may be bigger or
  4595. smaller than \var{X}.
  4596. \Errors
  4597. None.
  4598. \SeeAlso
  4599. \seef{Frac}, \seef{Int}, \seef{Trunc}
  4600. \end{function}
  4601. \latex{\inputlisting{refex/ex54.pp}}
  4602. \html{\input{refex/ex54.tex}}
  4603. \begin{procedure}{Runerror}
  4604. \Declaration
  4605. Procedure Runerror (ErrorCode : Word);
  4606. \Description
  4607. \var{Runerror} stops the execution of the program, and generates a
  4608. run-time error \var{ErrorCode}.
  4609. \Errors
  4610. None.
  4611. \SeeAlso
  4612. \seep{Exit}, \seep{Halt}
  4613. \end{procedure}
  4614. \latex{\inputlisting{refex/ex55.pp}}
  4615. \html{\input{refex/ex55.tex}}
  4616. \begin{procedure}{Seek}
  4617. \Declaration
  4618. Procedure Seek (Var F; Count : Longint);
  4619. \Description
  4620. \var{Seek} sets the file-pointer for file \var{F} to record Nr. \var{Count}.
  4621. The first record in a file has \var{Count=0}. F can be any file type, except
  4622. \var{Text}. If \var{F} is an untyped file, with no specified record size, 128
  4623. is assumed.
  4624. \Errors
  4625. A run-time error is generated if \var{Count} points to a position outside
  4626. the file, or the file isn't opened.
  4627. \SeeAlso
  4628. \seef{Eof}, \seef{SeekEof}, \seef{SeekEoln}
  4629. \end{procedure}
  4630. \latex{\inputlisting{refex/ex56.pp}}
  4631. \html{\input{refex/ex56.tex}}
  4632. \begin{function}{SeekEof}
  4633. \Declaration
  4634. Function SeekEof [(Var F : text)] : Boolean;
  4635. \Description
  4636. \var{SeekEof} returns \var{True} is the file-pointer is at the end of the
  4637. file. It ignores all whitespace.
  4638. Calling this function has the effect that the file-position is advanced
  4639. until the first non-whitespace character or the end-of-file marker is
  4640. reached.
  4641. If the end-of-file marker is reached, \var{True} is returned. Otherwise,
  4642. False is returned.
  4643. If the parameter \var{F} is omitted, standard \var{Input} is assumed.
  4644. \Errors
  4645. A run-time error is generated if the file \var{F} isn't opened.
  4646. \SeeAlso
  4647. \seef{Eof}, \seef{SeekEoln}, \seep{Seek}
  4648. \end{function}
  4649. \latex{\inputlisting{refex/ex57.pp}}
  4650. \html{\input{refex/ex57.tex}}
  4651. \begin{function}{SeekEoln}
  4652. \Declaration
  4653. Function SeekEoln [(Var F : text)] : Boolean;
  4654. \Description
  4655. \var{SeekEoln} returns \var{True} is the file-pointer is at the end of the
  4656. current line. It ignores all whitespace.
  4657. Calling this function has the effect that the file-position is advanced
  4658. until the first non-whitespace character or the end-of-line marker is
  4659. reached.
  4660. If the end-of-line marker is reached, \var{True} is returned. Otherwise,
  4661. False is returned.
  4662. The end-of-line marker is defined as \var{\#10}, the LineFeed character.
  4663. If the parameter \var{F} is omitted, standard \var{Input} is assumed.
  4664. \Errors
  4665. A run-time error is generated if the file \var{F} isn't opened.
  4666. \SeeAlso
  4667. \seef{Eof}, \seef{SeekEof}, \seep{Seek}
  4668. \end{function}
  4669. \latex{\inputlisting{refex/ex58.pp}}
  4670. \html{\input{refex/ex58.tex}}
  4671. \begin{function}{Seg}
  4672. \Declaration
  4673. Function Seg Var X : Longint;
  4674. \Description
  4675. \var{Seg} returns the segment of the address of a variable.
  4676. This function is only supported for compatibility. In \fpc, it
  4677. returns always 0, since \fpc is a 32 bit compiler, segments have no meaning.
  4678. \Errors
  4679. None.
  4680. \SeeAlso
  4681. \seef{DSeg}, \seef{CSeg}, \seef{Ofs}, \seef{Ptr}
  4682. \end{function}
  4683. \latex{\inputlisting{refex/ex60.pp}}
  4684. \html{\input{refex/ex60.tex}}
  4685. \begin{function}{SetJmp}
  4686. \Declaration
  4687. Function SetJmp (Var Env : Jmp\_Buf) : Longint;
  4688. \Description
  4689. \var{SetJmp} fills \var{env} with the necessary data for a jump back to the
  4690. point where it was called. It returns zero if called in this way.
  4691. If the function returns nonzero, then it means that a call to \seep{LongJmp}
  4692. with \var{env} as an argument was made somewhere in the program.
  4693. \Errors
  4694. None.
  4695. \SeeAlso
  4696. \seep{LongJmp}
  4697. \end{function}
  4698. \latex{\inputlisting{refex/ex79.pp}}
  4699. \html{\input{refex/ex79.tex}}
  4700. \begin{procedure}{SetTextBuf}
  4701. \Declaration
  4702. Procedure SetTextBuf (Var f : Text; Var Buf[; Size : Word]);
  4703. \Description
  4704. \var{SetTextBuf} assigns an I/O buffer to a text file. The new buffer is
  4705. located at \var{Buf} and is \var{Size} bytes long. If \var{Size} is omitted,
  4706. then \var{SizeOf(Buf)} is assumed.
  4707. The standard buffer of any text file is 128 bytes long. For heavy I/0
  4708. operations this may prove too slow. The \var{SetTextBuf} procedure allows
  4709. you to set a bigger buffer for your application, thus reducing the number of
  4710. system calls, and thus reducing the load on the system resources.
  4711. The maximum size of the newly assigned buffer is 65355 bytes.
  4712. {\em Remark 1:} Never assign a new buffer to an opened file. You can assign a
  4713. new buffer immediately after a call to \seep{Rewrite}, \seep{Reset} or
  4714. \var{Append}, but not after you read from/wrote to the file. This may cause
  4715. loss of data. If you still want to assign a new buffer after read/write
  4716. operations have been performed, flush the file first. This will ensure that
  4717. the current buffer is emptied.
  4718. {\em Remark 2:} Take care that the buffer you assign is always valid. If you
  4719. assign a local variable as a buffer, then after your program exits the local
  4720. program block, the buffer will no longer be valid, and stack problems may
  4721. occur.
  4722. \Errors
  4723. No checking on \var{Size} is done.
  4724. \SeeAlso
  4725. \seep{Assign}, \seep{Reset}, \seep{Rewrite}, \seep{Append}
  4726. \end{procedure}
  4727. \latex{\inputlisting{refex/ex61.pp}}
  4728. \html{\input{refex/ex61.tex}}
  4729. \begin{function}{Sin}
  4730. \Declaration
  4731. Function Sin (X : Real) : Real;
  4732. \Description
  4733. \var{Sin} returns the sine of its argument \var{X}, where \var{X} is an
  4734. angle in radians.
  4735. \Errors
  4736. None.
  4737. \SeeAlso
  4738. \seef{Cos}, \seef{Pi}, \seef{Exp}
  4739. \end{function}
  4740. \latex{\inputlisting{refex/ex62.pp}}
  4741. \html{\input{refex/ex62.tex}}
  4742. \begin{function}{SizeOf}
  4743. \Declaration
  4744. Function SizeOf (X : Any Type) : Longint;
  4745. \Description
  4746. \var{SizeOf} Returns the size, in bytes, of any variable or type-identifier.
  4747. {\em Remark:} this isn't Really a RTL function. Its result is calculated at
  4748. compile-time, and hard-coded in your executable.
  4749. \Errors
  4750. None.
  4751. \SeeAlso
  4752. \seef{Addr}
  4753. \end{function}
  4754. \latex{\inputlisting{refex/ex63.pp}}
  4755. \html{\input{refex/ex63.tex}}
  4756. \begin{function}{Sptr}
  4757. \Declaration
  4758. Function Sptr : Pointer;
  4759. \Description
  4760. \var{Sptr} returns the current stack pointer.
  4761. \Errors
  4762. None.
  4763. \SeeAlso
  4764. \end{function}
  4765. \latex{\inputlisting{refex/ex64.pp}}
  4766. \html{\input{refex/ex64.tex}}
  4767. \begin{function}{Sqr}
  4768. \Declaration
  4769. Function Sqr (X : Real) : Real;
  4770. \Description
  4771. \var{Sqr} returns the square of its argument \var{X}.
  4772. \Errors
  4773. None.
  4774. \SeeAlso
  4775. \seef{Sqrt}, \seef{Ln}, \seef{Exp}
  4776. \end{function}
  4777. \latex{\inputlisting{refex/ex65.pp}}
  4778. \html{\input{refex/ex65.tex}}
  4779. \begin{function}{Sqrt}
  4780. \Declaration
  4781. Function Sqrt (X : Real) : Real;
  4782. \Description
  4783. \var{Sqrt} returns the square root of its argument \var{X}, which must be
  4784. positive.
  4785. \Errors
  4786. If \var{X} is negative, then a run-time error is generated.
  4787. \SeeAlso
  4788. \seef{Sqr}, \seef{Ln}, \seef{Exp}
  4789. \end{function}
  4790. \latex{\inputlisting{refex/ex66.pp}}
  4791. \html{\input{refex/ex66.tex}}
  4792. \begin{function}{SSeg}
  4793. \Declaration
  4794. Function SSeg : Longint;
  4795. \Description
  4796. \var{SSeg} returns the Stack Segment. This function is only
  4797. supported for compatibolity reasons, as \var{Sptr} returns the
  4798. correct contents of the stackpointer.
  4799. \Errors
  4800. None.
  4801. \SeeAlso
  4802. \seef{Sptr}
  4803. \end{function}
  4804. \latex{\inputlisting{refex/ex67.pp}}
  4805. \html{\input{refex/ex67.tex}}
  4806. \begin{procedure}{Str}
  4807. \Declaration
  4808. Procedure Str (Var X[:NumPlaces[:Decimals]]; Var S : String);
  4809. \Description
  4810. \var{Str} returns a string which represents the value of X. X can be any
  4811. numerical type.
  4812. The optional \var{NumPLaces} and \var{Decimals} specifiers control the
  4813. formatting of the string.
  4814. \Errors
  4815. None.
  4816. \SeeAlso
  4817. \seep{Val}
  4818. \end{procedure}
  4819. \latex{\inputlisting{refex/ex68.pp}}
  4820. \html{\input{refex/ex68.tex}}
  4821. \begin{function}{Succ}
  4822. \Declaration
  4823. Function Succ (X : Any ordinal type) : Same type;
  4824. \Description
  4825. \var{Succ} returns the element that succeeds the element that was passed
  4826. to it. If it is applied to the last value of the ordinal type, and the
  4827. program was compiled with range checking on (\var{\{\$R+\}}, then a run-time
  4828. error will be generated.
  4829. \Errors
  4830. Run-time error 201 is generated when the result is out of
  4831. range.
  4832. \SeeAlso
  4833. \seef{Ord}, \seef{Pred}, \seef{High}, \seef{Low}
  4834. \end{function}
  4835. for an example, see \seef{Ord}.
  4836. \begin{function}{Swap}
  4837. \Declaration
  4838. Function Swap (X) : Type of X;
  4839. \Description
  4840. \var{Swap} swaps the high and low order bytes of \var{X} if \var{X} is of
  4841. type \var{Word} or \var{Integer}, or swaps the high and low order words of
  4842. \var{X} if \var{X} is of type \var{Longint} or \var{Cardinal}.
  4843. The return type is the type of \var{X}
  4844. \Errors
  4845. None.
  4846. \SeeAlso
  4847. \seef{Lo}, \seef{Hi}
  4848. \end{function}
  4849. \latex{\inputlisting{refex/ex69.pp}}
  4850. \html{\input{refex/ex69.tex}}
  4851. \begin{function}{Trunc}
  4852. \Declaration
  4853. Function Trunc (X : Real) : Longint;
  4854. \Description
  4855. \var{Trunc} returns the integer part of \var{X},
  4856. which is always smaller than (or equal to) \var{X}.
  4857. \Errors
  4858. None.
  4859. \SeeAlso
  4860. \seef{Frac}, \seef{Int}, \seef{Trunc}
  4861. \end{function}
  4862. \latex{\inputlisting{refex/ex70.pp}}
  4863. \html{\input{refex/ex70.tex}}
  4864. \begin{procedure}{Truncate}
  4865. \Declaration
  4866. Procedure Truncate (Var F : file);
  4867. \Description
  4868. \var{Truncate} truncates the (opened) file \var{F} at the current file
  4869. position.
  4870. \Errors
  4871. Errors are reported by IOresult.
  4872. \SeeAlso
  4873. \seep{Append}, \seef{Filepos},
  4874. \seep{Seek}
  4875. \end{procedure}
  4876. \latex{\inputlisting{refex/ex71.pp}}
  4877. \html{\input{refex/ex71.tex}}
  4878. \begin{function}{Upcase}
  4879. \Declaration
  4880. Function Upcase (C : Char or string) : Char or String;
  4881. \Description
  4882. \var{Upcase} returns the uppercase version of its argument \var{C}.
  4883. If its argument is a string, then the complete string is converted to
  4884. uppercase. The type of the returned value is the same as the type of the
  4885. argument.
  4886. \Errors
  4887. None.
  4888. \SeeAlso
  4889. \seef{Lowercase}
  4890. \end{function}
  4891. \latex{\inputlisting{refex/ex72.pp}}
  4892. \html{\input{refex/ex72.tex}}
  4893. \begin{procedure}{Val}
  4894. \Declaration
  4895. Procedure Val (const S : string;var V;var Code : word);
  4896. \Description
  4897. \var{Val} converts the value represented in the string \var{S} to a numerical
  4898. value, and stores this value in the variable \var{V}, which
  4899. can be of type \var{Longint}, \var{Real} and \var{Byte}.
  4900. If the conversion isn't succesfull, then the parameter \var{Code} contains
  4901. the index of the character in \var{S} which prevented the conversion.
  4902. The string \var{S} isn't allow to contain spaces.
  4903. \Errors
  4904. If the conversion doesn't succeed, the value of \var{Code} indicates the
  4905. position where the conversion went wrong.
  4906. \SeeAlso
  4907. \seep{Str}
  4908. \end{procedure}
  4909. \latex{\inputlisting{refex/ex74.pp}}
  4910. \html{\input{refex/ex74.tex}}
  4911. \begin{procedure}{Write}
  4912. \Declaration
  4913. Procedure Write ([Var F : Any filetype;] V1 [; V2; ... , Vn)];
  4914. \Description
  4915. \var{Write} writes the contents of the variables \var{V1}, \var{V2} etc. to
  4916. the file \var{F}. \var{F} can be a typed file, or a \var{Text} file.
  4917. If \var{F} is a typed file, then the variables \var{V1}, \var{V2} etc. must
  4918. be of the same type as the type in the declaration of \var{F}. Untyped files
  4919. are not allowed.
  4920. If the parameter \var{F} is omitted, standard output is assumed.
  4921. If \var{F} is of type \var{Text}, then the necessary conversions are done
  4922. such that the output of the variables is in human-readable format.
  4923. This conversion is done for all numerical types. Strings are printed exactly
  4924. as they are in memory, as well as \var{PChar} types.
  4925. The format of the numerical conversions can be influenced through
  4926. the following modifiers:
  4927. \var{ OutputVariable : NumChars [: Decimals ] }
  4928. This will print the value of \var{OutputVariable} with a minimum of
  4929. \var{NumChars} characters, from which \var{Decimals} are reserved for the
  4930. decimals. If the number cannot be represented with \var{NumChars} characters,
  4931. \var{NumChars} will be increased, until the representation fits. If the
  4932. representation requires less than \var{NumChars} characters then the output
  4933. is filled up with spaces, to the left of the generated string, thus
  4934. resulting in a right-aligned representation.
  4935. If no formatting is specified, then the number is written using its natural
  4936. length, with a space in front of it if it's positive, and a minus sign if
  4937. it's negative.
  4938. Real numbers are, by default, written in scientific notation.
  4939. \Errors
  4940. If an error occurs, a run-time error is generated. This behavior can be
  4941. controlled with the \var{\{\$i\}} switch.
  4942. \SeeAlso
  4943. \seep{WriteLn}, \seep{Read}, \seep{Readln}, \seep{Blockwrite}
  4944. \end{procedure}
  4945. \begin{procedure}{WriteLn}
  4946. \Declaration
  4947. Procedure WriteLn [([Var F : Text;] [V1 [; V2; ... , Vn)]];
  4948. \Description
  4949. \var{WriteLn} does the same as \seep{Write} for text files, and emits a
  4950. Carriage Return - LineFeed character pair after that.
  4951. If the parameter \var{F} is omitted, standard output is assumed.
  4952. If no variables are specified, a Carriage Return - LineFeed character pair
  4953. is emitted, resulting in a new line in the file \var{F}.
  4954. {\em Remark:} Under \linux, the Carriage Return character is omitted, as
  4955. customary in Unix environments.
  4956. \Errors
  4957. If an error occurs, a run-time error is generated. This behavior can be
  4958. controlled with the \var{\{\$i\}} switch.
  4959. \SeeAlso
  4960. \seep{Write}, \seep{Read}, \seep{Readln}, \seep{Blockwrite}
  4961. \end{procedure}
  4962. \latex{\inputlisting{refex/ex75.pp}}
  4963. \html{\input{refex/ex75.tex}}
  4964. %
  4965. % The index.
  4966. %
  4967. \printindex
  4968. \end{document}