{ «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«» «» «» «» TUTORIAL FIFTEEN «» «» «» «» by «» «» «» «» Anthony Peck «» «» «» «» «» «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»   43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 Have you anything to declare? Calling procedures can be done from anywhere in the program, with one limitation. If you call a procedure that has yet to be defined, you must make a forward declaration. Some programmers forward declare all of their procedures anyway, and that way they can swap them around without worrying which procedure is called when. We forward declare procedures using the directive "forward". Eh? Let's do a program in which all the procedures have been forward declared, and thus can be called from anywhere in the program... } PROGRAM MDTute15; { Program works out pay given the hours and the rate Author : A N Peck Date : 1 November 1994 Procedures used: Title - displays program information Getnumber - returns user entry Calculate - finds the total payment due } Procedure Calculate; forward; { ^ | |__ Here we declare a procedure called "calculate" and then give the directive forward. This is like Nostradamus predicting the winner of the 1995 Australian Checkers Spectacular. The compiler will now recognise the procedure when it is called. } Procedure Getnumber (whichone: string; Var userchoice: real);forward; { ^ | |__ This procedure accepts parameters. These are outlined now, and do not have to be defined again when the procedure is given in full. } Procedure Title; forward; { ---------------------------------------------------------- } Procedure Calculate; { Calls "Getnumber" procedure twice and multiplies the inputs } Var Rate,Hours: real; begin Getnumber('hours worked',Hours); { ^ | |__ Now here's an interesting thing! "Getnumber" hasn't been defined yet! It's OK however, because we have given the compiler some indication that it exists by using the forward declaration. The other weird thing here is that "Getnumber" is passed an undefined string ('hours worked'). In other words, unless a parameter is passed as a variable to be changed by a procedure, any parameter will do as long as it is of the same variable type. In this case, "Getnumber" is expecting a string. Whether or not that string is passed directly (as is the case here), or defined first as a variable and then passed... Var alternative: string[20]; begin alternative := 'hours worked'; Getnumber(alternative,hours); end ...is unimportant! This means that any parameter just passed to a procedure, which will not be altered by that procedure, can be sent without defining it first as a variable. I think I'll go and have a lie down. For example, let's say that a procedure takes two numbers... Procedure elk (first: real; second: integer); ...we can send it numbers like this... number1 := 2.45; number2 := 8; elk(number1,number2); ...or like this... elk(2.45,8); ...which is so beautiful I think it calls for a celebratory leg waxing. } Getnumber('rate of pay',Rate); { ^ | |__ Here we call "Getnumber" with a different string. You'll see why when you read the definition for the procedure below. } writeln(' The amount due is $',Rate*Hours:0:2); writeln; end; { Calculate } { ---------------------------------------------------------- } Procedure Getnumber; { ^ | |__ We've already outlined all the parameter passing information in our "forward" declaration. We could write it all out again here, but what's the point? (It'll teach you some humility and be great for outdoor barbeques) } { Prompts user for a number } begin write(' Please enter the ',whichone,': '); { ^ | |__ Ah! The point of sending a string becomes apparent. Now "Getnumber" is tailored for whatever information you need! "Whichone" defines the required data... } readln(userchoice); writeln; end; { Getnumber } { ---------------------------------------------------------- } Procedure Title; { Prints up a nice title on the screen } Const tab = ' '; begin writeln; writeln(tab,'PAY CALCULATOR'); writeln(tab,'--------------'); writeln; writeln(tab,'This program will calculate the pay due, given'); writeln; writeln(tab,'the hours worked and rate of pay...'); writeln; end; { Title } { ---------------------------------------------------------- } { ---------------------------------------------------------- } begin { Main Program } Title; Calculate; end. { Main Program } { ---------------------------------------------------------- } { Exquisite! I was never this forward before I began Pascal programming, and I hope it works for you too! Notice that there are no global variables in this program. Is that lunar or what? The next series of tutorials will concentrate on wine making and rocketry. I look forward to boring you then...                                                     «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«» «» «» «» Propellus headus maximus «» «» «» «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«» 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43   }