Difference between revisions of "Basic Pascal Tutorial/Chapter 4/Functions"

From Lazarus wiki
Jump to navigationJump to search
(New page: 4C - Functions Functions work the same way as procedures, but they always ''return a single value'' to the main program through its ''own name'': <font color="#006699"><strong>function</...)
 
m (Fixed syntax highlighting)
(9 intermediate revisions by 6 users not shown)
Line 1: Line 1:
4C - Functions
+
{{Functions}}
 +
{{TYNavigator|Parameters|Scope}}
 +
 
 +
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'':
 
Functions work the same way as procedures, but they always ''return a single value'' to the main program through its ''own name'':
<font color="#006699"><strong>function</strong></font> <font color="#009966"><strong>Name</strong></font> (parameter_list) : return_type;
+
<syntaxhighlight lang=pascal>
 +
function Name (parameter_list) : return_type;  
 +
</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:
a := Name (5) + 3;
+
<syntaxhighlight lang=pascal>
 
+
a := Name (5) + 3;
Be careful not to use the name of the function on the right side of any equation inside the function. That is:
+
</syntaxhighlight>
<font color="#006699"><strong>function</strong></font> <font color="#009966"><strong>Name</strong></font> <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>integer</strong></font><font color="#000000"><strong>;</strong></font>
 
<font color="#006699"><strong>begin</strong></font>
 
  <font color="#009966"><strong>Name</strong></font> <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">2</font><font color="#000000"><strong>;</strong></font>
 
  <font color="#009966"><strong>Name</strong></font> <font color="#000000"><strong>:=</strong></font> <font color="#009966"><strong>Name</strong></font> <font color="#000000"><strong>+</strong></font> <font color="#ff0000">1</font>
 
<font color="#006699"><strong>end</strong></font><font color="#000000"><strong>.</strong></font>
 
  
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.
+
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:
 +
<syntaxhighlight lang=pascal>
 +
function Name : integer;
 +
begin
 +
  Name := 2;
 +
  Name := Name + 1
 +
end.
 +
</syntaxhighlight>
 +
is a no-no. Instead of returning the value 3, as might be expected, this sets up an infinite recursive loop in certain language modes (e.g. {$MODE DELPHI} or {$MODE TP}; other modes require brackets for function call even if the brackets are empty due to no parameters being required for the particular function). 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.
Name := 5;
+
<syntaxhighlight lang=pascal>
 +
Name := 5;
 +
</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.
  
{|style=color-backgroud="white" cellspacing="20"
+
{{TYNavigator|Parameters|Scope}}
|[[Parameters|previous]] 
 
|[[op_contents|contents]]
 
|[[Scope|next]]
 
|}
 

Revision as of 06:21, 16 February 2020

български (bg) English (en) français (fr) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

 ◄   ▲   ► 

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 in certain language modes (e.g. {$MODE DELPHI} or {$MODE TP}; other modes require brackets for function call even if the brackets are empty due to no parameters being required for the particular function). 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.

 ◄   ▲   ►