Procedures |
Functions |
The Forward Declaration |
Sometimes the need arises to reuse a part of the code. It is no good programming to copy this part again into the code and use it this way. Instead, PASCAL has components, that is subprograms called procedures and functions for this purpose. These two components are not only used for this purpose, but also to make the program easier to debug.
There are two advantages of using subprograms:
Think of procedures as small programs, actually they can only be part of a program. They have a set of parameters, value or variable. What are value and variable parameters? Value parameters are those which are only passed to the procedure, because otherwise the procedure would not be able to do the task it is said to. Value parameters do not change after the execution of procedures. They are given in the parentheses which comes after the procedure declaration. Variable parameters are those which may affect the execution of the procedure and need to be updated according to this procedure or may only carry out the result of the procedure. The are also given in the parentheses after the procedure declaration, however they have a 'var' keyword before them to indicate that they are variable parameters.
To give a short outline:
program firstprocedure(input,output); |
var |
weight:real; |
{1.} procedure writeprecise(num:real); |
{2.} begin |
{3.} writeln(num:9:3); |
{4.} end; |
begin |
writeln('Please give in your precise weight'); |
readln(weight); |
writeprecise(weight); |
end. |
Lets look at each line of the procedure in detail. The first line is the procedure declaration, there you specify the name of the procedure which you use to call the procedure later on, and the list of parameter variables, variable or value, whatever nature they have. Here, the only parameter is num, which is a real variable and also a value parameter, that is its value is supplied to the procedure but it is not passed out of the procedure afterwards, because the changes to the variable, if ever made, are not significant for the program execution. The name you choose for the variable is not important for the main part of the program, because it does not use this variable at all, neither is it important if the variable has been declared in the variable part. This variable, although it may be possible that you have a variable with the same name in the main program and variable declaration part, is a completely new one and does not affect the value of the variable with the same name. On the 3. line of the procedure, it is specified what the variable is used for.
During the call of the procedure from the main program, you supply the same number of variables as are in the parameter list. There is a one-to-one mapping between the variables in the parameter list and the variables used during a procedure call. The first variable in the parameter list and in the list of the procedure call should have the same type, the second with the second.... Here, in the procedure call, you give weight as the parameter to the procedure. Now, as it comes to the procedure, the name weight is not used any more. Now, num takes over the value of weight, and any changes done to num do not reflect to weight. If weight were a variable parameter, then only after the procedure execution, the modified value of num would be passed to weight. Try to write this program and then try to debug it. How you can do this is explained in the section How to debug?
Lets see an example with a variable parameter. Suppose we want to calculate
the ideal weight of a person, which is the length of the person in cm's
-110.
program idealweigth(input,output); |
var |
ideal, length:real; |
procedure calculateideal( var input1 : real; input2 : real); |
begin {procedure} |
input1:=input2-110; |
end; |
begin {main} |
writeln('Enter your length in cm''s please.'); {Note that we have two ' ' after cm in order to be able to output a ' !} |
readln(length); |
calculateideal(ideal,length); |
write('This is your ideal weight >',ideal:5:2); |
end. |
Here, the procedure has two parameters, a variable and a value. input1 is the variable parameter and input2 is the value parameter. In the procedure call, the value of ideal is passed to input1, although it does not have any significant initial value, and the value of length is passed to input2, that is the input from the user. In the procedure, input1 gets the value input1-110. Then, after the procedure has finished execution, the new value of input1 is passed to ideal but not the value of input2. Now, ideal stores the calculated value and can be output with precision 2.
Functions are similar to procedures, except that they return one value with a specified type and all its parameters should be input parameters, that is value parameters. That is only one value should be able to be changed.
Lets implement the previous example with a function:
program idealweight(input,output); |
var |
ideal,length : real; |
function calculateideal(input1:real) : real; |
begin {function} |
calculateideal := input1 - 110; |
end; |
begin {main} |
write('Please enter your length in cm''s '); |
readln(length); |
ideal:=calculateideal(length); |
writeln('Your ideal weigth is ', ideal :5:2); |
end. |
Suppose you have to use a procedure call before you have the procedure declaration due to any reason. For this problem we have the forward declaration.
For this, you give the procedure name and the list of parameters and declare that it is forward. In the second declaration, you don't have to give the parameter list anymore.
Example:
procedure first(input1:real); Forward; |
procedure second(input2:integer); |
begin |
....... |
end; |
procedure first; |
begin |
.... |
end; |