Delphi Programming: Strings, Parameters, and Functions

Classified in Computers

Written on in English with a size of 2.37 KB

1. Understanding Short and Long Strings

Short strings have a fixed length, not exceeding 245 characters. They are declared as follows:

  • VAR S1: ShortString;
  • VAR S2: STRING[25];

Long strings are allocated dynamically and are limited only by available memory. They are declared as:

  • VAR S1: STRING;

2. What Are Parameters?

A parameter is a value passed to a function or procedure to be processed.

3. Parameter Passing: Value vs. Reference

Passing by Value: A copy of the original variable is created; the original value remains unchanged.

Passing by Reference: No copy is created; the procedure works directly with the original variable.

  • Value: PROCEDURE REVIEW(NAME: STRING);
  • Reference: PROCEDURE REVIEW(VAR NAME: STRING);

4. Functions vs. Procedures

  • Function: A code section that performs an operation and returns a value.
  • Procedure: A code section that performs an operation but does not return a value.

5. How Do Functions Return Values?

Functions return values by assigning the result to a local variable named Result or by assigning it to the function name itself.

6. Purpose of INC and DEC Functions

  • INC: Increments a numeric variable. Example: INC(X);
  • DEC: Decrements a numeric variable. Example: DEC(X);

7. Checking for Empty Components

To check if a Label1 component is empty (ignoring spaces):

IF Length(Trim(Label1.Caption)) = 0 THEN ShowMessage('Empty');

8. Common String and Conversion Commands

  • a. Transform 'address' to uppercase: address := UPPERCASE(address);
  • b. Transform 'X' to lowercase: X := LOWERCASE(X);
  • c. Convert REAL variable to STRING: NAME := FLOATTOSTR(NUM);
  • d. Convert STRING to INTEGER: NUM := STRTOINT(EDIT5.TEXT);
  • e. Extract characters using COPY: xyz := COPY(abc, 5, 3);

Related entries: