Delphi Programming/Variables and constants

From Wikibooks, open books for an open world
Jump to: navigation, search

Variables are indispensable in programming. A program wouldn't do much things without variable.

A variable links a name to a value. You must not confuse its name and its value. A variable is not constant. It may change during the application execution.

Variables and program[edit]

Variable declaration in the program[edit]

To declare a variable in a program, you have to write:

  • The variable name (var1, for example)
  •  :
  • Its type (integer)
  •  ;

An example:

  1. function foo()
    
  2. var
    
  3.     var1: integer;
    
  4.     var2: integer;
    
  5.  
    
  6. begin
    
  7. // Some instructions
    
  8. end;
    

You can also write:

  1. function foo()
    
  2. var
    
  3.     var1, var2: integer;
    
  4.  
    
  5. begin
    
  6. // Some instructions
    
  7. end;
    

Right syntax for the variable names[edit]

Rules Wrong identifiers Right identifiers
Must not start with a number 1name name1
Dots are not allowed name.2 name_2
Dashes are not allowed -name-3 _name_3
Spaces are not allowed Variable name Variable_name
Accented characters are not allowed déjà_vu deja_vu

You don't have to worry about lowercase and uppercase as Delphi is case-insensitive.

Display a variable[edit]

It's easy to display a variable in an application. You must call the

WriteLn();

function. Once done, to properly display an integer, you must call the

 WriteLn(variableToDisplay);

function. Here is the result in a whole application:

  1. program Display_a_variable;
    
  2.  
    
  3. {$APPTYPE CONSOLE}
    
  4.  
    
  5. uses
    
  6.   SysUtils;
    
  7. var
    
  8.   var1:integer;
    
  9.  
    
  10. begin
    
  11.   var1:= 12
    
  12.   WriteLn (var1);
    
  13.   ReadLn;
    
  14. end.
    

So this code will display 12.

Remark: If you don't want the display of a new line, use the Write function rather than WriteLn .
Remark: You can use the ReadLn function to avoid the console from closing too quickly, but the actual feature of this function is described below.

Retrieve a variable[edit]

It's easy too. You have to call the ReadLn(variable); function.

You have to first declare the variable you want to use. Here is a whole code:

  1. program Retrieve_a_Variable;
    
  2.  
    
  3. {$APPTYPE CONSOLE}
    
  4.  
    
  5. uses
    
  6.   SysUtils;
    
  7. var
    
  8.   var1:integer;
    
  9.  
    
  10. begin
    
  11.   ReadLn (var1);
    
  12. end.
    

In the next pages, we will see how to operate variable additions, use it in loops and conditions, etc...

Remark: If you don't want to skip a line after the entry, use the Read function rather than ReadLn .

Assignment[edit]

You can set a value to a variable at any time in a program, from another variable for example:

  1. program Assignment;
    
  2.  
    
  3. {$APPTYPE CONSOLE}
    
  4.  
    
  5. uses
    
  6.   SysUtils;
    
  7. var
    
  8.   sourceVariable:integer;
    
  9.   targetVariable:integer;
    
  10.  
    
  11. begin
    
  12.   ReadLn (sourceVariable);
    
  13.   targetVariable := sourceVariable;
    
  14. end.
    

The changed variable is on the left and the variable whose value is duplicated is on the right. Do not confuse.

The constants[edit]

Introduction[edit]

The constants are similar to variables, except one point: they can't change their value during the execution.

The constants of the system[edit]

Those constants specify all the values that are native and defined in the header files.

Example:

stdout points on the screen buffer
stdin points on the keyboard buffer

The symbolic constants[edit]

The symbolic constants are defined by the developer. They work as the variables, except for their declaration.

To declare a constant, you have to declare it after the reserved keyword const instead of var.

  1. program Declare_constant;
    
  2.  
    
  3. {$APPTYPE CONSOLE}
    
  4.  
    
  5. uses
    
  6.   SysUtils;
    
  7. const
    
  8.   const1 = 12;
    
  9. var
    
  10.   var1:integer;
    
  11.  
    
  12. begin
    
  13.   // Instructions
    
  14. end.
    


Test your knowledge

Write an application that asks the user its age and then display it.

Answer
  1. program Ask_your_age;
    
  2.  
    
  3. {$APPTYPE CONSOLE}
    
  4.  
    
  5. uses
    
  6.   SysUtils;
    
  7. var
    
  8.   age:integer;
    
  9.  
    
  10. begin
    
  11.   WriteLn ('How old are you?');
    
  12.   ReadLn (age);
    
  13.   Write ('You are ');
    
  14.   Write (age);
    
  15.   WriteLn (' year(s) old.');
    
  16. end.