delphi--processes and functions [Delphi]

Source: Internet
Author: User
Tags types of functions

Originally from: http://my.oschina.net/pc100/blog/380016

The procedure starts with the reserved word procedure, has no return value, and the function starts with the reserved word function and has a return value.

Parameters are enclosed in parentheses, and multiple parameters are separated by semicolons, for example:

Procedure SetDate (Year:integer; Month:integer; Day:integer);

You can also group parameters of the same type together, and the above procedure title is written as:

Procedure SetDate (year, Month, Day:integer);

The function has one more item in the caption--the type of the return value. The following is a function caption with a return value of type double:

function Getaverage (num:integer;total:double):D ouble;

1. Basic Concepts

(1 ) function

There are two types of functions: one is the standard function, which is defined and implemented by Delphi, the programmer can refer directly, and the other is a custom function, a custom function should first be declared in the Program Declaration section before it can be called in the execution part of the program.

The statement portion of a procedure or function is terminated by begin and end. The function requires a return value. The return value can be assigned to the function name, or the return value can be assigned to the result variable.

Cases::

function Getaverage (num:integer;total:double):D ouble;

Begin

Getaverage: = Total/num;

End

You can also assign the return value to the result variable:

Result: = Total/num;

(2 ) Process

The reserved word for the definition process is procedure.

There are two types of processes: a standard procedure defined for Delphi, and a custom process for another class.

2. parameters and scope

(1 ) Parameter

There are two ways in which processes and functions communicate with the outside world: one is through the global volume and the other through parameters. The most commonly used parameters are the value parameter, the variable parameter and the constant parameter 3 kinds.

· If the parameter is defined as a value parameter, the call to the function has no effect on the value of the argument.

· Variable argument (var declaration) If the parameter is defined as a variable argument, the result of the function call has an effect on the value of the argument.

· Constant parameter (const declaration) If the parameter is defined as a constant parameter, the value of the formal parameter is not changed when the procedure or function executes.

If the value of the formal parameter is not changed when the procedure or function executes, the most safe way is to use the constant parameter. Precede the parameter name of the formal parameter table with the reserved word const to make a formal parameter a constant argument.

(2 ) Scope

1) Global variables are variables defined in the main program.

2) A local variable is a variable defined in a procedure or function.

Global variables apply to the entire program, and local variables are only valid for the procedure or function that defines it.

The Object Pascal language has the following conventions for the scope of variables:

1) at different levels you can define a variable with the same name, but 1 variables in the same layer can only be defined 1 times.

2) When a variable of the same name is defined at different levels, it represents a different object. When the inner program is executed, the outer non-local variable exists, but it has been masked, and when the program exits from the inner layer, the local variables of the inner layer are no longer present, only the non-local variables of the outer layers still exist.

delphi--processes and functions [Delphi]

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.