Reference parameter passing (extracted from Delphi basic)

Source: Internet
Author: User

Passing by reference means that the subroutine actually refers to the passed variable rather than its value. Any changes to the value will affect the caller variable.We declare a variable to be passed by reference withVaRPrefix. rewriting the above Code to use by reference changes matters:
procedure doit (var a: integer);
begin
: = A * 2;
showmessagefmt ('a in the procedure = % d', [a]);
end;
procedure tform1.formcreate (Sender: tobject );
var
A: integer;
begin
A: = 22;
showmessagefmt ('a in program before call = % d', [a]);
// call the procedure
doit ();
showmessagefmt ('a in program now = % d', [a]);
end;

A In program before call = 22
A In the procedure = 44
A In program now = 44

Now the callerAVariable is updated by the procedure.
This is a very useful way of returning data from a procedure, as used by, for example, the Delphi insert routine. It also allows us to return more than one value from a subroutine.

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.