Routine: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Routine
| Old | New | Line | |
|---|---|---|---|
| 1 | + | A routine is a re-usable piece of [[Source code|source code]] having a name (called an [[Identifier|identifier]]) that performs some functionality. Pascal distinguishes between two kinds of routines: [[Procedure|procedures]] and [[Function|functions]]. Functions are capable of returning a result value, while procedures do not. In consequence functions can appear in expressions, but procedures cannot. | |
| 2 | + | ||
| 3 | + | A routine that is part of an [[Object|object]] or [[Class|class]] is called a [[Method|method]]. [[Property|Properties]] of objects or classes can redirect read and/or write access to methods in that property or class handling the read or write request, if their signatures have a certain structure. | |
| 4 | + | ||
| 5 | + | ## Routine declarations and definitions | |
| 6 | + | ||
| 7 | + | ### Functions | |
| 8 | + | ||
| 9 | + | A function is a routine that may return a value, and its signature indicates this. The signature of a function is as follows: | |
| 10 | + | ||
| 11 | + | ``` pascal | |
| 12 | + | function *name*: *result*; | |
| 13 | + | ``` | |
| 14 | + | ||
| 15 | + | or | |
| 16 | + | ||
| 17 | + | ``` pascal | |
| 18 | + | function *name* (*parameter list*): *result*; | |
| 19 | + | ``` | |
| 20 | + | ||
| 21 | + | The meaning of these terms will be explained below under Signature. | |
| 22 | + | ||
| 23 | + | ### Procedures | |
| 24 | + | ||
| 25 | + | A procedure is a routine that cannot return a value. The signature of a procedure is as follows: | |
| 26 | + | ||
| 27 | + | ``` pascal | |
| 28 | + | procedure *name*; | |
| 29 | + | ``` | |
| 30 | + | ||
| 31 | + | or | |
| 32 | + | ||
| 33 | + | ``` pascal | |
| 34 | + | procedure *name* (*parameter list*); | |
| 35 | + | ``` | |
| 36 | + | ||
| 37 | + | ### Signature | |
| 38 | + | ||
| 39 | + | The items in the signature are: | |
| 40 | + | ||
| 41 | + | ||
| 42 | + | *name* is an identifier than gives the name of the routine. | |
| 43 | + | ||
| 44 | + | *parameter list* (if one is included) is one or more parameters, separated by semicolons, describing each argument to be passed to the routine. | |
| 45 | + | ||
| 46 | + | *result type* (for functions only) defines what type of result the function may return. | |
| 47 | + | ||
| 48 | + | ### Parameters | |
| 49 | + | ||
| 50 | + | Routines may be parameterized, that means they can have arguments supplied to them when called. When introducing a new routine identifier a parameter list (one or more arguments) can be appended. For instance the following procedure signature tells the compiler, that `doSomething` requires an integer as first parameter. | |
| 51 | + | ||
| 52 | + | ``` pascal | |
| 53 | + | procedure doSomething(const someParameter: integer); | |
| 54 | + | ``` | |
| 55 | + | ||
| 56 | + | The following procedure signature tells the compiler, That `doSomethingelse` requires a real as first parameter and an integer as second parameter. | |
| 57 | + | ||
| 58 | + | ``` pascal | |
| 59 | + | procedure doSomethingelse(const someParameter: Real; SomeOtherParameter: Integer); | |
| 60 | + | ``` | |
| 61 | + | ||
| 62 | + | #### Default values | |
| 63 | + | ||
| 64 | + | Parameters can become optional when they are supplied with a [[Default parameter|default value]] like so: | |
| 65 | + | ||
| 66 | + | ``` pascal | |
| 67 | + | procedure doSomething(const someParameter: integer = 42); | |
| 68 | + | ``` | |
| 69 | + | ||
| 70 | + | Defining default values is only possible in [[Mode ObjFPC|{$mode objFPC}]] or [[Mode Delphi|{$mode Delphi}]], and they are only for simple types. | |
| 71 | + | ||
| 72 | + | Optional parameters if any, have to appear at the end of the formal parameter list. Consequently, mandatory parameters cannot appear after any optional parameter. | |
| 73 | + | ||
| 74 | + | #### Parameter hints | |
| 75 | + | ||
| 76 | + | While defining the formal parameters in front of each identifier(s), type tuple, the compiler can be supplied with additional hints. The compiler then can make further optimizations. | |
| 77 | + | ||
| 78 | + | - [[Const|const]] informs the compiler, that the named parameter(s) won't be changed in the routine definition. | |
| 79 | + | - [[Constref|constref]] imposes further restrictions. | |
| 80 | + | ||
| 81 | + | #### Parameter types | |
| 82 | + | ||
| 83 | + | By default each routine receives an own copy of each parameter (value parameter). | |
| 84 | + | ||
| 85 | + | - If the routine is supposed to work on the original, that means on the [[Variable|variable]] as it exists in the place the routine is called, the modifier [[Var|var]] will allow that. Thereby the named parameter becomes a [[Variable parameter|variable parameter]]. | |
| 86 | + | - Furthermore, if `{$modeswitch out+}` (automatically set by various modes), the output parameter type `out` exists. The routine will not, or is not supposed to read from such parameters, but only write. | |
| 87 | + | ||
| 88 | + | ### Routine overloading | |
| 89 | + | ||
| 90 | + | Routines can be overloaded. That means, one and the same identifier can be associated with varying definitions provided the formal signatures differ. | |
| 91 | + | ||
| 92 | + | > **Note:** Routine overloading is not possible for routines having the [[Cdecl|cDecl]] calling convention [[modifier]]. The FPC implements routine overloading by name mangling \[= encoding parameter data types in a modified version of the routine’s identifier\]. However, `cDecl` will *disable* such. | |
| 93 | + | ||
| 94 | + | ### Definition | |
| 95 | + | ||
| 96 | + | In order to define a routine, its signature is followed by a [[Block|block]]. | |
| 97 | + | ||
| 98 | + | The parameters are available by their identifiers inside the statement-frame and nested routines. In functions additional identifiers are available, in order to set the function's return value. | |
| 99 | + | ||
| 100 | + | ## Invoking routines | |
| 101 | + | ||
| 102 | + | Routines are called by stating their identifiers, followed by the list of (mandatory) parameter literals or variables their types are compatible. Depending on the routine's type, i.e. either [[Procedure|procedure]] or [[Function|function]], routine calls are only allowed as statements or in expressions, or even both. | |
| 103 | + | ||
| 104 | + | ## Comparative remarks | |
| 105 | + | ||
| 106 | + | The definition of a routine always concludes with a `ret`urn-instruction. Thus the control flow is always handed back to the LOC the routine was invoked at. In contrast to that behavior, [[Goto|goto jumps]] can be abused to circumvent that “limitation”. | |
| 107 | + | ||
| 108 | + | Every routine call is preceded by proper allocation of parameter values. | |
| 109 | + | ||
| 110 | + | --- | |
| 111 | + | *From the Free Pascal wiki, page "Routine", as it stood on 2022-09-23 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
