Skip to content

Delphi Fusion

Narrow screen resolution Wide screen resolution Increase font size Decrease font size Default font size    Default color brown color green color red color blue color
  • Regular Delphi programming articles and tutorials
  • Free members forum supporting everything Delphi
  • Support for all programming abilities
You are here: Home arrow Articles arrow Delphi arrow Part 3: Constants and Variables
Skip to content

User Menu

Login
Register
Logout

Latest Threads

Delphi and C++Builder Roadmap
by: Jon!
@: 03/09/08 11:53 am
Delphi 7 Enterprise!
by: uuf6429
@: 17/06/08 01:33 pm
Tracing file/registry activity
by: uuf6429
@: 17/06/08 01:25 pm

Delphi Fusion

Links
Sponsors
Amazon

Donate

Enter Amount:


Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285
Part 3: Constants and Variables Print E-mail
Written by Blueaura   

Learning Outcomes

This tutorial will explore the use of constants and variables in programming. By the end of this tutorial you will:

  • Understand what constants and variables are and why they are used
  • Be able to declare and use constants and variables in a program
  • Be able to write your own simple programs with user input and output

Keywords

  • Constant
    A named storage location in a computer’s memory that can contain data which cannot be modified during run-time.
  •  Variable
    A named storage location in a computer’s memory that can contain data which can be modified during run-time.

Introduction

Constants and variables are used to allow us to use the computers memory when programming. This is especially useful when you need to store data which is constantly changing and also when you need to recall the same data multiple times.

The easiest way to translate this is to think back to high school math. To calculate the area of a circle you multiply the square of the circles radius by pi to give, A = πr2. From looking at this equation you can apply it to any circle, all you do is substitute r with the measurement of the circles radius. After working this out you get your solution represented by A. Looking at this more closely A and r can represent any number making them both variables, however π can only ever be the number known as pi which makes it a constant. Now let’s put this into action.

Fire up Delphi and create a new console application just like you did during the Hello World tutorial. To stop the console window from disappearing don’t forget to add ReadLn so your code looks like this:

Basic console application code
  1. program Tutorial3;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. SysUtils;
  7.  
  8. begin
  9. ReadLn;
  10. end.
 

Declaring Constants and Variables

First we need to create the constant pi, and then assign the value of pi to it (or an approximation). To do this just above the begin statement we need to tell the compiler we are going to declare a constant by typing const  and then bellow that we can name the constant and give it a value. For now four significant figure accuracy is fine, I don’t think many readers will be using this program for scientific or engineering applications.

Declare Pi
  1. program Tutorial3;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. SysUtils;
  7.  
  8. const
  9. Pi = 3.142;
  10.  
  11. begin
  12. ReadLn;
  13. end.
 

Declaring variables is a similar process although we need to tell the compiler what type of data the variable will be storing so it can allocate a sufficient amount of memory. To do this below the declaration of Pi create a var section and add variables Area and Radius which will store a Double data type. Don’t worry what a double is at the moment, it will be discussed later.

Declare Area and Radius
  1. program Tutorial3;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. SysUtils;
  7.  
  8. const
  9. Pi = 3.142;
  10.  
  11. var
  12. Area: Double;
  13. Radius: Double;
  14.  
  15. begin
  16. ReadLn;
  17. end.
 
So that’s how we declare our constants and variables. Constants go in the constant section denoted by the keyword const, they are declared by stating the name of the constant, a colon, and the value of the constant. Variables go in the variable section denoted by the keyword var, they are declared by stating the variables name, a colon, and state the data type the variable will hold. Don’t forget, because declaring constants and variables are statements each declaration must finish with a semicolon.

Using Variables and Constants

A variable is a named block of memory which to being with can literally contain anything. This is because when a program terminates and vacates the memory it was previously using it doesn’t clear its contents to save time. It is very important that when you have declared a variable you always assign a value to it before you use it in any calculations. To assign a starting value to Area and Radius we use the assignment operator which looks like ‘Variable := NewValue;’. This statement assigns the right side to the left side. To clear the contents of variables a good starting place is to set them to zero, it is good to get into a habit of doing this once you have declared a variable – it prevents bugs and will make your life a lot easier.

Clear Variable Contents
  1. Area := 0;
  2. Radius := 0;
 
Now to do the calculation to work out the area of the circle we need to do a little math. All we do is assign the calculation Pi * (Radius * Radius) to Area. Then we can use WriteLn to display Area. Because Area is a floating point (decimal) number though we need to convert this to a string to be displayed in the console using FloatToStr.
Calculate Circle Area
  1. Area := Pi * (Radius * Radius);
 
If you run the program at this point not a lot will happen, you will be given the value of area as zero and that is all because 3.142 * ( 0 * 0 ) = 0. We can allow the user to assign values to variables though by reading the input from the console using ReadLn. When using ReadLn we also need to declare a new variable of a string data type to store the users input from the console, this can then be converted from a string into a floating point number so we can work out the circles area. Complex but because different data types have different sizes in the computer’s memory this is the way it has to be.

And now you have your working program, go ahead try it out.

Finished Code
  1. program Tutorial3;
  2. {$APPTYPE CONSOLE}
  3. uses
  4. SysUtils;
  5. const
  6. Pi = 3.142;
  7. var
  8. Area: Double;
  9. Radius: Double;
  10. begin
  11. Area := 0;
  12. Radius := 0;
  13. Area := Pi * (Radius * Radius);
  14. WriteLn('Area = ' + FloatToStr(Area));
  15. ReadLn;
  16. end.
 

 

Data Types

Fantastic, you can now declare constants and variables and hopefully appreciate why they are used in programming. However, another important aspect is data types which have only briefly been covered. Data types are there to tell the computer how much memory to set aside for each variable and how to interpret the data. Without declaring data types the computer would need to set aside an unnecessarily large amount of memory for each variable just in case you wanted to assign a large data type to it, not only that how would it know what data is text and what is numbers.

Here are a few common data types you will frequently use:

Name

Values

Memory Size

Integer Numbers

Byte

0 .. 255

1 Byte

Cardinal

0 .. 4294967295

4 Bytes

Integer

-2147483648 .. 2147483647

4 Bytes

Int64

-9223372036854775808 .. 9223372036854775807

8 Bytes

Floating Point

Single

1.18 x 10-38 to 3.4 x 1038

4 Bytes

Double

2.23 x 10-308 to 1.79 x 10308

8 Bytes

Extended

3.37 x 10-4932 to 1.18 x 104932

10 Bytes

Strings and Characters

Char

1 Character

~1 Byte

String

~231 characters

4 Bytes – 2GB

Logical

Boolean

True or False

1 Bit

 

The size of some of these can depend on your system, especially if you are using FreePascal on a computer without x86 architecture, though it is safe to say they are correct for most. There are of course a lot more data types and as you will see later you can even create your own but for now you will mostly use Integer, Double, String, and Boolean.

You can also convert between data types using these functions for their respective data types:

Function

Description

IntToStr

Converts an integer to a string

StrToInt

Converts a string to an integer

StrToFloat

Converts a string to a floating point

FloatToStr

Converts a floating point to a string

 

Integers can be directly assigned to floating point numbers however the reverse requires the floating point to be rounded or have the integer part separated.

Using everything you have learnt so far why not try and make your own program such as a simple calculator, or if you are confident with using constants and strings move on to the next tutorial.

There are no comments for this item.
Please keep your comments brief and on topic, and remember that this is not a discussion thread.
Name : E-mail :
Title : Website :
Comment(s) :
J! Reactions Commenting Software
General Site License
Copyright © 2006 S. A. DeCaro
 
< Prev