{ «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«» «» «» «» TUTORIAL SEVENTEEN «» «» «» «» by «» «» «» «» Anthony Peck «» «» «» «» «» «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»   43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 Truth, Justice and a set of steak knives... The pursuit of truth has leapt in disgust from the world of advertising and landed squarely in the lap of the programmer. Using functions, we can pursue the truth with the help of a variable type called BOOLEAN. Boolean algebra is named after George Boole (1815-1864), and isn't it great his name wasn't George Finklesteiner-Smitherson. A boolean variable type can have only two values, TRUE or FALSE. Here's some examples suggested to me by Ralph (my pet three-toed sloth and part-time exotic tap dancer)...    Statement: It rained today.       Boolean response: FALSE          Statement: Jackson Pollock knew something we don't.       Boolean response: TRUE          Statement: The number you're thinking of       is even and less than 10.       Boolean response: FALSE        The use of boolean operators such as AND, OR and NOT with a boolean variable type can provide a program with quite sophisticated means of making "judgements" about where it should look for it's next instruction. Enough! Let's try a progam that uses a boolean variable type in a function to find if an entered year is a leap year. } Program MDTute17 (Input,Output); { This program will determine if an entered year is a leap year. Author : A N Peck Date : 28th December 1994 Procedures used: Getnumber - prompts user for a number Title - makes a nice little title appear! Functions used: Leaper - calculates leap years } var year: integer; { ---------------------------------------------------------- } Procedure Title; { Prints up a nice title on the screen } Const tab = ' '; begin writeln; writeln(tab,'Leap Year Calculator'); writeln(tab,'--------------------'); writeln; write(tab,'This program will calculate if the year'); writeln(' entered is a leap year!'); writeln; end; { Title } { ---------------------------------------------------------- } Procedure Getnumber (whichone: string; Var userchoice: integer); { Prompts user for a number } begin write(' Please enter the ',whichone,': '); readln(userchoice); userchoice := Abs(userchoice); writeln; end; { Getnumber } { ---------------------------------------------------------- } Function Leaper(year_given: integer): boolean; { ^ | Here the function return type is _| given as BOOLEAN. The function will give only a TRUE or FALSE response, depending on the year that it is given.  } { Returns boolean for leap year calculation } begin leaper := FALSE; { ^ | |_ We set the function value to FALSE. We don't need to always initialise our function like this, but it's good for you and could lead to an early promotion. } if ((year_given mod 100 = 0) and (year_given mod 400 = 0)) or ((year_given mod 100 <> 0) and (year_given mod 4 = 0)) then leaper := TRUE; { ^ | |_ This whole "if" line uses the boolean operators AND and OR to check if the year satisfies the conditions to be a leap year. If these conditions are met, then the function leaper set to TRUE. See Diagram_seven for a pictorial representation of this weird statement. } end; { Leaper } { ---------------------------------------------------------- } { ---------------------------------------------------------- } begin { Main Program } year := 0; Title; writeln; Getnumber('year',year); if leaper(year) then { ^ | |_ Here we call the function leaper. If it's true (i.e. the year sent to the function is a leap year) then this returns a TRUE value. Given that the function is TRUE, the next line is executed. If not, the statement after the ELSE is executed. } writeln(' Yep, ',year,' is a leaper!') else writeln(' No leaping in ',year,'!'); writeln; end. { Main Program } { ---------------------------------------------------------- } { Massive! Boolean functions are used all the time. An example of a standard Pascal boolean function is ODD. This function accepts an integer value and returns TRUE if the number is odd. For example:    ...       Write('Enter a number: ');       Readln(number);       If Odd(number) then       writeln('This is odd!')       else       writeln('Now we are even!');       ...        Next time you need to fill in half an hour while waiting for the bus, try a boolean function. The time will go quickly, and you'll be admired by your fellow commuters.                              «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«» «» «» «» Mountus clime bus «» «» «» «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«» 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43   }