Difference between revisions of "Routine"

From Free Pascal wiki
Jump to navigationJump to search
(modeswitch)
(correct some minor grammatical errors to clarify)
Line 1: Line 1:
 
{{Routine}}
 
{{Routine}}
  
A routine is a re-usable piece of [[Source code|source code]] that performs some functionality.
+
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:
 
Pascal distinguishes between two kinds of routines:
 
[[Procedure|procedures]] and [[Function|functions]].
 
[[Procedure|procedures]] and [[Function|functions]].
The latter is capable of returning a result, whilst the former does not return any value.
+
Functions are capable of returning a result value, while procedures do not.
 
In consequence functions can appear in expressions, but procedures can not.
 
In consequence functions can appear in expressions, but procedures can not.
  
A routine that is part of an object or class is called [[Method|method]].
+
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.
[[Property|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 ==
 +
=== Functions ===
 +
A function is a routine that may return a value, and its signature indicates this. The signature of a function is as follows:
 +
<syntaxhighlight lang="pascal">function *name*: *result*;</syntaxhighlight> or
 +
<syntaxhighlight lang="pascal">function *name* (*parameter list*): *result*;</syntaxhighlight>
 +
The meaning of these terms will be explained below under "signature".
 +
=== Procedures ===
 +
A procedure is a routine that cannot return a value. The signature of a procedure is as follows:
 +
<syntaxhighlight lang="pascal">
 +
procedure *name*;
 +
</syntaxhighlight> or
 +
<syntaxhighlight lang="pascal">
 +
procedure *name* (*parameter list*);
 +
</syntaxhighlight>
 +
=== Signature ===
 +
The items in the signature are:
 +
: *name* is an identifier than gives the name of the routine.
 +
: *parameter list* (if one is included) is one or more parameters, separated by semicolons, describing each argument to be passed to the routine.
 +
: *result type* (for functions only) defines what type of result the function may return.
  
== routine declarations and definitions ==
 
 
=== parameters ===
 
=== parameters ===
Routines can be parameterized.
+
Routines may be parameterized, that means they can have arguments supplied to them when called.
When introducing a new routine identifier a parameter list can be appended.
+
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 <syntaxhighlight lang="pascal" enclose="none">doSomething</syntaxhighlight> accepts an integer as first parameter.
+
For instance the following procedure signature tells the compiler, that <syntaxhighlight lang="pascal" enclose="none">doSomething</syntaxhighlight> requires an integer as first parameter.
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
 
procedure doSomething(const someParameter: integer);
 
procedure doSomething(const someParameter: integer);
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
the following procedure signature tells the compiler, That <syntaxhighlight lang="pascal" enclose="none">doSomethingelse</syntaxhighlight> requires a real as first parameter and an integer as second parameter.
 +
<syntaxhighlight lang="pascal">
 +
procedure doSomethingelse(const someParameter: Real; SomeOtherParameter: Integer);
 +
</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>
 
procedure doSomething(const someParameter: integer = 42);
 
procedure doSomething(const someParameter: integer = 42);
 
</syntaxhighlight>
 
</syntaxhighlight>
Defining default values is only possible in [[Mode ObjFPC|<syntaxhighlight lang="pascal" enclose="none">{$mode objFPC}</syntaxhighlight>]] or [[Mode Delphi|<syntaxhighlight lang="pascal" enclose="none">{$mode Delphi}</syntaxhighlight>]], and there only for simple types.
+
Defining default values is only possible in [[Mode ObjFPC|<syntaxhighlight lang="pascal" enclose="none">{$mode objFPC}</syntaxhighlight>]] or [[Mode Delphi|<syntaxhighlight lang="pascal" enclose="none">{$mode Delphi}</syntaxhighlight>]], and they are only for simple types.
  
 
Optional parameters if any, have to appear at the end of the formal parameter list.
 
Optional parameters if any, have to appear at the end of the formal parameter list.

Revision as of 10:10, 14 October 2020

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

A routine is a re-usable piece of source code having a name (called an identifier) that performs some functionality. Pascal distinguishes between two kinds of routines: procedures and functions. Functions are capable of returning a result value, while procedures do not. In consequence functions can appear in expressions, but procedures can not.

A routine that is part of an object or class is called a method. 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.

Routine declarations and definitions

Functions

A function is a routine that may return a value, and its signature indicates this. The signature of a function is as follows:

function *name*: *result*;

or

function *name* (*parameter list*): *result*;

The meaning of these terms will be explained below under "signature".

Procedures

A procedure is a routine that cannot return a value. The signature of a procedure is as follows:

procedure *name*;

or

procedure *name* (*parameter list*);

Signature

The items in the signature are:

*name* is an identifier than gives the name of the routine.
*parameter list* (if one is included) is one or more parameters, separated by semicolons, describing each argument to be passed to the routine.
*result type* (for functions only) defines what type of result the function may return.

parameters

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.

procedure doSomething(const someParameter: integer);

the following procedure signature tells the compiler, That doSomethingelse requires a real as first parameter and an integer as second parameter.

procedure doSomethingelse(const someParameter: Real; SomeOtherParameter: Integer);


default values

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

procedure doSomething(const someParameter: integer = 42);

Defining default values is only possible in {$mode objFPC} or {$mode Delphi}, and they are only for simple types.

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+} (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.

definition

In order to define a routine, its signature is followed by a block.

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.

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.