Difference between revisions of "Routine"

From Free Pascal wiki
Jump to navigationJump to search
(qualification)
(structuralization; →‎comparative remarks)
Line 10: Line 10:
 
[[Property|Properties]] of objects or classes can redirect read and/or write access to such methods, if their signatures have a certain structure.
 
[[Property|Properties]] of objects or classes can redirect read and/or write access to such methods, if their signatures have a certain structure.
  
== parameters ==
+
== routine declarations and definitions ==
 +
=== parameters ===
 
Routines can be parameterized.
 
Routines can be parameterized.
 
When introducing a new routine identifier a parameter list can be appended.
 
When introducing a new routine identifier a parameter list can be appended.
Line 18: Line 19:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== default values ==
+
==== default values ====
 
Parameters can become optional when they are supplied with a [[Default parameter|default value]] like so:
 
Parameters can become optional when they are supplied with a [[Default parameter|default value]] like so:
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
Line 26: Line 27:
 
That means, mandatory parameters can not appear after any optional parameter.
 
That means, mandatory parameters can not appear after any optional parameter.
  
== parameter hints ==
+
==== parameter hints ====
 
While defining the formal parameters in front of each identifier(s), type tuple, the compiler can be supplied with additional hints.
 
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.
 
The compiler then can make further optimizations.
Line 32: Line 33:
 
* [[Constref|<syntaxhighlight lang="pascal" enclose="none">constref</syntaxhighlight>]] imposes further restrictions.
 
* [[Constref|<syntaxhighlight lang="pascal" enclose="none">constref</syntaxhighlight>]] imposes further restrictions.
  
== parameter types ==
+
==== parameter types ====
 
By default each routine receives an own copy of each parameter (value parameter).
 
By default each routine receives an own copy of each parameter (value parameter).
 
* 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#Pass by Reference|<syntaxhighlight lang="pascal" enclose="none">var</syntaxhighlight>]] will allow that. Thereby the named parameter becomes a [[Variable parameter|variable parameter]].
 
* 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#Pass by Reference|<syntaxhighlight lang="pascal" enclose="none">var</syntaxhighlight>]] will allow that. Thereby the named parameter becomes a [[Variable parameter|variable parameter]].
 
* Furthermore, if <syntaxhighlight lang="pascal" enclose="none">{$modeswitch out on}</syntaxhighlight> (automatically set by various modes), the output parameter type <syntaxhighlight lang="pascal" enclose="none">out</syntaxhighlight> exists. The routine will not, or is not supposed to read from such parameters, but only write.
 
* Furthermore, if <syntaxhighlight lang="pascal" enclose="none">{$modeswitch out on}</syntaxhighlight> (automatically set by various modes), the output parameter type <syntaxhighlight lang="pascal" enclose="none">out</syntaxhighlight> exists. The routine will not, or is not supposed to read from such parameters, but only write.
 +
 +
=== routine overloading ===
 +
Routines can be overloaded.
 +
That means, one and the same identifier can be associated with varying definitions provided the formal signatures differ.
  
 
== invoking routines ==
 
== invoking routines ==
Line 41: Line 46:
 
Depending on the routine's type, i.e. either [[Procedure|<syntaxhighlight lang="pascal" enclose="none">procedure</syntaxhighlight>]] or [[Function|<syntaxhighlight lang="pascal" enclose="none">function</syntaxhighlight>]], routine calls are only allowed as statements or in expressions, or even both.
 
Depending on the routine's type, i.e. either [[Procedure|<syntaxhighlight lang="pascal" enclose="none">procedure</syntaxhighlight>]] or [[Function|<syntaxhighlight lang="pascal" enclose="none">function</syntaxhighlight>]], routine calls are only allowed as statements or in expressions, or even both.
  
== routine overloading ==
+
== comparative remarks ==
Routines can be overloaded.
+
The definition of a routine always concludes with a <syntaxhighlight lang="asm" enclose="none">ret</syntaxhighlight>urn-instruction.
That means, one and the same identifier can be associated with varying definitions provided the formal signatures differ.
+
Thus the control flow is always handed back to the LOC the routine was invoked at.
 +
In contrast to that behavior, [[Goto|<syntaxhighlight lang="pascal" enclose="none">goto</syntaxhighlight> jumps]] can be abused to circumvent that “limitation”.
 +
 
 +
Every routine call is preceded by proper allocation of parameter values.

Revision as of 13:45, 23 May 2018

English (en) suomi (fi) français (fr) русский (ru)

A routine is a re-usable piece of source code that performs some functionality. Pascal distinguishes between two kinds of routines: procedures and functions. The latter is capable of returning a result, whilst the former does not return any value. In consequence functions can appear in expressions, but procedures can not.

A routine that is part of an object or class is called method. Properties of objects or classes can redirect read and/or write access to such methods, if their signatures have a certain structure.

routine declarations and definitions

parameters

Routines can be parameterized. When introducing a new routine identifier a parameter list can be appended. For instance the following procedure signature tells the compiler, that doSomething accepts an integer as first parameter.

procedure doSomething(const someParameter: integer);

default values

Parameters can become optional when they are supplied with a default value like so:

procedure doSomething(const someParameter: integer = 42);

Optional parameters if any, have to appear at the end of the formal parameter list. That means, mandatory parameters can not appear after any optional parameter.

parameter hints

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.

  • const informs the compiler, that the named parameter(s) won't be changed in the routine definition.
  • constref imposes further restrictions.

parameter types

By default each routine receives an own copy of each parameter (value parameter).

  • If the routine is supposed to work on the original, that means on the variable as it exists in the place the routine is called, the modifier var will allow that. Thereby the named parameter becomes a variable parameter.
  • Furthermore, if {$modeswitch out on} (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.

routine overloading

Routines can be overloaded. That means, one and the same identifier can be associated with varying definitions provided the formal signatures differ.

invoking routines

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 or function, routine calls are only allowed as statements or in expressions, or even both.

comparative remarks

The definition of a routine always concludes with a return-instruction. Thus the control flow is always handed back to the LOC the routine was invoked at. In contrast to that behavior, goto jumps can be abused to circumvent that “limitation”.

Every routine call is preceded by proper allocation of parameter values.