home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / PASCTUT1.ZIP / CHAP5.TXT < prev    next >
Encoding:
Text File  |  1986-07-14  |  21.8 KB  |  455 lines

  1.              CHAPTER 5 - The Pascal procedures and functions
  2.  
  3.  
  4.             In order to define the procedures and functions we  will 
  5.  
  6.         need   to  lay  some  groundwork  in  the  form  of  a   few 
  7.  
  8.         definitions.   These  are important concepts,  so pay  close 
  9.  
  10.         attention.
  11.  
  12.         Program  Heading - This is the easiest part since it is only 
  13.  
  14.                    one  line,  at  least it has been in all  of  our 
  15.  
  16.                    programs  up  to  this point.  It is  simply  the 
  17.  
  18.                    PROGRAM line,  and it never needs to be any  more 
  19.  
  20.                    involved  than  it has been up to this  point  in 
  21.  
  22.                    TURBO Pascal.
  23.  
  24.         Declaration  Part  - This is the part of the  Pascal  source 
  25.  
  26.                    code in which all constants,  variables, and user 
  27.  
  28.                    defined  auxiliary  operations  are  defined.  In 
  29.  
  30.                    some of the programs we have examined, there have 
  31.  
  32.                    been one or more VAR declarations.  These are the 
  33.  
  34.                    only  components of the declaration part we  have 
  35.  
  36.                    used  to  this point.  There  are  actually  five 
  37.  
  38.                    components  in  the  declaration  part,  and  the 
  39.  
  40.                    procedures  and functions are the fifth part.  We 
  41.  
  42.                    will cover the others in the next chapter.
  43.  
  44.         Statement  Part  - This  is  the last  part  of  any  Pascal 
  45.  
  46.                    program,  and it is what we have been calling the 
  47.  
  48.                    main  program.   It  is  one  compound  statement 
  49.  
  50.                    bracketed with the reserved words BEGIN and END.
  51.  
  52.             It   is   very  important  that  you  grasp  the   above 
  53.  
  54.         definitions because we will be referring to them  constantly 
  55.  
  56.         during  this chapter,  and throughout the remainder of  this 
  57.  
  58.         tutorial.   With that introduction,  lets go on to our first 
  59.  
  60.         Pascal program with a procedure in it, in fact, it will have 
  61.  
  62.         three procedures.
  63.  
  64.                            THE FIRST PROCEDURES
  65.  
  66.             Load  PROCED1  as your first working file and  edit  it.  
  67.  
  68.         You  will notice that it doesn't look like anything you have 
  69.  
  70.         seen up to this point because it has PROCEDUREs in it.  Lets 
  71.  
  72.         go  back to our definitions from above.   The first line  is 
  73.  
  74.         the  Program Heading which should pose no  difficulty.   The 
  75.  
  76.         Declaration Part begins with the VAR statement and continues 
  77.  
  78.         down through and including all three procedures.   The  last 
  79.  
  80.         five  lines  constitute  the Statement Part.   It  may  seem 
  81.  
  82.         strange   that   what  appears  to  be   executable   Pascal 
  83.  
  84.         statements, and indeed they are executable, are contained in 
  85.  
  86.         the Declaration Part rather than the Statement  Part.   This 
  87.  
  88.         is  because of the Pascal definition and it will make  sense 
  89.  
  90.         when  we  have  completed our study of  the  procedures  and 
  91.  
  92.         functions.
  93.  
  94.  
  95.  
  96.                                  Page 20
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.              CHAPTER 5 - The Pascal procedures and functions
  107.  
  108.  
  109.  
  110.             Continuing to examine PROCED1,  we will make note of the 
  111.  
  112.         program itself,  which is the Statement Part.   The program, 
  113.  
  114.         due  to the nature of Pascal,  clearly tells us what it will 
  115.  
  116.         do,  namely write a header,  eight messages,  and an ending.  
  117.  
  118.         The  only problem we are faced with is,  how will  it  write 
  119.  
  120.         these messages? This is where the Declaration Part is called 
  121.  
  122.         upon to define these operations in detail.   The Declaration 
  123.  
  124.         Part  contains three procedures which will completely define 
  125.  
  126.         what  is to be done in the main program,  or  the  Statement 
  127.  
  128.         Part.  Hopefully you can begin to see why the procedures are 
  129.  
  130.         included in the Declaration Part of the Pascal program.
  131.  
  132.             Now  to  examine  one procedure in  detail,  namely  the 
  133.  
  134.         first.   The first statement we come to in the main  program 
  135.  
  136.         is the one that says simply,  "write_a_header" followed with 
  137.  
  138.         the usual end of statement, the semicolon.  This is a simple 
  139.  
  140.         procedure  call.   When  the  compiler finds  this  it  goes 
  141.  
  142.         looking  for a predefined procedure which it can execute  at 
  143.  
  144.         this point.   If it finds one in the Declaration Part of the 
  145.  
  146.         program, it will execute that procedure.  If it doesn't find 
  147.  
  148.         a user defined procedure,  it will search the Pascal library 
  149.  
  150.         for  a system defined procedure and execute it.   The  WRITE 
  151.  
  152.         and   WRITELN   statements  are  actually   system   defined 
  153.  
  154.         procedures,  and  you  have already been using them quite  a 
  155.  
  156.         bit,  so  procedures are not completely new to you.   If  it 
  157.  
  158.         doesn't find the procedure defined in either place,  it will 
  159.  
  160.         generate an error message.
  161.  
  162.                           HOW TO CALL A PROCEDURE
  163.  
  164.             To call a procedure,  we simply need to state its  name.  
  165.  
  166.         To  define  a  simple procedure,  we use the  reserved  word 
  167.  
  168.         PROCEDURE followed by its calling name,  with a semicolon as 
  169.  
  170.         a terminator.  Following the Procedure Heading, there is the 
  171.  
  172.         Declaration  Part of the procedure followed by a body  which 
  173.  
  174.         is  nothing more than a compound statement bracketed by  the 
  175.  
  176.         reserved  words  BEGIN and END.   This is identical  to  the 
  177.  
  178.         Statement Part of the main program except that the procedure 
  179.  
  180.         ends with a semicolon instead of a period.  Any valid Pascal 
  181.  
  182.         statements  can  be put between the BEGIN and  END,  and  in 
  183.  
  184.         fact,  there  is  no  difference in what can  be  put  in  a 
  185.  
  186.         procedure and what can be put in the main program.
  187.  
  188.             The program we are examining would be no different if we 
  189.  
  190.         would  eliminate the first procedure completely and move the 
  191.  
  192.         WRITELN contained in it down to the Statement Part in  place 
  193.  
  194.         of  "write_a_header".   If  that is not clear,  go back  and 
  195.  
  196.         reread these last two paragraphs until it is.
  197.  
  198.  
  199.  
  200.  
  201.  
  202.                                  Page 21
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.              CHAPTER 5 - The Pascal procedures and functions
  213.  
  214.  
  215.             The   next   line  will  cause   the   procedure   named 
  216.  
  217.         "write_a_message" to be called 8 times,  each time writing a 
  218.  
  219.         line  of  output.   Suffice it to say at this time that  the 
  220.  
  221.         value of "count",  as defined here, is available "globally", 
  222.  
  223.         meaning  anywhere  in the entire Pascal  program.   We  will 
  224.  
  225.         define the variable availability shortly.  Finally, the last 
  226.  
  227.         procedure  call is made,  causing the ending message  to  be 
  228.  
  229.         displayed,  and  the program execution is complete.   Having 
  230.  
  231.         examined your first Pascal procedures, there is a fine point 
  232.  
  233.         that  is  obvious  but  could  be  easily  overlooked.    We 
  234.  
  235.         mentioned the unbroken rule of Pascal in an earlier  chapter 
  236.  
  237.         and  it must be followed here too.   "Nothing can be used in 
  238.  
  239.         Pascal until it has been defined".   The procedures must all 
  240.  
  241.         be  defined  ahead  of  any  calls  to  them,   once   again 
  242.  
  243.         emphasizing  the fact that they are part of the  Declaration 
  244.  
  245.         Part of the program, not the Statement Part.
  246.  
  247.                            MORE PROCEDURE CALLS
  248.  
  249.             Assuming   you   have  run  PROCED1   successfully   and 
  250.  
  251.         understand its output, lets go on to PROCED2 and examine it.  
  252.  
  253.         To  begin with,  notice that there are three procedure calls 
  254.  
  255.         in  the  Statement  Part  of the program  and  each  has  an 
  256.  
  257.         additional  term  not  contained in the calls  in  the  last 
  258.  
  259.         program,  namely the word "index" within brackets.   This is 
  260.  
  261.         Pascals way of taking a variable parameter to the  procedure 
  262.  
  263.         when  it  is  called.   You will notice  that  the  variable 
  264.  
  265.         "index" is defined as an integer variable in the very top of 
  266.  
  267.         the    Declaration    Part.     Therefore   the    procedure 
  268.  
  269.         "print_data_out" had better be expecting an integer variable 
  270.  
  271.         or  we will have a type mismatch.   In fact,  observing  the 
  272.  
  273.         procedure itself,  indicates that it is indeed expecting  an 
  274.  
  275.         integer  variable  but  it  prefers  to  call  the  variable 
  276.  
  277.         "puppy".  Calling it something different poses no problem as 
  278.  
  279.         long  as  the main program doesn't try to call its  variable 
  280.  
  281.         "puppy",  and  the  procedure doesn't try to  use  the  name 
  282.  
  283.         "index".   Both  are actually referring to the same piece of 
  284.  
  285.         data but they simply wish to refer to it by different names.  
  286.  
  287.         Notice  that  the next procedure is called with index  as  a 
  288.  
  289.         parameter  and  it  prefers to call it by  the  name  "cat".  
  290.  
  291.         Notice that in both cases,  the procedures simply print  out 
  292.  
  293.         the parameter passed to it,  and each then try to modify the 
  294.  
  295.         value passed to it before passing it back.  We will see that 
  296.  
  297.         one will be successful and the other will not.
  298.  
  299.             We  are in a loop in which "count" is incremented from 1 
  300.  
  301.         to  3 and we are not allowed to modify the loop variable  so 
  302.  
  303.         we  make a copy of the value and call it  "index".   We  can 
  304.  
  305.         then modify "index" if we desire.   The first procedure does 
  306.  
  307.         not  contain  a  VAR in front of the  passed  parameter  and 
  308.  
  309.         therefore the parameter passing is only one way.  The system 
  310.  
  311.  
  312.  
  313.                                  Page 22
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.              CHAPTER 5 - The Pascal procedures and functions
  324.  
  325.  
  326.         makes  a  copy  of  "index",  and passes  the  copy  to  the 
  327.  
  328.         procedure which can do anything with it, of course using its 
  329.  
  330.         new  name,  "puppy",  but  when control returns to the  main 
  331.  
  332.         program,  the original value of "index" is still there.   So 
  333.  
  334.         think  of  the passed parameter without the VAR as  one  way 
  335.  
  336.         parameter passing.   This is a "call by value" because  only 
  337.  
  338.         the value of the variable is passed to the procedure.
  339.  
  340.             The  second procedure has the reserved word VAR in front 
  341.  
  342.         of its desired name for the variable,  namely "cat",  so  it 
  343.  
  344.         can  not only receive the variable,  it can modify  it,  and 
  345.  
  346.         return the modified value to the main program. A copy is not 
  347.  
  348.         made.   The  original  variable  named "index"  is  actually 
  349.  
  350.         passed  to this procedure and it can  modify  it,  therefore 
  351.  
  352.         communicating to the main program.   A passed parameter with 
  353.  
  354.         a VAR in front of it is therefore a two way situation.  This 
  355.  
  356.         is a "call by reference" since the reference to the original 
  357.  
  358.         variable is passed to the procedure.
  359.  
  360.             When you run this program,  you will find that the first 
  361.  
  362.         procedure  is unable to get the value of 12 back to the main 
  363.  
  364.         program,  but  the second procedure does in fact succeed  in 
  365.  
  366.         returning  its value of 35 to the main  program.   Spend  as 
  367.  
  368.         much  time as you like studying this program until you fully 
  369.  
  370.         understand it.   It should be noted that as many  parameters 
  371.  
  372.         as  desired can be passed to and from a procedure by  simply 
  373.  
  374.         making  a  list  separated  by  commas  in  the  calls,  and 
  375.  
  376.         separated by semicolons in the procedure.
  377.  
  378.             For  your  own  enlightenment,  examine PROCED3  for  an 
  379.  
  380.         example  of a procedure call with more than one variable  in 
  381.  
  382.         the call.   Normally, you would group the three input values 
  383.  
  384.         together,   but  for  purposes  of  illustration,  they  are 
  385.  
  386.         separated.   Observe that the variable "fruit" is a two  way 
  387.  
  388.         variable.    When   you  are  satisfied  with  the   present 
  389.  
  390.         illustration,  we will go on to study the scope of variables 
  391.  
  392.         using PROCED4.
  393.  
  394.                         A MULTIPLY DEFINED VARIABLE
  395.  
  396.             If  you will examine PROCED4,  you will notice that  the 
  397.  
  398.         variable "count" is defined twice,  once in the main program 
  399.  
  400.         VAR  block  and once in the VAR block contained  within  the 
  401.  
  402.         procedure named "print_some_data".   This is perfectly legal 
  403.  
  404.         and is within the Pascal definition.
  405.  
  406.             The  variable "index" is defined in the main program VAR 
  407.  
  408.         block  and  is  valid  anywhere  within  the  entire  Pascal 
  409.  
  410.         program,  including the procedures.  The variable "count" is 
  411.  
  412.         also defined in the main program VAR block and is also valid 
  413.  
  414.         anywhere within the entire Pascal program, except within the 
  415.  
  416.  
  417.  
  418.                                  Page 23
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.              CHAPTER 5 - The Pascal procedures and functions
  429.  
  430.  
  431.         procedure  where another variable is defined with  the  same 
  432.  
  433.         name  "count".   The two variables with the same name are in 
  434.  
  435.         fact,  two  completely  different  variables.   If  a  third 
  436.  
  437.         variable  was  defined within the  procedure,  it  would  be 
  438.  
  439.         invisible  to  the main program,  since it was defined at  a 
  440.  
  441.         lower  level than that of the main  program.   Any  variable 
  442.  
  443.         defined in the main program or in any procedure is available 
  444.  
  445.         in  any  procedure that is within the scope of the  defining 
  446.  
  447.         procedure.  That last sentence seems confusing, but thinking 
  448.  
  449.         about it will clear it up.   This is a difficult concept  to 
  450.  
  451.         grasp but is very important.
  452.  
  453.  
  454.                     PROCEDURES CALLING OTHER PROCEDURES
  455.  
  456.             Load and examine PROCED5 to see an example of procedures 
  457.  
  458.         that call other procedures.  Keep in mind that, "Nothing can 
  459.  
  460.         be used in Pascal until it has been previously defined", and 
  461.  
  462.         the order of procedures will be clear in this example.
  463.  
  464.             Now that you have a good grasp of procedures, we need to 
  465.  
  466.         make  another  important point.   Remember that  any  Pascal 
  467.  
  468.         program is made up of three parts,  the Program Heading, the 
  469.  
  470.         Declaration Part,  and the Statement Part.   The Declaration 
  471.  
  472.         Part is composed of five unique components, four of which we 
  473.  
  474.         will  discuss  in detail in the next chapter,  and the  last 
  475.  
  476.         component,  which  is composed of some number of  procedures 
  477.  
  478.         and functions.  We will cover functions in the next example, 
  479.  
  480.         so  for  now  simply  accept the fact  that  it  is  like  a 
  481.  
  482.         procedure.   A procedure is also composed of three parts,  a 
  483.  
  484.         Program  Heading  (which says  "PROCEDURE"),  a  Declaration 
  485.  
  486.         Part,  and a Statement Part.  A procedure, by definition, is 
  487.  
  488.         therefore  nothing more or less than another complete Pascal 
  489.  
  490.         program embedded within the main program,  and any number of 
  491.  
  492.         procedures  can  be located in the Declaration Part  of  the 
  493.  
  494.         main program.  These procedures are all in a line, one right 
  495.  
  496.         after another.
  497.  
  498.             Since a procedure is defined like the main  program,  it 
  499.  
  500.         would  seem to be possible to embed another procedure within 
  501.  
  502.         the  declaration part of any procedure.   This is  perfectly 
  503.  
  504.         valid  and  is often done,  but remember that  the  embedded 
  505.  
  506.         procedure can only be called by the procedure in which it is 
  507.  
  508.         embedded,  not by the main program.   That is probably a bit 
  509.  
  510.         difficult to grasp.   Don't worry about it too much now,  as 
  511.  
  512.         you become proficient as a Pascal programmer,  you will very 
  513.  
  514.         clearly see how that is used.
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.                                  Page 24
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.              CHAPTER 5 - The Pascal procedures and functions
  533.  
  534.  
  535.                        NOW LET'S LOOK AT A FUNCTION
  536.  
  537.             Now to keep a promise, lets examine FUNCTION to see what 
  538.  
  539.         a  function  is  and how to use it.   In  this  very  simple 
  540.  
  541.         program,  we have a function that simply multiplies the  sum 
  542.  
  543.         of  two  variables by 4 and returns the result.   The  major 
  544.  
  545.         difference  between a function and a procedure is  that  the 
  546.  
  547.         function  returns a single value and is called from within a 
  548.  
  549.         mathematical expression, a WRITELN command, or anywhere that 
  550.  
  551.         it  is  valid  to put any variable,  since it  is  really  a 
  552.  
  553.         variable  itself.   Observing  the Program  Heading  of  the 
  554.  
  555.         function   reveals  the  two  input  variables  inside   the 
  556.  
  557.         parenthesis  pair  being defined as INTEGER  variables,  and 
  558.  
  559.         following  the parenthesis is a colon and  another  INTEGER.  
  560.  
  561.         The  last INTEGER is used to define the type of the variable 
  562.  
  563.         being returned to the main program.  Since any call to  this 
  564.  
  565.         function  is actually replaced by an integer upon completion 
  566.  
  567.         of the call,  it can be used anywhere in a program where  an 
  568.  
  569.         integer variable can be used.
  570.  
  571.                      NOW FOR THE MYSTERY OF RECURSION
  572.  
  573.             One of the great mysteries of Pascal to many people,  is 
  574.  
  575.         the recursion of procedure calls.  Simply defined, recursion 
  576.  
  577.         is  the ability of a procedure to call itself.   Examine the 
  578.  
  579.         Pascal  example file RECURSON for an example  of  recursion.  
  580.  
  581.         The  main  program  is very simple,  it  sets  the  variable 
  582.  
  583.         "count"   to   the   value  7  and   calls   the   procedure 
  584.  
  585.         "print_and_decrement".   The  procedure prefers to refer  to 
  586.  
  587.         the  variable by the name "index" but that poses no problem.  
  588.  
  589.         The  procedure writes a line to the video display  with  the 
  590.  
  591.         value of "index" written within the line, and decrements the 
  592.  
  593.         variable.
  594.  
  595.             The IF statement introduces the interesting part of this 
  596.  
  597.         program.   If  the variable is greater than zero,  and it is 
  598.  
  599.         now  6,  then the procedure "print_and_decrement" is  called 
  600.  
  601.         once again.   This might seem to create a problem except for 
  602.  
  603.         the  fact  that this is perfectly  legal  in  Pascal.   Upon 
  604.  
  605.         entering the procedure the second time, the value of "index" 
  606.  
  607.         is printed as 6, and it is once again decremented.  Since it 
  608.  
  609.         is  now 5,  the same procedure will be called again,  and it 
  610.  
  611.         will continue until the value of "index" is reduced to  zero 
  612.  
  613.         when each procedure call will be completed one at a time and 
  614.  
  615.         control will return to the main program.
  616.  
  617.                         ABOUT RECURSIVE PROCEDURES
  618.  
  619.             This is really a stupid way to implement this particular 
  620.  
  621.         program,  but  it is the simplest recursive program that can 
  622.  
  623.         be  written and therefore the easiest  to  understand.   You 
  624.  
  625.  
  626.  
  627.                                  Page 25
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.              CHAPTER 5 - The Pascal procedures and functions
  638.  
  639.  
  640.         will have occasional use for recursive procedures,  so don't 
  641.  
  642.         be  afraid  to  try  them.    Remember  that  the  recursive 
  643.  
  644.         procedure  must have some variable converging to  something, 
  645.  
  646.         or you will have an infinite loop.
  647.  
  648.                            THE FORWARD REFERENCE
  649.  
  650.             Occasionally  you  will  have  a need  to  refer  to   a 
  651.  
  652.         procedure  before you can define it.   In that case you will 
  653.  
  654.         need  a  FORWARD  reference.   The program  FORWARD  has  an 
  655.  
  656.         example of a forward reference in it.  In this program, each 
  657.  
  658.         one of the procedures calls the other,  a form of recursion.  
  659.  
  660.         This program,  like the last,  is a very stupid way to count 
  661.  
  662.         from  7 to 0,  but it is the simplest program possible  with 
  663.  
  664.         the forward reference.
  665.  
  666.             The  first procedure,  "write_a_line",  has  its  header 
  667.  
  668.         defined  in  exactly the same manner as any other  procedure 
  669.  
  670.         but instead of the normal procedure body,  only the reserved 
  671.  
  672.         word  FORWARD is given.   This tells the compiler  that  the 
  673.  
  674.         procedure  will  be defined later.   The next  procedure  is 
  675.  
  676.         defined  as usual,  then the body of "write_a_line" is given 
  677.  
  678.         with  only  the reserved word PROCEDURE  and  the  procedure 
  679.  
  680.         name.   The variable reference has been defined earlier.  In 
  681.  
  682.         this  way,  each  of the procedure names are defined  before 
  683.  
  684.         they are called.
  685.  
  686.             It would be possible,  by using the FORWARD reference in 
  687.  
  688.         great  numbers,  to  move  the main  program  ahead  of  all 
  689.  
  690.         procedure  definitions and have the program structured  like 
  691.  
  692.         some  other languages.   This style of programming would  be 
  693.  
  694.         perfectly legal as far as the compiler is concerned, but the 
  695.  
  696.         resulting  program would be very nonstandard and  confusing.  
  697.  
  698.         You   would  do  well  to  stick  with  conventional  Pascal 
  699.  
  700.         formatting   techniques  and  use  the   FORWARD   reference 
  701.  
  702.         sparingly.
  703.  
  704.                            PROGRAMMING EXERCISES
  705.  
  706.         1.  Write a program to write your name,  address,  and phone 
  707.  
  708.             number with each WRITELN in a different procedure.
  709.  
  710.         2.  Add a statement to the procedure in RECURSON to  display 
  711.  
  712.             the value of "index" after the call to itself so you can 
  713.  
  714.             see  the  value increasing as the  recurring  calls  are 
  715.  
  716.             returned to the next higher level.
  717.  
  718.         3.  Rewrite  TEMPCONV putting the  centigrade to  fahrenheit 
  719.  
  720.             formulas in a function call.
  721.  
  722.  
  723.  
  724.  
  725.                                  Page 26
  726.  
  727.