Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Delphi Programming/Variables and constants

From Wikibooks, open books for an open world
<Delphi Programming

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

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 |edit source]

Variable declaration in the program

[edit |edit source]

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

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

An example:

functionfoo()varvar1:integer;var2:integer;begin// Some instructionsend;

You can also write:

functionfoo()varvar1,var2:integer;begin// Some instructionsend;

Right syntax for the variable names

[edit |edit source]
Wrong identifierViolated ruleRight identifier
1nameMust not start with a numbername1
name.2Dots are not allowedname_2
-name-3Dashes are not allowed_name_3
Variable nameSpaces are not allowedVariable_name
déjà_vuAccented characters are not alloweddeja_vu

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

Display a variable

[edit |edit source]

It's easy to display a variable in an application. In aconsole application, you use the command

WriteLn(variableToDisplay);

.

Here is the result in a whole application:

programDisplay_a_variable;{$APPTYPE CONSOLE}usesSysUtils;varvar1:integer;beginvar1:=12WriteLn(var1);ReadLn;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.
Remark: In GUI applications, you display variables invisual components.

Retrieve a variable

[edit |edit source]

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:

programRetrieve_a_Variable;{$APPTYPE CONSOLE}usesSysUtils;varvar1:integer;beginReadLn(var1);end.

In the next pages, we will see how to operate variable additions, use variables 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 |edit source]

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

programAssignment;{$APPTYPE CONSOLE}usesSysUtils;varsourceVariable:integer;targetVariable:integer;beginReadLn(sourceVariable);targetVariable:=sourceVariable;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 |edit source]

Introduction

[edit |edit source]

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

The constants of the system

[edit |edit source]

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 |edit source]

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 keywordconst instead ofvar.

programDeclare_constant;{$APPTYPE CONSOLE}usesSysUtils;constconst1=12;varvar1:integer;begin// Instructionsend.


Test your knowledge

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

Answer
programAsk_your_age;{$APPTYPE CONSOLE}usesSysUtils;varage:integer;beginWriteLn('How old are you?');ReadLn(age);Write('You are ');Write(age);WriteLn(' year(s) old.');end.
Retrieved from "https://en.wikibooks.org/w/index.php?title=Delphi_Programming/Variables_and_constants&oldid=3703186"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp