Worksheet 12 (Value and Variable Parameter in Procedure)

advertisement
HONG KONG CHINESE WOMEN'S CLUB COLLEGE
F.4 COMPUTER STUDIES PROGRAMMING EXERCISE (12)
Class:________ Name:__________________________ Class No.:_______
Parameters
The use of parameters provided a channel for the exchange of data between the main
program and the procedure. Data are transferred between the actual parameters
(which is included in procedure calling) and the formal parameters (which is defined in
the procedure heading).
PROGRAM xxx;
PROCEDURE aaa(formal parameters);
BEGIN
:
:
END;
BEGIN {main program}
:
aaa(actual parameters);
:
END.
Note: (1) The number of actual parameters in procedure calling must be the same as
the number of formal parameters in procedure definition.
(2) Each actual parameter must be of the same type as its corresponding formal
parameter.
1
(A) Value Parameter
PROGRAM xxx;
VAR a:INTEGER;
b:STRING;
PROCEDURE aaa(x:INTEGER;y:STRING);
BEGIN
:
{process the values of x and y}
:
END;
BEGIN {main program}
:
aaa(123,'pqrst');
:
aaa(a,b);
:
END.
Note: When data is transferred between an actual parameter and a value parameter,
the value of the actual parameter is assigned to the value parameter.
2
Value Parameter for Input Only
Example (1):
program xxx;
var a:integer;
procedure abc(p:integer);
begin
writeln(p);
end;
begin (*main program*)
abc(15);
abc(2*3+4);
a:=100;
abc(a);
abc(5*a-4);
end.
What is the output of the above program?
3
Example (2):
program xxx;
var a,b:integer;
c:string;
procedure abc(p,q:integer;r:string);
begin
writeln(p);
writeln(q);
writeln(r);
end;
begin (*main program*)
abc(15,5+4*7,'Hello');
a:=100;
b:=30;
c:='computer';
abc(a+5,7,c);
end.
What is the output of the above program?
4
(B) Variable Parameter
PROGRAM xxx;
VAR a:INTEGER;
b:STRING;
PROCEDURE aaa(VAR x:INTEGER;VAR y:STRING);
BEGIN
:
{process the values of x and y}
:
END;
BEGIN {main program}
:
aaa(a,b);
:
END.
Note: When the procedure with variable parameter is called, the
actual parameter is substituted for the formal parameter
in the procedure (i.e. they share the same memory location)
This results in a two-way (input/output) transfer of data
between the procedure and the main program.
5
Variable Parameters for Input and Output
Example (1):
Example (2):
program xxx;
program yyy;
var a:integer;
var a:integer;
procedure abc(var p:integer);
procedure abc(p:integer);
begin
begin
writeln('p=',p:10);
writeln('p=',p:10);
p:=10000;
p:=10000;
end;
end;
begin (*main program*)
begin (*main program*)
a:=1;
a:=1;
abc(a);
abc(a);
writeln('a=',a:5);
writeln('a=',a:5);
a:=2;
a:=2;
abc(a);
abc(a);
writeln('a=',a:5);
writeln('a=',a:5);
end.
end.
What is the output of the
What is the output of the
above program?
above program?
6
A Combination of Simple Parameters and Variable Parameters
program xxx;
var a,b:integer;
procedure abc(p:integer;var q:integer);
begin
writeln('p=',p:10);
writeln('q=',q:10);
p:=10000;
q:=5000;
end;
begin {main program}
a:=1;
b:=2;
abc(a,b);
writeln('a=',a:10);
writeln('b=',b:10);
end.
What is the output of the above program?
7
Exercise:
1. (a) Given the following distance formula:
distance = (x2-x1)2 + (y2-y1)2
where (x1,y1) and (x2,y2) are the coordinates of two points.
Write a procedure to calculate the distance between two points.
(Note: x1,x2,y1,y2 are value parameters and d is the variable parameter)
(b) Write a complete Pascal program to calculate the perimeter of a triangle from the
coordinates of its three vertices (a,b), (c,d) and (e,f). These coordinates are
inputted by the user. The main program should call the above procedure to
calculate the lengths of the three sides of the triangle and then add them together
to get the perimeter.
Test your program with the following test data:
(1,2), (3,4), (5,6)
The calculated perimeter should be 11.3
8
program distance;
var i:integer;
perimeter:real;
x,y:array[1..3] of integer;
d:array[1..3] of real;
procedure calculate_distance(x1,x2,y1,y2:integer;var d:real);
begin
d:=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
end;
begin {main program}
writeln('Enter the coordinates of A, B and C of the triangle');
for i:=1 to 3 do
begin
writeln('Point ',i);
readln(x[i],y[i]);
end;
calculate_distance(x[1],x[2],y[1],y[2],d[3]);
calculate_distance(x[2],x[3],y[2],y[3],d[1]);
calculate_distance(x[3],x[1],y[3],y[1],d[2]);
perimeter:=d[1]+d[2]+d[3];
writeln('The perimeter is ',perimeter:0:1);
end.
9
Download