ref.tex 182 KB

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