ref.tex 182 KB

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