|
|
| Part 3: Constants and Variables |
|
|
| Written by Blueaura | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Learning OutcomesThis tutorial will explore the use of constants and variables in programming. By the end of this tutorial you will:
Keywords
IntroductionConstants 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:
Declaring Constants and VariablesFirst 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.
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.
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.
And now you have your working program, go ahead try it out.
Data TypesFantastic, 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:
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:
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. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| < Prev |
|---|