Pascal (parameter transfer)

Source: Internet
Author: User
Value parameter: the value of the real parameter is assigned to the corresponding parameter. Changes in the shape parameter do not affect the real parameter.

Unit first; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) btn1: tbutton; edt1: tedit; edt2: tedit; Procedure btn1click (Sender: tobject); Private function formalparameter (X, Y: integer): integer; {private Declarations} public {public declarations} end; var form1: tform1; implementation {$ R *. DFM} procedure tform1.btn1click (Sender: tobject); var X, Y: integer; R: integer; begin X: = 1; Y: = 100; R: = formalparameter (X, y); edt1.text: = 'X = '+ inttostr (x); edt2.text: = 'y =' + inttostr (y); MessageBox (0, pchar (inttostr (R), 'r value: ', mb_ OK); end; function tform1.formalparameter (X, Y: integer): integer; begin X: = x + 1; Y: = Y + 2; Result: = x + y; end.

Output: x = 1, y = 100, r = 104

Variable Parameter: The Variable Parameter passes a reference pointing to the parameter, that is, the pointer. Changing the parameters passed by reference will affect the actual parameters.

Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) btn1: tbutton; edt1: tedit; edt2: tedit; Procedure btn1click (Sender: tobject); Procedure variableparameter (var x, Y: integer); Private {private Declarations} public {public declarations} end; var form1: tform1; implementation {$ R *. DFM} procedure tform1.btn1click (Sender: tobject); var X, Y: integer; begin X: = 100; Y: = 100; variableparameter (x, y); edt1.text: = 'x value: '+ inttostr (x); edt2.text: = 'y value:' + inttostr (y); end; Procedure tform1.variableparameter (var x, Y: integer); begin X: = x + 1; Y: = Y + 1; end.

Output: x = 101, y = 101

Constant parameters: parameters passed to a function or process cannot be changed in a function or process,

Function myfunction (const X, Y: integer): integer; begin // X: = x + 1; error Syntax: The parameter value cannot be changed // y: = Y + 1; result: = x * Y; end;
4. array parameters: An array can be used as a function or process parameter, but the declaration of an array parameter cannot contain the declaration of the array index type.

Procedure sort (A: array [1 .. 10] of integer); // incorrect type tdigits = array [1 .. 10] of integer; Procedure sort (A: tdigits); // you can use dynamic arrays as parameters for functions and procedures. Example: Procedure clear (var a: array of real); var I: integer; begin for I: = 0 to high (a) Do A [I]: = 0; end;

 

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.