Home
Introduction
Courses
Glossary
About

Beginner - Implementation In Pascal

This is the course that rounds up the revision of concepts in the previous courses. It shows you how to implement "Variables And Constants", "Control Flow Structures", and "Procedures and Functions" in Pascal.

How Variables and Constants are implemented in Pascal

We'll kick off by showing you how to implement Variables and Constants. Each of them has its own implementation, though, but the style is pretty much the same. First off, implementing variables...

    Implementing Variables

    Variables in Pascal are declared using the 'var' keyword. You need to declare a variable before you can use it. Why? Well, it's like if you were making the box for the value to be stored. Then, after the box is finished, you can start placing the value into it. You can't use a box if you don't make one! These are how declarations look like:

      var
        Number, Value: integer;
        Text: string;
        TrueOrFalse: boolean;
    
    The syntax for a declaration is to put the name of the variable, a colon, the data type of the variable, and finally, the grand semicolon. You can also declare more than one variable in one line by separating them with commas. Now that reminds me, we still haven't told you the different data types, haven't we? Well, here they are, shamelessly lifted from the Turbo Pascal help:

    Type Range Precision
    Shortint -128..127 Whole numbers only
    Integer -32768..32767 Whole numbers only
    Longint -2147483648..2147483647 Whole numbers only
    Byte 0..255 Whole numbers only
    Word 0..65535 Whole numbers only
    Boolean True or False Whole numbers only
    Real 2.9e-39..1.7e38 11-12
    Single 1.5e-45..3.4e38 7-8
    Double 5.0e-324..1.7e308 15-16
    Char a character Characters only
    String 255 delightful characters! Text only

    Of course, those are not all the data types you can have in Pascal. In fact, you can even make your own data types, so there really isn't any limit! Variables can be any size you want them to be.

    Implementing Constants

    In Pascal, constants are listed under the 'const' keyword, just like the look-up table concept explained above. A typical list of constants would look like this:

      const
        Pi = 3.14;
        Gravity = 9.8;
        Golden = 1.6;
    
    Because these numbers are meaningless by their own, we give them more meaningful names to make our code more readable. Imagine reading an entire maths textbook which refers to pi by its value! Unthinkable!

Below is an example program listing to demonstrate the above. Open a new code window to type in the code. You know how to run them, right? Just press Ctrl-F9.

Type

1:  program VariableMania;
2:
3:    const
4:      Interest = 2;
5:
6:    var
7:      Money, Debt: integer;
8:
9:    begin
10:     Debt := 100;
11:     Writeln('Debt is $', Debt, '.');
12:
13:     Money := 99;
14:     Writeln('Money is $', Money, '.');
15:
16:     Writeln('Interest of ', Interest, '% issued.');
17:     Debt := Debt + (Debt div 100 * Interest);
18:     Writeln('Debt increased by $', (Debt div 100 * Interest), '.');
19:     Writeln('Debt is $', Debt, '.');
20:   end.

Output

Debt is $100.
Money is $99.
Interest of 2% issued.
Debt increased by $2.
Debt is $102.

Analyse

  • Line 4
    This is a declaration for the constant 'Interest'. Now, whenever the compiler sees 'Interest', it'll return this number instead.
  • Line 7
    Here are the declarations of variables 'Money' and 'Debt'. Both are integers, which only allow them to store whole numbers, but as you'll see later, this arrangement makes the program much easier to read.
  • Line 10
    This is where the program really starts. See the := sign? Whenever that sign is used, it means that the value to the left of the sign will now store the value to the right of the sign. So in this case, 'Debt' now stores the value 100.
  • Line 11
    This line prints out a message that changes with the value of 'Debt'. If you inspect the line closer, you'll see that after the first string, there is a comma, the variable name, and another comma, followed by the next string. When this line gets printed, The value of the variable is printed onto the screen.
  • Line 13 & 14
    These lines do exactly the same thing as lines 13 and 14, except that they print out 'Money' instead of 'Debt'.
  • Line 16
    This line uses a constant to print out the sentence. The compiler replaces the word 'Interest' with the number 2. Once again, this uses the Writeln command to output the sentence.
  • Line 17
    Here, a calculation is carried out at the right of the :=, and the value is stored into 'Debt'. What it does into to take the current value of 'Debt', add it to Debt divided by 100 times 2, and place the new value into 'Debt'. In this calculation, division is replaced by 'div'.
  • Line 18 & 19
    These lines output the amount of interest and the new value for 'Debt'.

If this is your first program, you'll be surprised that the output simply flies past you when you run the program. Before you know it, you'll be back at the IDE. Well, to get your output, all you have to do is to press Alt-F5 to view the User Screen. Voila!

    Tip

    By the way, the reason why div was used instead of / is because the two dividends were integers. the '/' sign is only used to return real numbers.

