Hello ! We meet again ! Get ready for this short one ! In real world, we meet many constants, such as pi = 3.1415926513... or e = 2.7182818284529... Many of these are impractical number so that it is quite hard for us to remember, at least for some of us. Pascal has a good facility to use the constants. Even it has predefined ones, like pi.
How to define our own constant ? Easy, just like a variable definition, we just give const above our constants AND start tayloring our needs.
const : myconst = 1234; : : (Like variable definition)
We just omit the constant type, Pascal will understand it. We can use it just like a normal variable. Example :
const myconst = 1234; var i : word; begin i:=40; writeln(i*myconst); end.
Easy, no ? But heed my word, constants is unchangeable. If you try to change it, Pascal will say error. Wait a minute ! I heard that Pascal's constants can be changed somewhere in the program. You are absolutely right ! Now, the declaration becomes :
const myconst : mytype = thevalue;
example:
const abc : word = 1234;
You may not declare constants with values exceeding the type, like these :
const aaa : word = 100000; { Illegal because word is integer ranging 0-65535 } bbb : byte = 123.44; { Illegal because of the fractional part } ccc : string = 123; { Illegal because incompatible type }
Clear ? How can we modify them inside our program ?
const myconst : word = 1234; var i : word; begin i:=40; myconst:=1000; writeln(i*myconst); end.
Easy, just like a normal variable, doesn't it ? That's all for this chapter !
Where to go ?
Back to main page
Back to Pascal Tutorial Lesson 1 contents
To the quiz
Back to Chapter 3 about branching(if, case..of)
To Chapter 5 about looping in Pascal(for, while..do, repeat..until)
My page of programming link
Contact me here
By : Roby Joehanes, © December 1996