Saturday, May 19, 2018

Method definition

I try to explain the method syntax of Oxygene Pascal in this post.

Methods are the named code parts where a small part of the functional problem that the application handles is solved.

In other Pascal dialects, the keyword Function is used for methods that return a value, while the keyword Procedure is used for methods that do not return a value. While Oxygene Pascal supports this syntax, the it is preferred  to use the Method keyword for both.

Methods are declared within the class definition (see  The structure of a Pascal file).
The visibility (see Visibility modifiers) of the method is determent by the visibility section where it is declared.


Method declaration
The most simple form of a Method declaration:
The name of this method is MethodName, it has no return value (so, a procedure) and no parameters.

To add a return value, simply add ": type" to the declaration:
This function will return a string when it is called.


Tuples
Oxygene Pascal has also support for Tuples, a way to return more than one return value:
In this case the function returns three distinct values.


Instance and static methods
In the above examples, we were writing instance methods. Instance methods are only available in an instance of the class. To create a static method, the method must be prefixed with the "class" keyword:

A static method can be called without an instance being created - just simply call the method in the form of classname.methodname.


Parameters
Next we can add parameters to the method.
There are four different kinds of parameters available; by value, by reference, output and constant parameters:
The parameters work as following:
by value
(no prefix)
For value type variables:
The variable is copied to the method but never copied back.
When the variable is changed within the implementation of the method, the original variable will not be changed.

For reference type variables:
The pointer to the variable is copied to the method but never copied back
If the variable is re-assigned in the method implementation, the original variable is not changed, but when properties or fields of the reference type are changed, these will change in the original too, because the copied pointer points to the same memory.
by reference
(var prefix)
For value type variables:
The variable is copied before the method is called and copied back to the original variable when the method exits;when the variable is changed within the implementation of the method, the original variable will be changed too.

For reference type variables:
The pointer to the variable is copied to the method and copied back to the original variable when the method exits - if the variable is re-assigned in the method implementation or when properties or fields are changed, the original variable is changed too.
output
(out prefix)
The variable used to call the method is not copied to the method, but the variable has a value of Default<type> when the method starts. This means that the value is nil for reference types or the default of the used value type at the start of the method implementation. When the method exits, the new content of the out variable is copied back to the calling variable.
constant
(const prefix)
For value type variables:
The variable is copied before the method is called but never copied back. The variable is immutable within the method implementation.

For reference type variables: 
 The pointer to the variable is copied to the method but never copied back. The  pointer to the variable itself is immutable and cannot be re-assigned within the method implementation. But when properties or fields are changed, the original these changes are reflected in the calling variable too.

A method can have more parameters, they are separated with a semicolon:

Default value for a parameter
Further, a parameter can have a default value. This default value is used when the parameter is omitted when the function is called:
When this function is called without the s parameter  MethodName(i); s will have the value of  "the default value" at the start of the method implementation.


Parameter arrays
With a parameter array, the method gets an unending amount of parameters with all the same type.
It is defined as:
the parameter type is always an array of <type>, in this case an object, so any type can be used in the call. When, by example, using an array of string as type for the parameter, only strings can be passed.

Multi part methods
A multi part method has its parameters between the name of the method, to create a very readable syntax. Take the following standard method:
This is not very readable. But when we use a multipart method, everything becomes clear:

And I think that these multipart methods are a beautiful end to this post. 




No comments:

Post a Comment