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

From Lazarus wiki
Jump to navigationJump to search
m (Text replace - "delphi>" to "syntaxhighlight>")
m (bypass language bar/categorization template redirect [cf. discussion])
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Parameters}}
+
{{Basic Pascal Tutorial/Chapter 4/Parameters}}
 +
{{TYNavigator|Chapter 4/Procedures|Chapter 4/Functions}}
  
 
4B - Parameters (author: Tao Yue, state: unchanged)
 
4B - Parameters (author: Tao Yue, state: unchanged)
  
 
A parameter list can be included as part of the procedure heading. The parameter list allows variable values to be transferred from the main program to the procedure. The new procedure heading is:
 
A parameter list can be included as part of the procedure heading. The parameter list allows variable values to be transferred from the main program to the procedure. The new procedure heading is:
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
procedure Name (formal_parameter_list);
 
procedure Name (formal_parameter_list);
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 15: Line 16:
  
 
The procedure is called by passing arguments (called the actual parameter list) of the same number and type as the formal parameter list.
 
The procedure is called by passing arguments (called the actual parameter list) of the same number and type as the formal parameter list.
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
procedure Name (a, b : integer; c, d : real);
 
procedure Name (a, b : integer; c, d : real);
 
begin
 
begin
Line 25: Line 26:
  
 
Suppose you called the above procedure from the main program as follows:
 
Suppose you called the above procedure from the main program as follows:
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
alpha := 30;
 
alpha := 30;
 
Name (alpha, 3, 4, 5);
 
Name (alpha, 3, 4, 5);
Line 35: Line 36:
  
 
Another way of passing parameters is '''call-by-reference'''. This creates a '''link''' between the formal parameter and the actual parameter. When the formal parameter is modified in the procedure, the actual parameter is likewise modified. ''Call-by-reference'' is activated by preceding the parameter group with a <tt>VAR</tt>:
 
Another way of passing parameters is '''call-by-reference'''. This creates a '''link''' between the formal parameter and the actual parameter. When the formal parameter is modified in the procedure, the actual parameter is likewise modified. ''Call-by-reference'' is activated by preceding the parameter group with a <tt>VAR</tt>:
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
VAR identifier1, identifier2, ..., identifiern : datatype;
 
VAR identifier1, identifier2, ..., identifiern : datatype;
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 42: Line 43:
  
 
Here's an example which mixes ''call-by-value'' and ''call-by-reference'':
 
Here's an example which mixes ''call-by-value'' and ''call-by-reference'':
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
procedure Name (a, b : integer; VAR c, d : integer);
 
procedure Name (a, b : integer; VAR c, d : integer);
 
begin
 
begin
Line 65: Line 66:
 
In other words, call-by-value is one-way data transfer: main program to procedure. Call-by-reference goes both ways.
 
In other words, call-by-value is one-way data transfer: main program to procedure. Call-by-reference goes both ways.
  
{|style=color-backgroud="white" cellspacing="20"
+
{{TYNavigator|Chapter 4/Procedures|Chapter 4/Functions}}
|[[Procedures|previous]] 
 
|[[Contents|contents]]
 
|[[Functions|next]]
 
|}
 

Latest revision as of 15:19, 20 August 2022

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

 ◄   ▲   ► 

4B - Parameters (author: Tao Yue, state: unchanged)

A parameter list can be included as part of the procedure heading. The parameter list allows variable values to be transferred from the main program to the procedure. The new procedure heading is:

procedure Name (formal_parameter_list);

The parameter list consists of several parameter groups, separated by semicolons:

param_group_1; param_group2; ... ; param_groupn

Each parameter group has the form:

identifier_1, identifier_2, ... , identifier_n : data_type

The procedure is called by passing arguments (called the actual parameter list) of the same number and type as the formal parameter list.

procedure Name (a, b : integer; c, d : real);
begin
  a := 10;
  b := 2;
  writeln (a, b, c, d)
end;

Suppose you called the above procedure from the main program as follows:

alpha := 30;
Name (alpha, 3, 4, 5);

When you return to the main program, what is the value of alpha? 30. Yet, alpha was passed to a, which was assigned a value of 10. What actually happened was that a and alpha are totally distinct. The value in the main program was not affected by what happened in the procedure.

This is called call-by-value. This passes the value of a variable to a procedure.

Another way of passing parameters is call-by-reference. This creates a link between the formal parameter and the actual parameter. When the formal parameter is modified in the procedure, the actual parameter is likewise modified. Call-by-reference is activated by preceding the parameter group with a VAR:

VAR identifier1, identifier2, ..., identifiern : datatype;

In this case, constants and literals are not allowed to be used as actual parameters because they might be changed in the procedure.

Here's an example which mixes call-by-value and call-by-reference:

procedure Name (a, b : integer; VAR c, d : integer);
begin
  c := 3;
  a := 5
end;

begin
  alpha := 1;
  gamma := 50;
  delta := 30;
  Name (alpha, 2, gamma, delta);
end.

Immediately after the procedure has been run, gamma has the value 3 because c was a reference parameter, but alpha still is 1 because a was a value parameter.

This is a bit confusing. Think of call-by-value as copying a variable, then giving the copy to the procedure. The procedure works on the copy and discards it when it is done. The original variable is unchanged.

Call-by-reference is giving the actual variable to the procedure. The procedure works directly on the variable and returns it to the main program.

In other words, call-by-value is one-way data transfer: main program to procedure. Call-by-reference goes both ways.

 ◄   ▲   ►