|
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:
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.
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.
Debt is $100. Money is $99. Interest of 2% issued. Debt increased by $2. Debt is $102.
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! 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.
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.
The sum of a, b, and c is 9.
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:
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é! |
|||||||||||||||||||||||||||||||||||||