I. routine: a unique name in Delphi. Routine is a form of encapsulating the code of a specific function:
1. Process
2. Function
The difference between a procedure and a function is whether there is a return value
Function of routine
1. It can solve the problem of naming conflict
2. Improve code reuse rate
3. Improve code maintainability
III. use history
1. Definition
1 procedure Demo1(); //process 2 begin 3 4 end;
Definition of function
1 function Demo2(var a : Integer;var b : Integer) : Integer; //function 2 begin 3 Writeln(a,',',b); 4 end; 5 6 {The number of transfer parameters is not fixed (multiple parameters)} 7 procedure Demo3(const num1 : array of Integer); 8 var 9 X,Y : Integer; 10 begin 11 12 end; 13 14 begin 15 Demo3([1,2,3,4]); 16 Readln; 17 end.
4. Routine return value
1 2 function add (num1: Integer; num2: Integer): Integer; / / the last Integer declares the operation result data type 3 begin 4 Result: = num1 + num2; / / result get return value 5 exit; / / terminate 6 end; 7 var 8 Sum : Integer; 9 begin 10 Sum := Add(2,3); 11 Writeln(Sum); 12 Readln; 13 end.1 {Return value: get the operation result by reference} 2 function Sub1(Num3 : Integer;Num4 : Integer;var sum : Integer) : Integer; 3 //var sum : Integer Incoming address 4 begin 5 sum := Num3 - Num4; 6 Exit; 7 end; 8 9 var 10 Sum1 : Integer; 11 begin 12 Sub1(5,3,Sum1); 13 Writeln(Sum1); 14 Readln; 15 end.
1 {Return value:out} 2 function mul(Num5:Integer;Num6:Integer;out sum:Integer):Integer; 3 begin 4 sum := Num5 * Num6; 5 Exit; 6 end; 7 var 8 sum : Integer; 9 begin 10 mul(5,3,sum); 11 Writeln(sum); 12 Readln; 13 end.1 2 function GetResultByFunName():Integer; 3 begin 4 GetResultByFunName := 1+3; 5 end; 6 begin 7 Writeln(GetResultByFunName()); 8 Readln; 9 end.
1 {overload Function overload: same function name and different parameter list,Use keyword identification} 2 function Add(Num1 : Integer;Num2 : Integer;var sum : Integer) : Integer; overload; 3 begin 4 sum := Num1 + Num2; 5 Exit; 6 end; 7 8 function Add(Num1 : Double;Num2 : Double;out sum : Double) : Double; overload; 9 10 begin 11 sum := Num1 + Num2; 12 Exit; 13 end; 14 15 var 16 // sum : Integer; 17 sum : Double; 18 begin 19 // Add(2,3,sum); 20 Add(2.4,2.02132,sum); 21 Writeln(sum); 22 Readln; 23 end.
1 {forward forward declaration} 2 function DivFun() : Integer; forward; 3 4 function MilFun() : Integer; 5 begin 6 DivFun(); 7 end; 8 9 function DivFun() : Integer; 10 begin 11 Writeln('test'); 12 end; 13 begin 14 MilFun(); 15 Readln; 16 end.