var, out, and const in Delphi function parameter decoration

Source: Internet
Author: User

(1 ) var modifier

Adding Var is an address pass that modifies the original variable

Var

s:string;

Begin

S: = ' Hello ';

Changesvar (s);

ShowMessage (S);

End

Changesvar definition

Procedure Tform1.changesvar (var a:string);

Begin

A: = A + ' world ';

End

The above will output Hello world, because it is the address, modify the original A

(2 ) without any modifiers

Var

s:string;

Begin

S: = ' Hello ';

Changes (s);

ShowMessage (S);

End

Changes definition

Procedure Tform1.changes (a:string);

Begin

A: = A + ' world ';

End

The above will output hello, because the method changes actually creates a new a, and the output is the original A, the value does not change

(3 ) out modifier

Var

s:string;

Begin

S: = ' Hello ';

Changesout (s);//At this time the value of S is ' Hello ' instead of ' Hello,world '!, the original value of s in the procedure Hello is discarded

ShowMessage (S);

End

Changesout definition

Procedure Tform1.changesout (out a:string);

Begin

A: = A + ' world ';

End

The above output world,out only accept the returned value, and any input to out will be ignored. The actual arguments that are passed to the procedure at the same time do not have to be initialized, such as calls to Changesout:

Var

tmp:string;

Begin

Changesout (TMP);//compilation can also be done by

End;

(4) Const modifier

Const-decorated parameters are not allowed to be modified after they are passed in

If you modify parameters in the process, you will get an error, such as:

Procedure xxxx. Testconst (const a:string);

Begin

A: = ' ss '; Attempt to modify const modified parameters will be an error

End;

var, out, and const in Delphi function parameter decoration

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.