How procedures and functions are implemented

How are procedures and functions implemented? Well, here's the low-down on the syntax for declaring your very own procedures and functions.

Procedures are declared using the procedure keyword, as you can see below. The name of the procedure comes first, followed by a semicolon, then the contents of the procedure.

  procedure WhyMe;

    var
      i: integer;

    begin
      for i := 1 to 10
        Writeln('Why Me?!');
    end;
A procedure is just like a separate program in itself. You can declare variables and use them within your program code. However, as described above, after the procedure ends, the variables vapourize with it. So, now that you have declared a procedure. How do you call it? Ahh, this is easy. Just type in the name of the procedure. What else could be easier?
  begin
    WhyMe;
    Writeln('Those were my true feelings regarding Exams');
  end.
When this program executes, it will print 10 "Why Me?!"s and print the final message "Those were my true feelings regarding Exams". After a procedure finishes, it returns back to place where it was called. A procedures always ends where it started.

Functions, on the other hand, cannot be used just like that because they return a value back to the program instead of just doing things. For instance, we may have a function which acts like this:

  function AddEmUp : integer;
    begin
      AddEmUp := a + b + c;
    end;
This function will return the sum of a, b, and c. Well, how does it work? The function name comes after the keyword 'function'. After the function name, you get the data type the function should return. Huh? Yes, even when using functions, you can't run away from the nearly limitless list of data types. The value the function should return is assigned to the name of the function. In this case, because the name of the function is 'AddEmUp', we assign the result of the function to the same name! How do you use functions then? Well...
  begin
    a := 5;
    b := 9;
    c := 4;
    Writeln(AddEmUp);
  end.
This program will print out the sum of a, b, and c! Marvellous use of functions, isn't it? Well, it isn't finished yet!

Procedures and functions would have been dreadfully boring and unflexible if it wasn't for a great feature called parameters. You can use parameters to pass values to the procedure or function to use. For example:

  function AddEmUp2( x, y, z: integer ) : integer;
    begin
      AddEmUp2 := x + y + z;
    end;
Then, to use this function, you must provide values for x, y, and z. This is done by using brackets.
  begin
    Writeln(AddEmUp2(2, 3, 5));
  end.
In this example, x would be 2, y would be 3, and z would be 5. You're passing values over for the procedure or function to make use of. Got the idea? No? Well, here's another example.
  procedure PrintEm( x, y, z: integer );
    begin
      Writeln(x);
      Writeln(y);
      Writeln(z);
    end;
If you pass the values 2, 3, and 5, this procedure would print out those numbers. That's great! Now you can provide information to the program on the fly. Power on the go...

The final example program...

Here it is, in its full glory, another example program! Buckle up, and enjoy typing this utterly simple yet powerful program.

Type

1:  program AddEmUpAgain;
2:
3:    function AddEmUp( a, b, c: integer ) : integer;
4:      begin
5:        AddEmUp := a + b + c;
6:      end;
7:
8:    procedure PrintIt( a, b, c: integer );
9:      begin
10:       Writeln('The sum of a, b, and c is ', AddEmUp(a, b, c), '.');
11:     end;
12:
13:   begin
14:     PrintIt(2, 3, 4);
15:   end.

Run

The sum of a, b, and c is 9.

Analyse

  • Line 3
    This declares the function AddEmUp, which takes three parameters and adds them up.
  • Line 5
    The only line of AddEmUp, it justs returns the sum of parameters a, b, and c.
  • Line 8
    Declares procedure PrintIt, which also accepts three parameters.
  • Line 10
    This line simply prints out the correct message by calling AddEmUp and passing it the three parameters.
  • Line 14
    This single line starts the whole ball rolling!

Well, that was another example program! But this is far from over. Keep in mind these tips and rules when creating procedures and functions in Pascal:

  1. You must declare a function or a procedure before it is used. For example, in the example program above, you can't just put the procedure on top of the function because PrintIt needs to use AddEmUp, it needs to be declared first.
  2. However, if the world somehow gives you a reason to use a procedure before you declare it, you can use the forward keyword. Find out from your nearest F1 key!
  3. What happens if you provide more parameters than a procedure or function needs? Well, the compiler simply gives you an error.
  4. No, you can't re-declare constants in a new procedure - Why would you anyway?
  5. Procedures and functions are a very important part of structured programming. It helps to make code modular, so use them often.
There's a whole lot more, but describing those details here would be repeating what's already well described in the online help. To learn more, go to the help index and look for the keywords 'procedure' and 'function'.

The Final Chapter is coming

Joy! Rapture! Divinity! (Get the joke?) Day 5 is finally over and you can go on to Day 6 - Comments and Other Neat Stuff. Neat Stuff? What neat stuff?! Well, find out for yourself. Avé!

PreviousNextTop