Chapter II-delphi Object-oriented Programming (iii) (1)

Source: Internet
Author: User

2.1.8.4 The statement part of a procedure and function

The statement portion of a procedure or function begins with the beginning and ends with end. function requires a return value. You can assign a return value to a function name, or you can assign a return value to a result variable. The following routine assigns the return value to the function name:

function Calculateinterest (principal,interestrate:double):D ouble;

Begin

Calculateinterest: = Principal * interestrate;

End

It is also possible to assign the return value to a result variable, and the above procedure should read:

Result: = Principal*interestrate;

Here's how to call this function:

interestearned: =calculateinterest (2000,0.012);

The procedures and functions that follow the implementation are available and can only be used by the event-handling process of this library unit. To allow procedures and functions to be used by other library units, you need to place the title portion of a procedure or function in the interface part of the library unit, and place the entire process or function with the caption in the inplementation part of the library unit, and add the library unit name that describes the procedure or function in the uses clause of the library unit where you want to access the procedure or function.

Recursive invocation of 2.1.8.5 function

In Object Pascal, a procedure or function must first describe the call again. The Novalue function above must be described and executed before using its event-handling procedure, or the program will report an unknown identifier error.

The rules above are exceptions when recursive calls. The so-called recursive invocation refers to the case where function a calls function B, and function B calls function A. In a recursive call, a function is preceded by a reserved character ForWord at the end of the function or procedure's title section. The following routines are a typical example of a recursive call:

...

Implementation

Var

Alpha:integer;

Procedure Test2 (Var a:integer): ForWord;

{Test2 is described as a predecessor process}

Procedure Test1 (var a:integer);

Begin

A: =a-1;

If A>0 Then

Test2 (A); {The procedure Test2} is invoked after the preceding description.}

Writeln (A);

End

Procedure Test2 (var a:integer); {The execution part of the TEST2 with the preceding description}

Begin

A: =a Div 2;

If A>0 Rhen

Test1 (A); {The executed procedure Test1} is invoked in Test2.}

End

Procedure Tform1.button1click (Sender:tobject);

Begin

Alpha: = 15; {Assign an initial value to alpha}

Test1 (Alpha); {First call to Test1, recursive start}

End

The button's OnClick event-handling process gives alpha an initial value and implements a cyclic recursive call of 1 minus 2 before Alpha is less than 0.

Parameters for 2.1.8.6 procedures and functions

When your program code calls a procedure or function, it usually passes the data to the invoked procedure or function. The most commonly used parameters are three kinds of numerical parameters, variable parameters and constant parameters.

Parameters defined by the invoked procedure or function are formal parameters, and the arguments specified by the calling procedure or function are called arguments. In the Novalue function, the aneditbox in the function body is the formal parameter, and the call is in the If Novalue (Edit1) ... , Edit1 is the argument.

The numeric parameter only changes the value of its formal parameter in the course of operation, and does not change the value of the parameter, that is, the value of the argument cannot be passed to the outside of the procedure. Let's try the following routines:

Procedure Calculate (Calno:integer);

Begin

Calno: = calno*10;

End

Call the Calculate function with the following routine:

...

Number: = Strtoint (Edit1.text);

Calculate (number);

Edit2.text: = inttostr (number);

...

Number accepts the value entered by the edit box 1 and is calculate by the process. It is a numeric argument. After entering the Calculate function, the number argument is copied to the formal parameter calno, the Calno is increased by 10 times times in the process, but is not passed, so the number value does not change, and in edit box 2, the input value in edit box 1 is still displayed. The formal parameters and arguments occupy different memory addresses, and when a procedure or function is invoked, the value of the argument is copied into the memory occupied by the parameter. Therefore, after a procedure or function, the values of the formal parameter and the argument are different, but the value of the argument does not change.

If you want to change the parameter values that you pass in, you need to use the variable argument, which is preceded by the reserved word var in the parameter table of the invoked program. For example:

Procedure Calculate (var calno:integer);

Calno does not occupy one position in memory, but instead points to the argument number. When a variable parameter is passed, any changes to the formal parameters are reflected in the argument. This is because two parameters point to the same address. To precede the parameter calno of the procedure header in the previous routine with Var and invoke it with the same program, the result of the calculation is displayed in the second edit box, and the value in the first edit box is magnified 10 times times. The value of the parameter Calno and the argument number is 10 times times the initial value of the Nnmber.

If the procedure or function execution is a requirement to not change the value of the formal parameter, the safest approach is to use constant arguments. You can make a parameter constant by adding a reserved word const to the parameter table before the argument name. Using a constant parameter instead of a numeric parameter protects your argument so that you do not accidentally assign the new value to this parameter when you do not want to change the parameter value.

2.1.9 defines a new data type

Object Pascal has some system-predefined data types that have been described in 2.1.2. You can take advantage of these data types to create new data types to meet the specific needs of your program. The following is a brief description of the major data types you can establish, such as enumerations, subcategories, arrays, sets, records, objects, and so on.

2.1.9.1 Enumeration types

A description of an enumerated type lists all the values that this type can include:

Type

Tdays= (Sunday, monday,tuesday,wednesday,thursday,friday,saturday);

You can define variables for the above enumerated types:

Var

Dayofweek:tdays;

In an enumerated type, each value in parentheses has a cosmetic value that is determined by the position in which it is described. For example, the Sunday has a 0,monday value of 1, such as cosmetic value. You can describe DayOfWeek as a shape variable, and you will be able to reshape the value of each one week to achieve the same effect, but using an enum will make the program readable and easy to write. When you list a value in an enumerated type, you also indicate that the value is an identifier. For example, if your program already contains the Tdays type and describes the Dayofweeks variable, then the Monday variable cannot be used in the program because it is already described as an identifier.

2.1.9.2 Child boundary type

The child boundaries are values within a range of these types: shaping, Boolean, character, or enum. When you want to limit the range of values for a variable, the child boundary is very useful.

Type

Thours = 0..23;

Tvalidletter = ' A '. ' F ';

Tdays = (Sunday, Monday,tuesday,wednesday,thursday,

Friday,saturday); {Enum type}

Tworkday = Monday. Friday; {A tdays of a type}

A child boundary defines the range of possible values for a variable. When the scope check is open, {$R * is behind the implementation of the Library unit. DFM} The typeface indicates that the range check is open, otherwise you can options| Project| Complier options, select Range cheking to open a range check), a range check error occurs if the variable is taken to a value other than the child bounds.

2.1.9.3 array type

An array is an ordered combination of a data type in which the value of each element is specified by its relative position, where you can place the data at a location and use that data when needed. The following type illustrates an array variable of type double:

Var

Check:array [1..10] of Double;

It means that the check points to a data string column with 10 double elements, representing a number between 1 and 10 for each element, called an index. Each item of an array is represented by an array name plus an index in []. Check contains 10 variables, Check[1] represents the first variable. You can also define the array as a type:

Type

Tcheck = array[1..10] of Double;

The variable description should read:

Var

Check:tcheck;

You can use arrays by assigning values to arrays. The following statement assigns 0.0 to all the elements in the check array:

For J: = 1 todo

CHECK[J]: = 0.0;

Arrays can also be multidimensional, and the following type defines an array of 20 rows and 20 columns.

Type

Ttable = array[1..20,1..20] of Double;

Var

table1:ttable;

To initialize all of the data for this table to 0.0, you can use a For loop:

Var

Col,row:integer;

...

For Col: =1 todo

For Row: = 1 todo

Table1[col,row]: = 0.0;

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.