{ «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«» «» «» «» TUTORIAL SIXTEEN «» «» «» «» by «» «» «» «» Anthony Peck «» «» «» «» «» «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»   43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 I don't care how it looks as long as it functions... Having played around with procedures a bit in the last series of tutorials, you can probably begin to see that they are of some importance to programmers, but of little importance to tropical fish. One limitation of the dash-ed things is that you cannot use a procedure in the context of a line of code. For example, let's say that you had a procedure called ADD that took two numbers and added them (wow, ground breaking! ). In order to correctly code a statement that prints to the screen the result of this procedure you'd need something like this...    ...         ADD(number1,number2,result);         writeln('The answer is ',result);         ...         An extra variable called result is needed, and the whole thing is more trouble than a mother-in-law on a honeymoon. Wouldn't it be nice to be able to call that procedure during the writeln command? It'd be like having all the strawberries and cream to yourself without having to spit on them first! We could then rewrite the above code...    ...         writeln('The answer is ',ADD(number1,number2));        ...         The procedure would then be like the commands SQRT and ABS, which take a number and return the square root and absolute value, respectively. These thingos are called functions. Pascal has quite a few functions built in to the language (like SQRT and ABS), but the fun bit is that you can also design your own. This gives bulk heaps mega flexibility for your programs. This tutorial will make use of a function to find the number of bottles of red wine produced per acre of grapes. Hic...} Program MDTute16 (Input,Output); { This program finds the number of bottles of red wine produced given the number of acres of grapes planted. Author : A N Peck Date : 22nd December 1994 Procedures used: Getnumber - prompts user for a number Title - makes a nice little title appear! Functions used: Grapetowine - calculates bottles of wine } var acres,factor: real; { ---------------------------------------------------------- } Procedure Title; { Prints up a nice title on the screen } Const tab = ' '; begin writeln; writeln(tab,'Wine Calculator'); writeln(tab,'---------------'); writeln; writeln(tab,'This program will calculate the number of bottles'); writeln; writeln(tab,'of wine produced for the acres of grapes planted!'); writeln; end; { Title } { ---------------------------------------------------------- } Procedure Getnumber (whichone: string; Var userchoice: real); { Prompts user for a number } begin write(' Please enter the ',whichone,': '); readln(userchoice); userchoice := Abs(userchoice); writeln; end; { Getnumber } { ---------------------------------------------------------- } Function Grapetowine(landsize,rate: real): real;  { ^ ^ ^ | | | | | |_ The function has | | a variable type. In | | this case "real". | | This means the value | | returned will have | | this type. Note the | | full colon. | | | |_ Variables can be passed to functions just | like procedures. In this case, we have | passed the landsize and rate numbers, both | of which have the type "real". | |_ The start of any function code has the reserved word "function", followed by the name of the function, in this case "Grapetowine". } { Returns wine bottles production given the area and rate } begin { ^ | |_ The code begins!  } Grapetowine := landsize*rate; { ^ | |_ All functions must have their name and the result somewhere in the function code. It is this line (in this case the only line) which sets the value of "Grapetowine". Notice how we treat this name just like a variable. Alternatively you could have a local variable and then make "Grapetowine" equal to that variable. For example...    ...         var result:real;         begin         result := landsize*rate;         Grapetowine := result;         end;         ...         end; { Grapetowine } { ---------------------------------------------------------- } { ---------------------------------------------------------- } begin { Main Program } acres := 0.00; factor := 0.00; Title; writeln; Getnumber('number of acres planted',acres); Getnumber('bottles per acre predicted',factor); writeln(' Bottles of wine expected:', Grapetowine(acres,factor):0:2); { ^ | |_ Here the function "Grapetowine" is called within the writeln statement, just as if it were a built in Pascal function like "SQRT". Oh what joy to be able to define your very own functions! } writeln; end. { Main Program } { ---------------------------------------------------------- } { How simple! Functions can certainly make your code a lot easier to read, and like procedures they are quite portable. If you have any good functions, send them to the major charity organisations and watch your smug-meter go through the roof!                           «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«» «» «» «» Terrus Firmus! «» «» «» «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«» 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43   }