The function consists of one or more code, and a particular function can be implemented. Using a function can make the code more easy to read, easy to understand, speed up programming speed and reduce duplicate code. The process is similar to the function, and the process and function is the most important difference in that the process does not return value, and the function can have a return value. In Delphi 7.0, there is a lot of functions, which are generally classified, and there are 6 types: data type conversion functions, strings, array operation functions, files, disk operation functions, memory, pointer operation functions, mathematical operation functions, Date function.
In Delphi, call function, in general, can be used directly, but because some functions are not included in the unit listed in the unit (the default unit has Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms Dialogs;), so we need to add a unit manually. For example, the MIDSTR function is not included in these units, and the MIDSTR belongs in Strutils, so we add Strutils to the USES.
In this manual, all functions that are not included in the unit that are listed by default, it has indicated that the unit to which it belongs, should be noted when used.
First, the data type conversion function is used in our written programs, and multiple data types are used according to different situations. When performing different types, you must convert different types into the same type. Therefore, it is very important to skilled the conversion of data types.
1.FloatTostr function Description: This function is used to convert "floating point" into "character type".
Reference example:
Edit1.text: = floattostr (1.981);
2.intTostr function description: This function is used to convert "integer type" into "character type".
Reference example:
S: = INTTOSTR (10); (Note: s String type variable.)
3.intToHex function Description: This function is used to convert "decimal" into "decimal". This function has two parameters. The first parameter is the decimal data to be converted, and the second parameter is specified to use the number of bits to display hexadecimal data.
Reference example:
Edit1.Text: = INTTOHEX ('100', 2);
Execute result, edit1.text is equal to 64.
Note: Delphi does not provide a dedicated "hex" to convert to a "decimal" function. This feature can be implemented using the strtoinT function. The specific code is: i: = start ('s /' '64'); this time I is equal to 100. Add a "hex" to "decimal".
4.STRTOINT Function Description: This function is used to convert "characters" into "integer".
Reference example:
I: = start ('100');
Note: The type of such as StrtOINT ('Ab') or STRTOINT ('Good') is not converted because they do not exist.
5.STRTOFLOAT Function Description: This function is used to convert "characters" into "floating point".
Reference example:
N: = struploat (edit1.text);
Note: The content in Edit1.Text is 1.981 (where the text displayed in the EDIT control is string). N is the Double type for saving the converted floating point data.
Second, strings, array operation functions to operate on strings and arrays, is that each programmer must be mastered. Skilled use of these functions, can be more heartbered during programming.
1.copy Function Description: This function is used to copy the characters in the specified range from the string. This function has three parameters. The first parameter is a data source (i.e., the replicated string), the second parameter is replicated from a string, and the third parameter is to copy the length of the string (ie). The final function returns a new string (ie, we specify the string content to be copied). Reference example:
VAR
String;
MYSTR: STRING; / / Save new string
Begin
S: = 'i love china!';
// The "Love" string in i love china will be obtained below.
MyStr: = COPY (S, 3, 4);
END;
Executive results, mystr is equal to "LOVE", "LOVE" string starts in "I Love China!", So the second parameter is 3, "love" has 4 characters, so the third parameter 4.
2. Concat function Description: Connect two or more strings as a string.
Reference example:
VAR
S1, S2: STRING;
Begin
S1: = Concat ('a', 'b'); // Connect two strings, S1 variables are equal to AB.
S2: = Concat ('borland', 'delphi', '7.0'); // Connect three characters, S2 variables are equal to Borland Delphi 7.0.
END;
3. DELETE Function Description: Delete the string specified in the string. This function has three parameters. The first parameter is a string to be processed, and the second parameter begins to delete, the third parameter is the number of characters deleted.
Reference example:
VAR
String;
Begin
S: = 'i like ready cpcw.';
// The following code will delete the "C" character in the S variable.
Delete (s, 16, 1);
END;
At this point, the S variable is I like recaps PCW. ("C" does not exist).
4.High function description: Returns the maximum value of the array subscript.
Reference example:
VAR
Arrtext: array [0..9] of char;
i: integer;
Begin
I: = high (arrtext); // i's value is 9
END;
5. Insert function description: Insert a character (string). This function has three parameters. The first parameter is the character (string) to be inserted, the second parameter is inserted into a string (source string), and the third parameter is inserted from.
Reference example:
VAR
String;
Begin
S: = 'WAT is your name?';
/ / The WHAT in the above sentence checks a "h" character, below the INSERT function will add H in.
INSERT ('h', s, 2); // Insert "H" from the second place.
END;
6.leftstr Description: Returns a new character (string) that specifies a number of characters left on the left. This function has two parameters. The first parameter is a complete string, and the second parameter is specified.
Reference example:
VAR
S, A: String;
Begin
S: = 'MSN Messenger';
A: = Leftstr (s, 3); // From the left, get three characters on the left. Therefore, the A variable is equal to MSN.
END;
7.length function Description: This function is used to count the length (ie, number) of the specified string.
Reference example:
VAR
NLEN1, NLEN2: Integer; // Used to save strings length begin
Nlen1: = Length ('CPCW');
Nlen2: = Length ('Computer News ");
END;
Execution, NLEN1 is equal to 4, NLEN2 is equal to 6. Since a Chinese character is equivalent to the length of the two characters, the length of 3 Chinese characters is 6.
8.LOW Function Description: Returns the minimum of the array subscript.
Reference example:
VAR
Arrtext: array [1..9] of char;
i: integer;
Begin
I: = high (arrtext); // i's value is 1
END;
9.LowerCase function Description: Convert English characters in the character (string) to lowercase.
Reference example:
VAR
S, A: String;
Begin
S: = 'abc';
A: = Uppercase (s); // After the Uppercase function is converted, A is equal to ABC.
END;
10.MIDSTR (Sport: strutils) Function Description: Returns the string within the specified range. This function has three parameters. The first parameter is the source string, the second parameter is the starting point, the third parameter is the end point. By the second, third parameters, you can specify the range to copy the string.
The COPY function is similar to this function. MIDSTR is primarily used to process strings containing Chinese characters.
Reference example:
VAR
String;
H: String;
Begin
S: = Midstr ('China', 1, 2); // s variable is CH
H: = Midstr ('Computer News', 1, 1); // h variable is "electricity". If you use a COPY function, it should be H: = Copy ('Computer News, 1, 2), otherwise the return will not be "electricity". Therefore, when using a string containing Chinese, it is best to use the MIDSTR.
END;
11.Pos Function Description: Find the character (string) location. This function has two parameters. The first parameter is the character (string) to look up, and the second parameter is the lookup character (string).
Reference example:
VAR
Npos: integer; // The location of the character used to save the lookup
Begin
NPOS: = POS ('Like', 'I Like Reading!');
END;
At this time, NPOS is equal to 3. If it is not found, NPOS is 0.
Note: The POS function is to distinguish between the character size when looking up. If you want to implement the size, you need to use the UpperCase or LowerCase function to convert the characters (string) of the two parameters to "Upperword" or "lowercase".
There is also a function of finding characters (string) ---- ANSIPOS, the usage method of this function is exactly the same as the POS function. When you look for Chinese characters, it is best to use the ANSIPOS function.
12.rightStr Description: Returns a new character (string) of the specified number of strings on the right side. This function has two parameters. The first parameter is a complete string, and the second parameter is specified.
Reference example:
VAR
S, A: String;
Begin
S: = 'MSN Messenger';
A: = Rightstr (s, 3); // From the rightmost side, get the three characters on the right. Therefore A variable is equal to the Ger.
END;
13.SetLength Function Description: Set string or dynamic array length. This function has two parameters. The first parameter is the string variable or dynamic array variable to be set, and the second parameter is the specified length, which ranges from 0 to 255. Reference example:
VAR
String;
Arrtext: array of char; // Define a dynamic array
Begin
SETLENGTH (S, 10); // When the setting, the S variable can only assign a string of the length of 10.
SETLENGTH (Arrtext, 10); // Only dynamic arrays only after using SETLENGTH to allocate memory spaces for dynamic arrays. The role of this code is equivalent to Arrtext: array [0..9] of char
END;
14. Strpcopy Function Description: Copy the string to the character array. This function has two parameters. The first parameter is "target array", the second parameter is "string".
Reference example:
VAR
Arrchar: array [0..255] of char; // This declares a CHAR type array of length 256
Begin
Strpcopy (Arrchar, 'Come on, Baby!');
END;
15.Trim function Description: Delete spaces on the left and right sides of the string (how many spaces are all deleted regardless of the left and right sides).
Reference example:
VAR
String;
Begin
S: = 'delphi 7.0';
S: = trim (s);
END;
16.TrimleFT Function Description: Delete the space left on the left of the string (no matter how many spaces on the left are all deleted).
Reference example:
S: = trimleft ('delphi');
17.trimight function Description: Delete the space left on the left of the string (no matter how many spaces on the left are all deleted).
Reference example:
S: = trimright ('delphi');
18.Uppercase function Description: Convert English characters in the character (string) to uppercase.
Reference example:
VAR
S, A: String;
Begin
S: = 'abc';
A: = Uppercase (s); // After the Uppercase function is converted, A is equal to ABC.
END;
Third, files, disk operation function software, most of the file, disk, disk, disk. Familiar with these functions can help you easily create, delete, save files.
1.Append function description: Add content to the file. The file must exist.
Reference example:
VAR
String;
F: TextFile;
Begin
S: = 'this is a book.';
AssignFile (f, 'c: /myfile.txt'); // Establish a C: /MYFILE.TXT file with the F variable, and the file can be operated using the F variable.
Append (f); // Open file in appended manner
Writeln (f, s); // adds the contents of the S variable to the end of the text.
Closefile (f); // Close file
END;
2.ssignfile function Description: Connect to the specified file.
Reference example:
VAR
F: textfile; // Declare text file type variable
Begin
AssignFile (f, 'c: /myfile.txt'); // Establish a C: /MYFILE.TXT file with the F variable, and the file can be operated using the F variable.
END;
3.Chdir function Description: Change the current directory (folder).
Reference example:
VAR
SDIR: STRING;
Begin
SDIR: = 'c: / windows';
CHDIR (SDIR); // At this point, the current directory of the system is a C: / Windows directory.
END;
4.Closefile Function Description: Close the file. When the operation of the file is complete, you should use the Closefile function to close the open file.
Reference example:
VAR
String;
AllText: String;
F: TextFile;
Begin
AssignFile (f, 'c: /myfile.txt'); // Establish a C: /MYFILE.TXT file with the F variable, and the file can be operated using the F variable.
RESET (F); // Open file
While Not Eof (f) Do Begin // Use the While loop to determine if the file is not tail
Readln (f, s); // Read a line of text
Alltext: = allText S;
END;
Closefile (f); // Close file
END;
5.Deletefile Function Description: Delete the specified file. This function has only one parameter. This parameter is to delete the full path to the file. Returns true if deleted is successful. Returns false if the deletion fails, or the file does not exist.
Reference example:
VAR
Isok: boolean; // to save the delete status
Begin
Isok: = deletefile ('c: / my documents / index.html'); // After the function is confracted, return the result to the ISOK variable. If the ISOK variable is TRUE, the file deletion is successful.
IF isok the showMessage ('file delete success!')
Else ShowMessage ('file delete failed!');
END;
6.Directoryexists function Description: Detects the specified file clamp exists. If you exist, return TRUE, but it is false.
Reference example:
VAR
ISEXISTS: BOOLEAN;
Begin
ISEXISTS: = Directoryexists ('c: / windows');
If ISEXISTS TEN SHOWMESSAGE ('windows folder exists!')
Else ShowMessage ('windows folder does not exist!');
END;
7.DiskFree Function Description: Get the remaining space of the specified disk. This function has only one parameter. This parameter is used to specify the disk number to get the remaining space. When the parameter is 0, it means that the remaining space of the current disk is acquired, 1 is a disk, 2 is a B disk, and so on. If the return value is -1, it means that the specified disk is invalid.
Reference example:
VAR
FREESIZE: INT64;
Begin
FreeSize: = DiskFree (3); // Gets the remaining space in the C drive. The returned value is in "bytes".
END;
8. DISKSIZE Function Description: Get the space for the specified disk. This function has only one parameter. This parameter is used to specify the disk number to get the disk space. When the parameter is 0, it means that the space of the current disk is acquired, 1 is a disk, 2 is a B disk, and so on. If the return value is -1, it means that the specified disk is invalid.
Reference example:
VAR
Disksize: int64;
Begin
Disksize: = disksize (3); // Gets the space of the C drive. The returned value is in "bytes".
END;
9.EOF function description: Decision whether the file pointer is moved to the file. When the EOF function returns a value to TRUE, you cannot use the READLN function to read files. Reference example:
VAR
String;
AllText: String;
F: TextFile;
Begin
AssignFile (f, 'c: /myfile.txt'); // Establish a C: /MYFILE.TXT file with the F variable, and the file can be operated using the F variable.
RESET (F); // Open file
While Not Eof (f) Do Begin // Use the While loop to determine if the file is not tail
Readln (f, s); // Read a line of text
Alltext: = allText S;
END;
END;
10.RASE Function Description: Delete the file.
Reference example:
VAR
F: File;
Begin
AssignFile (f, 'c: /myfile.txt'); // Establish a C: /MYFILE.TXT file with the F variable, and the file can be operated using the F variable.
RESET (F); // Open file
Closefile (f); // Close file
ERASE (f); // Delete files. You must close the file before deleting the file.
END;
11.FileExists function Description: Detects the specified file to exist. If you exist, return TRUE, but it is false.
Reference example:
VAR
ISEXISTS: BOOLEAN;
Begin
ISEXISTS: = FileExists ('c: /test.txt');
If ISEXISTS THEN SHOWMESSAGE ('This file exists!')
Else ShowMessage ('This file does not exist!');
END;
12.FileSize Feature Description: Get the file size. The result of returning is byte.
Reference example:
VAR
F: file of byte;
nsize: longint;
Begin
AssignFile (f, 'c: /myfile.txt'); // Establish a C: /MYFILE.TXT file with the F variable, and the file can be operated using the F variable.
RESET (F); // Open file
nsize: = filesize (f); // Get file size.
Closefile (f); // Close file
END;
13.FORCEDIRECTORIES Function Description: Create a new subdirectory. The directory that does not exist in the path will be created together.
Reference example:
Forcedirectories ('c: / flash / myfolder'); // If the Flash folder itself is not allowed, you will create a Flash folder first, then create a MyFolder folder.
14.MKDIR Function Description: Create a new subdirectory (folder).
Reference example:
Mkdir ('c: / myfolder'); // creates a folder called MyFolder in the C-driven directory.
It should be noted that if you create a subdirectory in the directory that does not exist in itself, you will fail. For example, the C drive does not exist of the flash folder, written into MKDir ('c: / flash / myfolder'); this will not be successfully created in the MyFolder folder.
15.Reset features: Open the file in a read-only mode.
Reference example:
VAR
F: textfile; // Declare text file type variable
Begin
AssignFile (f, 'c: /myfile.txt'); // Establish a C: /MYFILE.TXT file with the F variable, and the file can be operated using the F variable. RESET (F); // Open file
END;
16.Rewrite Function Description: Open files with a write-writable. If the file does not exist, it will be automatically created. Using this function, you will overwrite all the contents in the file.
Reference example:
VAR
F: textfile; // Declare text file type variable
Begin
AssignFile (f, 'c: /myfile.txt'); // Establish a C: /MYFILE.TXT file with the F variable, and the file can be operated using the F variable.
REWRITE (F); // Open the file. If the file does not exist, you will create myFile.txt files in the C drive. If the file exists, you will override all the contents in MyFile.txt.
END;
17. Readln Function Description: Read a line of text.
Reference example:
VAR
String;
F: TextFile;
Begin
AssignFile (f, 'c: /myfile.txt'); // Establish a C: /MYFILE.TXT file with the F variable, and the file can be operated using the F variable.
RESET (F); // Open file
Readln (f, s); // Reads a line of text to the S variable
END;
18.Rename function description: Change the file name.
Reference example:
VAR
F: File;
Begin
AssignFile (f, 'c: /myfile.txt'); // Establish a C: /MYFILE.TXT file with the F variable, and the file can be operated using the F variable.
Rename (f, 'c: /newfile.txt'); // Renamed newfile.txt
END;
19.Writeln function description: Write a line of text.
Reference example:
VAR
String;
F: TextFile;
Begin
S: = 'this is a book.';
AssignFile (f, 'c: /myfile.txt'); // Establish a C: /MYFILE.TXT file with the F variable, and the file can be operated using the F variable.
Rewrite (f); // Create a new file
Writeln (f, s); // writes the contents of the S variable into the text.
END;
Fourth, memory, pointer operation functions are in programming, and dynamic arrays have brought great convenience for our handling data. The Windows API function also provides powerful guarantees to enhance the functionality. When we use these dynamic arrays and API functions, we often need to dynamically allocate memory space so that the dynamic array can be used by us, the API function can return the result. Therefore, these functions are essential.
1.allocmem Function Description: Allocated memory space and automatically initialize zero. If you don't need to initialize zero, you can use getMem instead of allocmem.
Reference example:
VAR
Buffer: pchar;
Begin
Buffer: = allocmem (256); // Allocation size of 256 bytes of memory space
END;
2.Dispose function Description: Release the memory space allocated by the pointer.
Reference example:
VAR
P: pinteger; // Declare an Integer pointer
Begin
NEW (P);
P ^: = 100;
Dispose (P) // Release memory
END;
3.Freemem function Description: Release the allocated memory space.
Reference example:
VAR
Buffer: pchar; begin
GetMem (buffer, 256); // allocates a memory space with a size of 256 bytes.
FreeMem (buffer); // Release memory space
END;
4.Getmem function Description: Allocate memory space.
Reference example:
VAR
Buffer: pchar;
Begin
GetMem (buffer, 256); // allocates a memory space with a size of 256 bytes.
END;
5.NEW Function Description: Allocated memory space for the pointer.
Reference example:
VAR
P: pinteger; // Declare an Integer pointer
Begin
NEW (P); // allocate memory
P ^: = 100; // assignment
END;
V. Mathematical Operation Functions We have a large use of mathematical operations when writing a close-related program with math. For example, image processing software will use these mathematical operation functions.
By default, Delphi has newly built projects, there is no most mathematical run function, so you need to add Math units in Usees.
1.ABS function description: Search absolute value.
Reference example:
VAR
R: SINGLE;
i: integer;
Begin
R: = ABS (-2.8); // R equal to 2.8
I: = ABS (-156); // I equal to 156
END;
2.EXP function Description: Exp Returns the value of the X power in E, where E is a natural logarithmic substrate.
Reference example:
E: = Exp (1.0); // e is real type variable
END;
3.Floor Function Description: A maximum integer that is less than equal to X is obtained.
Reference example:
FLOOR (-2.8) = -3
FLOOR (2.8) = 2
FLOOR (-1.0) = -1
4.int Function Description: Returns the integer part in the parameter.
Reference example:
VAR
R: REAL;
Begin
R: = int (123.456); // r equal to 123.0
R: = int (-123.456); // r equal to -123.0
END;
5.max (the unit: Math) function Description: Compare two numbers and return the maximum number.
Reference example:
VAR
K: integer;
Begin
K: = max (10, 20); // K will be 20
END;
6.min (the unit: math) function Description: Compare two numbers and return a minimum number.
Reference example:
VAR
K: integer;
Begin
K: = min (10, 20); // K will be 10
END;
7.PI function description: Accurate calculation Return round week rate.
Reference example:
VAR
x extended;
Begin
x: = pi; // x equal to 3.1415926535897932385
END;
8.Round function description: Since a real number, it is rounded.
Reference example:
VAR
I, J: Integer;
Begin
i: = round (1.25); // i is equal to 1
J: = round (1.62); // j is equal to 2
END;
9.SQR function description: The square of the value is given.
Reference example:
VAR
i: integer;
Begin
I: = SQR (3); // i is equal to 9
END;
Sixth, date function is handled on the date, generally used in business software, and commercial software is often used. If you intend to write a single-restricted software, you can implement it with the following functions. 1.Date function Description: Returns the current date.
Reference example:
Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);
Begin
Label1.caption: = 'Today is:' DateTostr (date);
END;
Label is shown as: Today is January 1, 2005.
2.DateTostr function description: Convert the date type to a character pattern.
Reference example:
VAR
String;
Begin
S: = DATETOSTR (DATE);
END;
3.Datetimetostr function Description: Convert the DateTime type to a character pattern.
Reference example:
VAR
String;
Begin
S: = DateTimetostr (now);
END;
4. Dayofthemonth Function Description: Get the date of the specified date.
Reference example:
Label1.caption: = INTOSTR (Dayofthemonth (now);
Suppose the current date is January 2, 2005, then Label will appear to 2.
5. Dayoftheweek (Sports: Dateutils) Function Description: Get the day of the day according to the specified date.
Reference example:
Label1.caption: = INTOSTR (Dayofthemonth (now);
Suppose the current date is January 2, 2005, then Label will appear as 7. Determine based on the returned value is a week. 7 means Sunday, 1 is Monday, and it is pushed according to category.
6. Dayoftheyear (the unit: Dateutils) function Description: After obtaining the number of days according to the specified date.
Reference example:
Label1.caption: = INTTOSTR (Dayoftheyear (now);
Suppose the current date is January 2, 2005, then Label will appear to 2. It is said that it was the second day of 2005.
7. Dayof (Sport: Dateutils) Function Description: Return to the day according to the specified date.
Reference example:
Label1.caption: = INTOSTR (dayof (date));
Suppose the current date is January 2, 2005, then Label will appear to 2.
8.IsLeapyear Function Description: Determine whether it is a leap year according to the designated year. You can use the YearOf function to get the year.
Reference example:
Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);
Begin
if isleapyear (Yearof (Date) Then ShowMessage ('is a leap year')
Else ShowMessage ('is not a leap year');
END;
9.Monthof (Sport: Dateutils) Function Description: Returns the month according to the specified date.
Reference example:
Label1.caption: = INTTOSTR (Monthof (Date));
Assuming the current date is January 2, 2005, then Label will appear as 1.
10.NOW Function Description: Returns the current date and time.
Reference example:
Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT); Begin
Label1.caption: = 'is now:' datetimetostr (now);
END;
11.yearof (Sport: Dateutils) Function Description: Returns the year according to the specified date.
Reference example:
Label1.caption: = INTOSTR (Yearof (Date));
Suppose the current date is January 2, 2005, then Label will be displayed as 2005.