ref.tex 193 KB

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