Functions and procedures (procedures) [1]

Source: Internet
Author: User

FunctionAndProcessIt is a code segment independent of the main program. In the program, when you need to complete some specific actions, these code segments will be executed. You can call these functions at any time throughout the program.

There are two forms of routines in Delphi:Process (procedures)AndFunctions).

The process is the operation that you require the computer to perform; the function is the calculation that can return values. The difference between the two lies in that the function can return the calculation result, that is, there is a return value, but the process does not.

By writing a function example, you can get to know about it first.

1,CreateApplication.

2,InForm1PlaceButton component(Btn1) andLabel component(Lbl1), such:

3,InCode EditorInImplementationEnter the following code:

...var  Form1: TForm1;implementation{$R *.dfm}procedure SayHello;begin  ShowMessage('Hello, World');end;function Multiply(Num1, Num2: Integer): Integer;begin  Result := Num1 * Num2;end;end.

4,ThenForm DesignerDouble-clickBtn1CreateOnclickEnter the following code for the event handler:

Procedure tform1.btn1click (Sender: tobject); var X: integer; begin X: = multiply (10, 20); // call the multiply function and return 10*20 results to X lbl1.caption: = inttostr (x); // The result 200 sayhello is displayed in the label Label; // call the sayhello process end;

5,Run the programF9Click the button to change the label to 200 in the result, and the Hello, world dialog box is displayed. The result is as follows:

6,The entire workflow is as follows:

CallBtn1clickEvent Handler.MultiplyFunction, with 10 and 20 as the passing parameters. The result is placed in the X variable and displayed in the lbl1 label.

7,Each function has a nameResultIs declared by the compiler in an invisible manner, and it is used to save the return value of the function. Therefore, to return a specified value from the function, you only need to assign this value to the result variable in the function.

8,There are multiple methods to call a function. The passed variable can be a direct value or another function call result. The following are the correct call methods:

X: = multiply (2, 5); {pass the value directly} X: = multiply (a, B); {pass the variables a and B} lbl1.caption: = inttostr (multiply (x, y); {return value is used as a parameter of another function} multiply (x, y); {return value is invalid}

Tip

  • As long as you repeatedly use a piece of code in the program, you can move the code to a sub-program. In this way, this subroutine can be called wherever this code is required in the program.
  • If no return value is required, the function can be called as a process.
  • When functions and procedures do not have parameters, you can directly call them by name without the empty parentheses. In the exampleSayhelloCall.

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.