Delphi's syntax knowledge and parameter transfer problem, passing parameters by reference, passing parameters by value method

Source: Internet
Author: User

The difference between exit,abort,break,continue in Delphi

Exit: Exit Function Body
Abort: Encounter exception, quiet processing, just do not show not prompt
Break: Exits the current loop body, including for, while, repeat and other loop bodies
Continue: Ends this processing within the loop and continues execution from the beginning of the loop body

Exit is jumping out of the current code block, which is the current function, and is going to continue execution down (if there is a subsequent code).

Abort is coming from Eabort, can inspire exception, its essence is Abort = RaiseException (), is a non-occurrence of the dialog box exception. So the behavior of Abort is the same as the exception, and its code execution order is also the process of follow exception.
For example:
Try
(1)//execute the
Abort
(2)//Do not execute
exception
(3)//execute the
End

Use Abort to execute the code inside the exception, but if you use Exit, go straight away, regardless of the exception.

For example:
Procedure P1;
Begin
P2;
P3;
End
procedure P2;
Begin
Abort Exit
End
Procedure P3;
Begin
ShowMessage ().
End

If you use Abort, you do not P3, and if you use Exit, you can execute to P3

******************************************************************************

In Delphi process, the function passes the parameter several modifiers are const, Var, out. Another non-modifier is to pass the parameter by value by default.
First, the default way to pass parameters in value mode
Procedure Tform1.procnormal (value:string);
Begin
Orignum:=value+ ' Me ';
Lblreturn.caption:=orignum;//orignum for ' Hello Me '
Lblorig.caption:=value;//value for ' Hello '
End

Call:
orignum:= ' Hello ';
Procnormal (Orignum);

Second, in a const way to pass parameters, this parameter can not be changed during the call process, and this method is optimized by the compiler, it is generally recommended to use this method as much as possible.
Procedure Tform1.procconst (const value:string);
Begin
Orignum:=value+ ' Me ';
lblreturn.caption:=orignum;//for ' Hello Me '
lblorig.caption:=value;//for ' Hello Me '
End

Iii. passing parameters by reference
Procedure Tform1.procref (var value:string);
Begin
Orignum:=value+ ' Me ';
lblreturn.caption:=orignum;//for ' Hello Me '
lblorig.caption:=value;//for ' Hello Me '
End

Four, in the way of passing parameters, when passing parameters, the parameters can not be initialized, even if the value is ignored, it is generally used for output, it can be implemented in a process to return multiple values, we typically use it in a distributed object model, such as COM.
Procedure Tform1.procout (out value:string);
Begin
Orignum:=value+ ' Me ';
lblreturn.caption:=orignum;//for ' Me '
lblorig.caption:=value;//for ' Me '
End

Five, no type parameter, this is a more special method, the type of the parameter is not deterministic, only with const, Var, out decoration, can not be used for the value of the way to pass parameters, the use of the example is as follows:
Procedure Tform1.procuntype (const Value);
Begin
Orignum:=string (Value) + ' Me ';
lblreturn.caption:=orignum;//for ' Hello Me '
Lblorig.caption:=string (Value);//For ' Hello Me '
End

The default parameter, that is, if this parameter is not provided at the time of invocation, the default value is used.
Procedure Tform1.procdefault (const Value, constdefaultvalue:string= ' 123 ');
Begin
Orignum:=value+ ' Me ' +defaultvalue;
lblreturn.caption:=orignum;//for ' Hello Me 123 '
lblorig.caption:=value;//for ' Hello Me 123 '
End

The open array parameter, that is, the number of elements in the parameter array is indeterminate.
Procedure Tform1.procarray (const value:array of string);
Var
I:integer;
Begin
For I:=low (value) to high (value) does
orignum:=orignum+value[i];//called after ' Hello ABC dbd '
Lblreturn.caption:=orignum;
End

Call:

orignum:= ' Hello ';
Procarray ([' abc ', ' dbd ']);

No type open array parameter, that is, the type and the number of elements are indeterminate. In the WIN32 platform, the type of this parameter is actually an array oftvarrec, which uses the following example:
Procedure Tform1.procarrayconst (const value:array of const);
Var
I:integer;
Begin
For I:=low (value) to high (value) does
With Value[i] Do
Case VType of
vtansistring:orignum:= orignum+string (vansistring);
Vtinteger:orignum:=orignum+inttostr (Vinteger);
Vtboolean:orignum: = Orignum + booltostr (Vboolean);
Vtchar:orignum: = Orignum + Vchar;
Vtextended:orignum: = Orignum + floattostr (vextended^);
Vtstring:orignum: = Orignum + vstring^;
Vtpchar:orignum: = Orignum + Vpchar;
Vtobject:orignum: = Orignum + vobject.classname;
Vtclass:orignum: = Orignum + vclass.classname;
Vtcurrency:orignum: = Orignum + currtostr (vcurrency^);
Vtvariant:orignum: = Orignum + string (vvariant^);
Vtint64:orignum: = Orignum + inttostr (vint64^);
End
lblreturn.caption:=orignum;//called after ' Hello ABC 3 '
End

