Delphi's Basic Pascal Grammar (fifth. function and process programming)

Source: Internet
Author: User

The fifth chapter, function and process programming
Functions (including standard functions and custom functions)
1. Definition of function
Format: Function name (< parameter list >): return value type;
Var
< variable description >
BEGIN
< function body >
END
[Note]: 1, formal parameter list each parameter is indicated its type;
2. There is only one return value, and the return value is assigned to the function name.
[Example]: To find the area of Pentagon
function Area (a,b,c:real): real;
Var
P:real;
Begin
p:= (A+B+C)/2;
Area:=sqrt (p* (p-a) * (p-b) * (P-C);
End
2. Invocation of function
1), the invocation of the function must appear in the expression (to the right of the equal sign) and cannot be represented in the statement individually
2), when invoked, passes each argument to the corresponding parameter
Program Ml_area (Input,output);
Var
L1,l2,l3,l4,l5,l6,l7,s:real;
function Area (a,b,c:real): real;
Var
P:real;
Begin
p:= (A+B+C)/2;
Area:=sqrt (p* (p-a) * (p-b) * (P-C);
End
Begin
Write (' input ');
READLN (L1,L2,L3,L4,L5,L6,L7);
S:=area (L1,L2,L6) +area (L2,L6,L7) + ...;
Writeln (' s= ', s:10:3);
End.

Second, the process
Format:procedure< Process name > (< form parameter table >)
Var
< description section >
BEGIN
< process body >
END;

[Example 1] The formal parameter list of a process is (var a,b:real;c,d:integer;var e:char);
The main program has the following variable description: x,y:real; M:integer; Next:char;
The following argument tables are valid: (1, 2, 3)
1, (X,y,m+3,70,next)
2, (Y,x,m,m,next)
3, (y,x,35,m DIV 10,next)
4, (M,x,30,10,next)
5, (x,y,m,10)
6, (35,0,y,m,10,next)
7, (X,y,m,m/10,next)
8, (A,b,30,10,next)

[Example 2] The result of the program output is obtained.
Program EX (INPUT,OUTPUT);
Var
X,y,z:integer;
procedure S (X:integer; var y:integer);
Var
Z:integer
Begin
x:=5; y:=6; z:=7;
End
Begin
X:=1; y:=2; z:=3;
S (x, y);
Writeln (x, y, z);
End.
Output results: 1 6 3

[Example 3]: Find the maximum value in five numbers.
Program Max_in_5 (Input,output);
Var
N1,n2,n3,n4,n5:integer;
Procedure Lagest_in_three (A,b,c:integer;var E:integer)
Begin
E:=a;
If B>e Then
E:=b;
If C>e Then
E:=c;
End
Begin
Write (' ... ');
READLN (N1,N2,N3,N4,N5);
Lagest (N1,N2,N3,N1);
Lagest (N1,N4,N5,N1);
Write (N1);
End.

Three, nested
[Example]: calculation,
Program CMN (Input,output);
function Fax (x:integer): integer:
Var
I:integer;
Begin
Fac:=1;
For I:=1 to X do
Fac:=fac*i;
End
Function C (a,b:integer): real;
Begin
C:=FAC (a)/FAC (b) *FAC (a)
End
Begin
Writeln (' C (9,3) = ', C (9,3));
Writeln (' C (8,5= ', C (8,5));
End.

Iv. recursion
A function or procedure calls itself, called recursion.
[Example 1], recursive calculation n! {=m!/n! (M-N)!}
Program Fac_n (Input,output);
Var
N:integer;
function FAC (n:integer): integer;
Begin
If N=1 then fac:=1
else FAC:=N*FAC (n-1);
End
[Example 2], recursive calculation

1), Function Power (a,b:integer): integer;
Begin
If B=1 then Power:=a
else Power:=a*power (a,b-1);
End
2), Procedure Power (A,b:integer;var C:integer);
Begin
If B=1 then C:=a
else power (A,B-1,C);
C:=a*c;
End

[Example 3], enter a string (with '. ') End) Press Inverse output
Program revers (input,output);
Procedure Rever;
Var
C:char;
Begin
Read (c);
If c<> '. ' Then rever;
Write (c);
End
Begin
Rever;
End.

V. Scope of identifiers
1. Global variables and local variables
1), Global variables: In the main program is described, scope: the entire program;
2), local variables: is described in the subroutine, scope: the main program and its subordinate program.
2. Scope of subroutine (function, precedure)
A subroutine can call its next-level program, or it can invoke a sibling subroutine defined before it.
[Example] write out the result of the program running.
Program range (input,output);
Var
X,y:integer;
Procedure P1;
Var
X,z:integer;
Begin
x:=10; y:=y+1; z:=10;
Writeln (x, y, z);
End
Begin
X:=1; Y:=1;
Writeln (x, y);
P1;
Writeln (x, y, z);
End.

The result is:
(x y z)
1 1
10 2 10
1 2 (Error)

3, in the subroutine is changed the value of the variable can be returned two methods:
A, variable parameter procedure (var variable name);
b, the procedure defined at the previous level, and the procedure at the next level has not been redefined.
[note] If the main program's variables are redefined in the subroutine, the variables of the main program are stored in the subroutine, and the variables in the main program are invalid.

Delphi's Basic Pascal Grammar (fifth. function and process programming)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.