Difference between revisions of "Functions"

From Lazarus wiki
m (Text replace - "delphi>" to "syntaxhighlight>")
Line 2: Line 2:
  
 
Functions work the same way as procedures, but they always ''return a single value'' to the main program through its ''own name'':
 
Functions work the same way as procedures, but they always ''return a single value'' to the main program through its ''own name'':
<delphi>
+
<syntaxhighlight>
 
function Name (parameter_list) : return_type;  
 
function Name (parameter_list) : return_type;  
</delphi>
+
</syntaxhighlight>
  
 
Functions are called in the main program by using them in expressions:
 
Functions are called in the main program by using them in expressions:
<delphi>
+
<syntaxhighlight>
 
a := Name (5) + 3;
 
a := Name (5) + 3;
</delphi>
+
</syntaxhighlight>
  
 
If your function has no argument, be careful not to use the name of the function on the right side of any equation inside the function. That is:
 
If your function has no argument, be careful not to use the name of the function on the right side of any equation inside the function. That is:
<delphi>
+
<syntaxhighlight>
 
function Name : integer;
 
function Name : integer;
 
begin
 
begin
Line 18: Line 18:
 
   Name := Name + 1
 
   Name := Name + 1
 
end.
 
end.
</delphi>
+
</syntaxhighlight>
 
is a no-no. Instead of returning the value 3, as might be expected, this sets up an infinite recursive loop. Name will call Name, which will call Name, which will call Name, etc.
 
is a no-no. Instead of returning the value 3, as might be expected, this sets up an infinite recursive loop. Name will call Name, which will call Name, which will call Name, etc.
  
 
The return value is set by assigning a value to the function identifier.
 
The return value is set by assigning a value to the function identifier.
<delphi>
+
<syntaxhighlight>
 
Name := 5;
 
Name := 5;
</delphi>
+
</syntaxhighlight>
  
 
It is generally bad programming form to make use of VAR parameters in functions -- functions should return only one value. You certainly don't want the sin function to change your pi radians to 0 radians because they're equivalent -- you just want the answer 0.
 
It is generally bad programming form to make use of VAR parameters in functions -- functions should return only one value. You certainly don't want the sin function to change your pi radians to 0 radians because they're equivalent -- you just want the answer 0.

Revision as of 15:22, 24 March 2012

4C - Functions (author: Tao Yue, state: unchanged)

Functions work the same way as procedures, but they always return a single value to the main program through its own name:

function Name (parameter_list) : return_type;

Functions are called in the main program by using them in expressions:

a := Name (5) + 3;

If your function has no argument, be careful not to use the name of the function on the right side of any equation inside the function. That is:

function Name : integer;
begin
  Name := 2;
  Name := Name + 1
end.

is a no-no. Instead of returning the value 3, as might be expected, this sets up an infinite recursive loop. Name will call Name, which will call Name, which will call Name, etc.

The return value is set by assigning a value to the function identifier.

Name := 5;

It is generally bad programming form to make use of VAR parameters in functions -- functions should return only one value. You certainly don't want the sin function to change your pi radians to 0 radians because they're equivalent -- you just want the answer 0.

previous contents next