Call:

orignum:= ' Hello ';
Procarrayconst ([' ABC ', 3]);

These are some common ways of passing parameters.

Another article about the Delphi parameter transfer//

 delphi parameter passing   parameter passing      declaration/Implementation A procedure uses a parameter called a formal parameter (the formal argument), and the parameter passed in when the procedure is called is called the actual parameter (the argument).  {Info is a formal parameter} procedure showinfo (info:string); Begin  showmessage (info);end; var   s:string;begin  s: = ' LXPBUAA ';   {s is the actual parameter}  showinfo (S);end;  parameter passing is divided into two types: by Val) and references (by ref). The essential difference between these two approaches is that when passed by value, formal parameters and arguments are two variables, and they begin with the same value, that is, the data of the argument is copied to the formal parameter. So at this point, the parameter changes do not affect the argument. When a reference is passed, the formal parameters and arguments are the same variables, and one of them can be considered another alias. So at this point, when the parameter changes, the argument changes. By default, a parameter is passed by value, a copy of the data is passed, or a reference is passed if the Var prefix is added.   We look at the following example: Procedure Tform1.byval (I:integer);     {is passed I}begin  showmessage (IntToStr (@i)) by value;      {gets the address of the formal parameter. You will find that it differs from the argument address because the arguments and parameters are different for the two variables}  i: = I + 1;end; procedure tform1.byref (var i:integer); {Reference Pass I}begin  showmessage (IntToStr (Integer (@i)));      {gets the address of the formal parameter. You will find that it is the same as the argument address because the argument and the parameter are the same variable}  i: = I + 1;end; procedure Tform1.button1click (sender:tobject); Var  i:integer;begin  i: = 1;  showmessage (IntToStr (Integer (@I)));        {Obtain the address of the argument}  byval (I);  {i =1} showmessage (i); {i:=1; The argument does not change}  byref (i);  {i =2} showmessage (i); {i:=2, the argument changes the}end;  parameter passed by value can specify a default value, such as the above ByVal can be: procedure ByVal (I:integer = 0), call it can be omitted to have the default value of the parameter: ByVal. Parameters with default values must be at the end of the parameter list, such as:  procedure ByVal (i:integer = 0; B:boolean); No, it should be replaced by: procedure ByVal (B:boolean; I:integer = 0);  because the default value must be a constant expression, parameters such as Dynamic-array, procedural, class, class-reference, and interface can only specify the nil default value , and parameters of types such as record, Variant, file, and Static-array cannot specify a default value at all.   If you pass a pointer-type parameter by value, the situation becomes complex and interesting. At this point, what is actually passed? Is it a copy of the actual data? No, it is a copy of the pointer, that is, the formal parameter and the argument are two pointers, but the two pointers point to the same address. So at this point, the parameters and arguments can share their points to the data in the address, but if the pointer to the parameter is changed, the pointer to the argument cannot be changed. So to summarize, that is: when passing pointer arguments by value, the arguments and parameters can share pointers to the data in the address, but cannot share the pointer's own point. When a reference is passed, it is fully shared because the argument and the parameter are the same variable. See the following example:  procedure tform1.byval (Obj:tobject); Begin  obj: = Button1;     &nBsp {Change the parameter pointer to point, the pointer to the argument will not change, because they are two variables. If you simply change the properties of OBJ without changing the pointer, the attributes of the arguments will change}end; procedure Tform1.byref (var obj:tobject); Begin  obj: = Button1;          {change the parameter pointer to point, the pointer to the argument is changed, because they are the same variable}end; procedure Tform1.button1click (Sender:tobject); Var  obj:tobject;begin  obj: = self;             {self is Form1, so the class name of the argument obj (ClassName) is " TForm1 "}  byval (OBJ);                     {Pass pointer variable obj}   showmessage (obj.classname) by value;    {Displays the class name "TForm1"}  byref (OBJ);                        {reference pass pointer variable obj}  showmessage (OBJ.CLASSNAME);    {Display class name "TButton1"}end;  above, the most fundamental is a sentence: when passed by value, formal parameters and arguments are two variables; When a reference is passed, the parameters and the actualThe parameter is the same variable. Seize this sentence, it is equal to seize everything. (PS: Key summary): I believe you have also seen the following format parameter declaration: function comparestr (const S1, s2:string): Integer;function trystrtoint (const s:string; out Value:integer): Boolean, where the const and out keywords are used. If you have not seen such a statement, it does not matter, they are real. The parameters of a const declaration are passed by value and the parameter cannot be changed. The arguments to the out declaration are passed by reference and are primarily used to define the output parameters, that is, no input values are required (that is, the arguments do not need to be initialized), and the values passed to the parameters are ignored. If the pointer parameter is modified with const, the data in the pointer address can be modified only through the parameter, and the pointer itself cannot be modified. For example, for a const object parameter, you can modify its properties, but you cannot point it to another object. For example:  procedure showinfo (const form:tform); begin  {The following sentence cannot be passed, the compiler prompts: [Error] Unit1.pas: left side  cannot be assigned to}  {form: = Form1;}   {but modifies the data that belongs to the Form by its properties or methods}  form.caption: = ' Lxpbuaa ';   showmessage (form.caption) ;end;  at the end of this section, we have to mention a very special type of parameter: the untyped parameter (untyped parameters). Arguments that do not specify a data type when declaring are called untyped parameters. Therefore, the untyped parameter can receive any type of data syntactically. Untyped arguments must be prefixed with a const, out, or var; the untyped parameter cannot specify a default value.   As some of the following Delphi-defined procedures use untyped parameters:  procedure SetLength (var S; Newlength:integer);       {parameter s}procedure Move (const Source;var Dest; Count:integer);  &nbsp {parameter source, dest}procedure tstream.writebuffer (const Buffer; Count:longint); {parameter buffer}  the so-called untyped parameter can receive any type of value, only from a syntactic perspective. Or, theoretically, we can implement a process that can use any type of variable as a parameter, but it's not really necessary or possible. Let's say we want to build a car that can load any object. Because it is "any object", so the object may be any shape, so the car must be no hood, in addition to a few wheels on a large enough (big enough is already a big problem) outside the plate, can not have anything else. At this time, the tablet can be seen as untyped, because it can sit on people, put a table, you can also catch some animals to stand or lie down. Although it can carry many kinds of things, but there are restrictions, such as not to put a mountain, can not accommodate 10,000 pigs. Therefore, the types of untyped parameters are often limited. For example, SetLength's parameter s can only be strings, dynamic arrays, and so on. This limitation is typically done in the implementation of the procedure, and checks the actual type of the parameter value at run time. Constraints can also be built into the compiler for parameters that are closely related to the development environment. The reason for using an untyped parameter is that you cannot use a uniform type at the time of declaration to describe the possible types of the runtime, such as the SetLength parameter s can be a string and a dynamic array, and there is no uniform type to represent the string and the dynamic array type, so simply declare it as untyped. Instead, place the type restrictions somewhere else (such as the compiler). For example, SetLength's restriction rules are written in the compiler, which can only be used for long strings or dynamic arrays. When you attempt to complete the following function: Var  i:integer;begin  setlength (I, ten) the;end;  compiler will give an error message when compiling: [ERROR] Unit1.pas (+): incompatible types. Causes a compile break.   Summary This section is more important, focusing on understanding the nature of parameters by value passing and reference passing: when passing by value, formal parameters and arguments are two variables; When a reference is passed, the formal parameter and argument are the same variable. The   Declaration directive declares a process by which the register, Pascal, Cdecl, stdcall, and safecall directives can be used to specify the order of parameter passing and parameter memory management, thereby affecting the operation of the process. such as: function MyFunction (X, Y:integer): Integer; Cdecl;  These five directives have different meanings, as shown in table 3-1.   Table 3-1    Five instructions different meanings    directive      parameter storage location       Parameter pass order    parameters memory management         where to apply  register  CPU register        left-to-right       callee    default. Published Property Access                                                                      methods must use  pascal          stacks             left-to-right       callee     Backwards compatible, no longer using  cdecl           stacks   &Nbsp;        from right to left       callers        Call C + + shared libraries  stdcall          Stacks             right-to-left       callee    api called, such as callback functions   Safecall         Stacks             Right-to-left       called by caller    api, such as a callback function. Dual   In some source code (including Delphi's own VCL source code), you may also see near, far, export, and inline, assemble, and other directives, which are for the 16-bit Windows system or early pascal/ Delphi compatibility, in the current Delphi version, has no meaning, so in the new development do not use again.

Some syntax knowledge of Delphi and parameter passing problem, passing parameters by reference, passing parameters by value

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.