home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / CTUTOR1.ZIP / CHAP5.TXT < prev    next >
Encoding:
Text File  |  1986-06-30  |  28.4 KB  |  653 lines

  1.                     Chapter 5 - Functions and variables
  2.  
  3.  
  4.                       OUR FIRST USER DEFINED FUNCTION
  5.  
  6.              Load  and examine the file SUMSQRES.C for an example of 
  7.  
  8.         a C program with functions.   Actually this is not the first 
  9.  
  10.         function  we have encountered because the "main" program  we 
  11.  
  12.         have been using all along is technically a function,  as  is 
  13.  
  14.         the  "printf" function.   The "printf" function is a library 
  15.  
  16.         function that was supplied with your compiler. 
  17.  
  18.              Notice the executable part of this program.   It begins 
  19.  
  20.         with a line that simply says "header()", which is the way to 
  21.  
  22.         call any function.  The parentheses are required because the 
  23.  
  24.         C compiler uses them to determine that it is a function call 
  25.  
  26.         and not simply a misplaced variable.  When the program comes 
  27.  
  28.         to this line of code, the function named "header" is called, 
  29.  
  30.         its  statements  are executed,  and control returns  to  the 
  31.  
  32.         statement  following this call.   Continuing on we come to a 
  33.  
  34.         "for"  loop which will be executed 7 times and  which  calls 
  35.  
  36.         another  function named "square" each time through the loop, 
  37.  
  38.         and  finally  a function named "ending" will be  called  and 
  39.  
  40.         executed.    For  the  moment  ignore  the  "index"  in  the 
  41.  
  42.         parentheses of the call to "square".  We have seen that this 
  43.  
  44.         program  therefore calls a header,  7 square calls,  and  an 
  45.  
  46.         ending. Now we need to define the functions.
  47.  
  48.                            DEFINING THE FUNCTIONS
  49.  
  50.              Following the main program you will see another program 
  51.  
  52.         that  follows all of the rules set forth so far for a "main" 
  53.  
  54.         program  except that it is named "header()".   This  is  the 
  55.  
  56.         function which is called from within the main program.  Each 
  57.  
  58.         of  these  statements are executed,  and when they  are  all 
  59.  
  60.         complete, control returns to the main program. 
  61.  
  62.              The  first  statement sets the variable "sum" equal  to 
  63.  
  64.         zero because we will use it to accumulate a sum of  squares.  
  65.  
  66.         Since  the  variable  "sum" is defined as  an  integer  type 
  67.  
  68.         variable  prior to the main program,  it is available to  be 
  69.  
  70.         used  in  any of the following functions.   It is  called  a 
  71.  
  72.         "global" variable,  and it's scope is the entire program and 
  73.  
  74.         all  functions.   More  will  be  said about  the  scope  of 
  75.  
  76.         variables  at the end of this chapter.   The next  statement 
  77.  
  78.         outputs  a header message to the monitor.   Program  control 
  79.  
  80.         then  returns  to  the  main  program  since  there  are  no 
  81.  
  82.         additional statements to execute in this function.
  83.  
  84.              It should be clear to you that the two executable lines 
  85.  
  86.         from  this  function  could be moved to  the  main  program, 
  87.  
  88.         replacing the header call,  and the program would do exactly 
  89.  
  90.         the same thing that it does as it is now written.  This does 
  91.  
  92.         not minimize the value of functions,  it merely  illustrates 
  93.  
  94.  
  95.  
  96.                                   Page 29    
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.                     Chapter 5 - Functions and variables
  107.  
  108.  
  109.         the operation of this simple function in a simple way.   You 
  110.  
  111.         will find functions to be very valuable in C programming.
  112.  
  113.                        PASSING A VALUE TO A FUNCTION
  114.  
  115.              Going  back  to the main program,  and the  "for"  loop 
  116.  
  117.         specifically,  we find the new construct from the end of the 
  118.  
  119.         last  lesson used in the last part of the for  loop,  namely 
  120.  
  121.         the "index++".   You should get used to seeing this,  as you 
  122.  
  123.         will see it a lot in C programs. 
  124.  
  125.              In the call to the function "square",  we have an added 
  126.  
  127.         feature, namely the variable "index" within the parentheses.  
  128.  
  129.         This  is  an indication to the compiler that when you go  to 
  130.  
  131.         the function,  you wish to take along the value of index  to 
  132.  
  133.         use in the execution of that function.  Looking ahead at the 
  134.  
  135.         function  "square",  we  find that another variable name  is 
  136.  
  137.         enclosed in its parentheses,  namely the variable  "number".  
  138.  
  139.         This  is  the name we prefer to call the variable passed  to 
  140.  
  141.         the  function when we are in the function.   We can call  it 
  142.  
  143.         anything  we wish as long as it follows the rules of  naming 
  144.  
  145.         an identifier.   Since the function must know what type  the 
  146.  
  147.         variable  is,  it is defined following the function name but 
  148.  
  149.         before the opening brace of the function itself.   Thus, the 
  150.  
  151.         line  containing "int number;" tells the function  that  the 
  152.  
  153.         value  passed to it will be an integer type variable.   With 
  154.  
  155.         all of that out of the way,  we now have the value of  index 
  156.  
  157.         from  the main program passed to the function "square",  but 
  158.  
  159.         renamed "number", and available for use within the function.
  160.  
  161.              Following the opening brace of the function,  we define 
  162.  
  163.         another  variable "numsq" for use only within  the  function 
  164.  
  165.         itself,  (more  about  that  later)  and  proceed  with  the 
  166.  
  167.         required  calculations.   We set "numsq" equal to the square 
  168.  
  169.         of  number,  then add numsq to the current total  stored  in 
  170.  
  171.         "sum".   Remember  that "sum += numsq" is the same as "sum = 
  172.  
  173.         sum + numsq" from the last lesson.   We print the number and 
  174.  
  175.         its square, and return to the main program.
  176.  
  177.                   MORE ABOUT PASSING A VALUE TO A FUNCTION
  178.  
  179.              When we passed the value of "index" to the function,  a 
  180.  
  181.         little  more  happened  than meets  the  eye.   We  did  not 
  182.  
  183.         actually  pass  the  value  of index  to  the  function,  we 
  184.  
  185.         actually  passed  a  copy of the value.   In  this  way  the 
  186.  
  187.         original value is protected from accidental corruption by  a 
  188.  
  189.         called  function.   We  could  have  modified  the  variable 
  190.  
  191.         "number" in any way we wished in the function "square",  and 
  192.  
  193.         when we returned to the main program, "index" would not have 
  194.  
  195.         been  modified.   We thus protect the value of a variable in 
  196.  
  197.         the main program from being accidentally corrupted,  but  we 
  198.  
  199.  
  200.  
  201.                                   Page 30    
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.                     Chapter 5 - Functions and variables
  212.  
  213.  
  214.         cannot  return  a value to the main program from a  function 
  215.  
  216.         using this technique.  We will find a well defined method of 
  217.  
  218.         returning  values  to  the main program or  to  any  calling 
  219.  
  220.         function  when we get to arrays and another method  when  we 
  221.  
  222.         get  to pointers.   Until then the only way you will be able 
  223.  
  224.         to  communicate  back to the calling function will  be  with 
  225.  
  226.         global  variables.    We  have  already  hinted  at   global 
  227.  
  228.         variables  above,  and will discuss them in detail later  in 
  229.  
  230.         this chapter.
  231.  
  232.              Continuing  in  the main program,  we come to the  last 
  233.  
  234.         function call, the call to "ending".  This call simply calls 
  235.  
  236.         the last function which has no local variables defined.   It 
  237.  
  238.         prints out a message with the value of "sum" contained in it 
  239.  
  240.         to  end the program.   The program ends by returning to  the 
  241.  
  242.         main  program and finding nothing else to do.   Compile  and 
  243.  
  244.         run this program and observe the output.
  245.  
  246.                         NOW TO CONFESS A LITTLE LIE
  247.  
  248.              I told you a short time ago that the only way to get  a 
  249.  
  250.         value  back to the main program was through use of a  global 
  251.  
  252.         variable,  but  there  is another way which we will  discuss 
  253.  
  254.         after  you load and display the file  named  SQUARES.C.   In 
  255.  
  256.         this  file we will see that it is simple to return a  single 
  257.  
  258.         value  from a called function to the calling function.   But 
  259.  
  260.         once again,  it is true that to return more than one  value, 
  261.  
  262.         we will need to study either arrays or pointers.
  263.  
  264.              In the main program, we define two integers and begin a 
  265.  
  266.         "for"  loop  which  will be executed  8  times.   The  first 
  267.  
  268.         statement  of the for loop is "y = squ(x);",  which is a new 
  269.  
  270.         and rather strange looking construct.  From past experience, 
  271.  
  272.         we  should have no trouble understanding that  the  "squ(x)" 
  273.  
  274.         portion  of  the statement is a call to the  "squ"  function 
  275.  
  276.         taking along the value of "x" as a variable.   Looking ahead 
  277.  
  278.         to  the function itself we find that the function prefers to 
  279.  
  280.         call  the variable "in" and it proceeds to square the  value 
  281.  
  282.         of "in" and call the result "square".   Finally,  a new kind 
  283.  
  284.         of a statement appears,  the "return" statement.   The value 
  285.  
  286.         within  the parentheses is assigned to the  function  itself 
  287.  
  288.         and  is  returned  as a usable value in  the  main  program.  
  289.  
  290.         Thus,  the  function call "squ(x)" is assigned the value  of 
  291.  
  292.         the square and returned to the main program such that "y" is 
  293.  
  294.         then  set  equal  to  that value.   If  "x"  were  therefore 
  295.  
  296.         assigned the value 4 prior to this call,  "y" would then  be 
  297.  
  298.         set to 16 as a result of this line of code. 
  299.  
  300.              Another  way  to  think  of this  is  to  consider  the 
  301.  
  302.         grouping  of characters "squ(x)" as another variable with  a 
  303.  
  304.         value  that is the square of "x",  and this new variable can 
  305.  
  306.  
  307.  
  308.                                   Page 31    
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.                     Chapter 5 - Functions and variables
  319.  
  320.  
  321.         be used any place it is legal to use a variable of its type.  
  322.  
  323.         The values of "x" and "y" are then printed out.
  324.  
  325.              To  illustrate  that the grouping of  "squ(x)"  can  be 
  326.  
  327.         thought  of as just another variable,  another "for" loop is 
  328.  
  329.         introduced in which the function call is placed in the print 
  330.  
  331.         statement rather than assigning it to a new variable.
  332.  
  333.              One  last  point must be made,  the  type  of  variable 
  334.  
  335.         returned must be defined in order to make sense of the data, 
  336.  
  337.         but the compiler will default the type to integer if none is 
  338.  
  339.         specified.   If  any  other  type is  desired,  it  must  be 
  340.  
  341.         explicitly defined.   How to do this will be demonstrated in 
  342.  
  343.         the next example program.
  344.  
  345.              Compile and run this program.
  346.  
  347.                             FLOATING POINT FUNCTIONS
  348.  
  349.              Load the program FLOATSQ.C for an example of a function 
  350.  
  351.         with a floating point type of return.  It begins by defining 
  352.  
  353.         a global floating point variable we will use later.  Then in 
  354.  
  355.         the  "main"  part  of the program,  an integer  is  defined, 
  356.  
  357.         followed  by two floating point variables,  and then by  two 
  358.  
  359.         strange  looking definitions.   The expressions "sqr()"  and 
  360.  
  361.         "glsqr()"  look like function calls and they are.   This  is 
  362.  
  363.         the proper way in C to define that a function will return  a 
  364.  
  365.         value that is not of the type "int", but of some other type, 
  366.  
  367.         in  this case "float".   This tells the compiler that when a 
  368.  
  369.         value  is returned from either of these  two  functions,  it 
  370.  
  371.         will be of type "float".
  372.  
  373.              Now  refer to the function "sqr" near the center of the 
  374.  
  375.         listing and you will see that the function name is  preceded 
  376.  
  377.         by the name "float".   This is an indication to the compiler 
  378.  
  379.         that  this  function will return a value of type "float"  to 
  380.  
  381.         any program that calls it.   The function is now  compatible 
  382.  
  383.         with  the call to it.   The line following the function name 
  384.  
  385.         contains  "float inval;",  which indicates to  the  compiler 
  386.  
  387.         that  the variable passed to this function from the  calling 
  388.  
  389.         program will be of type "float".
  390.  
  391.              The next function,  namely "glsqr",  will also return a 
  392.  
  393.         "float"  type  variable,  but it uses a global variable  for 
  394.  
  395.         input.   It  also does the squaring right within the  return 
  396.  
  397.         statement  and  therefore has no need to define  a  separate 
  398.  
  399.         variable to store the product.
  400.  
  401.              The  overall structure of this program should  pose  no 
  402.  
  403.         problem and will not be discussed in any further detail.  As 
  404.  
  405.  
  406.  
  407.  
  408.  
  409.                                   Page 32    
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.                     Chapter 5 - Functions and variables
  420.  
  421.  
  422.         is customary with all example programs, compile and run this 
  423.  
  424.         program.
  425.  
  426.              There  will  be times that you will have a need  for  a 
  427.  
  428.         function   to   return  a  pointer  as  a  result  of   some 
  429.  
  430.         calculation.  There is a way to define a function so that it 
  431.  
  432.         does  just that.   We haven't studied pointers yet,  but  we 
  433.  
  434.         will soon.  This is just a short preview of things to come.
  435.  
  436.                              SCOPE OF VARIABLES
  437.  
  438.              Load the next program,  SCOPE.C,  and display it for  a 
  439.  
  440.         discussion of the scope of variables in a program.
  441.  
  442.              The first variable defined is a global variable "count" 
  443.  
  444.         which  is available to any function in the program since  it 
  445.  
  446.         is defined before any of the functions.   In addition, it is 
  447.  
  448.         always  available  because  it does not come and go  as  the 
  449.  
  450.         program  is  executed.   (That  will  make  sense  shortly.) 
  451.  
  452.         Farther down in the program,  another global variable  named 
  453.  
  454.         "counter"  is  defined  which  is also  global  but  is  not 
  455.  
  456.         available  to the main program since it is defined following 
  457.  
  458.         the main program.  A global variable is any variable that is 
  459.  
  460.         defined  outside of any function.   Note that both of  these 
  461.  
  462.         variables  are sometimes referred to as  external  variables 
  463.  
  464.         because they are external to any functions.
  465.  
  466.              Return  to  the  main  program and  you  will  see  the 
  467.  
  468.         variable  "index"  defined as an integer.   Ignore the  word 
  469.  
  470.         "register" for the moment.   This variable is only available 
  471.  
  472.         within the main program because that is where it is defined.  
  473.  
  474.         In addition, it is an "automatic" variable, which means that 
  475.  
  476.         it  only comes into existence when the function in which  it 
  477.  
  478.         is  contained  is  invoked,  and ceases to  exist  when  the 
  479.  
  480.         function  is  finished.   This  really  means  nothing  here 
  481.  
  482.         because the main program is always in operation,  even  when 
  483.  
  484.         it  gives  control to another function.  Another integer  is 
  485.  
  486.         defined  within  the  "for"  braces,  namely  "stuff".   Any 
  487.  
  488.         pairing  of braces can contain a variable  definition  which 
  489.  
  490.         will  be  valid  and  available only while  the  program  is 
  491.  
  492.         executing statements within those braces.  The variable will 
  493.  
  494.         be  an  "automatic" variable and will cease  to  exist  when 
  495.  
  496.         execution leaves the braces.   This is convenient to use for 
  497.  
  498.         a loop counter or some other very localized variable.
  499.  
  500.                        MORE ON "AUTOMATIC" VARIABLES
  501.  
  502.              Observe  the  function named "head1".   It  contains  a 
  503.  
  504.         variable  named  "index",  which has nothing to do with  the 
  505.  
  506.         "index" of the main program,  except that both are automatic 
  507.  
  508.         variables.   When  the  program is  not  actually  executing 
  509.  
  510.  
  511.  
  512.                                   Page 33    
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.                     Chapter 5 - Functions and variables
  523.  
  524.  
  525.         statements  in  this function,  this variable named  "index" 
  526.  
  527.         does not even exist.   When "head1" is called,  the variable 
  528.  
  529.         is  generated,  and  when "head1" completes  its  task,  the 
  530.  
  531.         variable  "index" is eliminated completely  from  existence.  
  532.  
  533.         Keep  in mind however that this does not affect the variable 
  534.  
  535.         of  the  same  name  in the main  program,  since  it  is  a 
  536.  
  537.         completely separate entity.
  538.  
  539.              Automatic   variables  therefore,   are   automatically 
  540.  
  541.         generated and disposed of when needed.   The important thing 
  542.  
  543.         to remember is that from one call to a function to the  next 
  544.  
  545.         call,  the  value of an automatic variable is not  preserved 
  546.  
  547.         and must therefore be reinitialized.
  548.  
  549.                          WHAT ARE STATIC VARIABLES?
  550.  
  551.              An  additional variable type must be mentioned at  this 
  552.  
  553.         point,  the "static" variable.  By putting the reserved word 
  554.  
  555.         "static"  in  front  of  a  variable  declaration  within  a 
  556.  
  557.         function,  the variable or variables in that declaration are 
  558.  
  559.         static  variables  and will stay in existence from  call  to 
  560.  
  561.         call  of  the  particular function.  
  562.  
  563.              By  putting  the  same reserved word  in  front  of  an 
  564.  
  565.         external variable, one outside of any function, it makes the 
  566.  
  567.         variable  private  and  not accessible to use in  any  other 
  568.  
  569.         file.  This implies that it is possible to refer to external 
  570.  
  571.         variables  in other separately compiled files,  and that  is 
  572.  
  573.         true.  Examples of this usage will be given in chapter 14 of 
  574.  
  575.         this tutorial.
  576.  
  577.                          USING THE SAME NAME AGAIN
  578.  
  579.              Refer  to  the  function named  "head2".   It  contains 
  580.  
  581.         another  definition  of the variable  named  "count".   Even 
  582.  
  583.         though  "count"  has  already  been  defined  as  a   global 
  584.  
  585.         variable,  it  is  perfectly all right to reuse the name  in 
  586.  
  587.         this  function.   It is a completely new variable  that  has 
  588.  
  589.         nothing to do with the global variable of the same name, and 
  590.  
  591.         causes  the  global  variable  to  be  unavailable  in  this 
  592.  
  593.         function.   This allows you to write programs using existing 
  594.  
  595.         functions  without worrying about what names were  used  for 
  596.  
  597.         variables in the functions because there can be no conflict.  
  598.  
  599.         You  only  need to worry about the variables that  interface 
  600.  
  601.         with the functions.
  602.  
  603.                         WHAT IS A REGISTER VARIABLE?
  604.  
  605.              Now  to  fulfill  a promise made earlier about  what  a 
  606.  
  607.         register  variable  is.   A  computer can  keep  data  in  a 
  608.  
  609.         register  or  in  memory.   A  register is  much  faster  in 
  610.  
  611.  
  612.  
  613.                                   Page 34    
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.                     Chapter 5 - Functions and variables
  624.  
  625.  
  626.         operation  than  memory  but there are  very  few  registers 
  627.  
  628.         available for the programmer to use.   If there are  certain 
  629.  
  630.         variables  that are used extensively in a program,  you  can 
  631.  
  632.         designate  that  those  variables  are to  be  stored  in  a 
  633.  
  634.         register  if possible in order to speed up the execution  of 
  635.  
  636.         the program.   Depending on the computer and the compiler, a 
  637.  
  638.         small  number  of register variables may be allowed and  are 
  639.  
  640.         designated  by putting the word "register" in front  of  the 
  641.  
  642.         desired variable.  Check your compiler documentation for the 
  643.  
  644.         availability  of  this  feature and the number  of  register 
  645.  
  646.         variables.   Most  compilers that do not have  any  register 
  647.  
  648.         variables available,  will simply ignore the word "register" 
  649.  
  650.         and run normally, keeping all variables in memory. 
  651.  
  652.              Register  variables  are only available  for  use  with 
  653.  
  654.         integer  and character type variables.   This may or may not 
  655.  
  656.         include  some  of the other integer-like variables  such  as 
  657.  
  658.         unsigned,  long, or short.  Check the documentation for your 
  659.  
  660.         compiler.
  661.  
  662.                         WHERE DO I DEFINE VARIABLES?
  663.  
  664.              Now for a refinement on a general rule stated  earlier.  
  665.  
  666.         When  you have variables brought to a function as  arguments 
  667.  
  668.         to  the  function,  they are defined immediately  after  the 
  669.  
  670.         function  name  and  prior  to the  opening  brace  for  the 
  671.  
  672.         program.   Other  variables used in the function are defined 
  673.  
  674.         at the beginning of the function,  immediately following the 
  675.  
  676.         opening  brace of the function,  and before  any  executable 
  677.  
  678.         statements. 
  679.  
  680.                          STANDARD FUNCTION LIBRARIES
  681.  
  682.              Every  compiler  comes  with some  standard  predefined 
  683.  
  684.         functions  which  are available for  your  use.   These  are 
  685.  
  686.         mostly   input/output   functions,   character  and   string 
  687.  
  688.         manipulation functions,  and math functions.   We will cover 
  689.  
  690.         most of these in subsequent chapters. 
  691.  
  692.              In addition,  most compilers have additional  functions 
  693.  
  694.         predefined that are not standard but allow the programmer to 
  695.  
  696.         get the most out of his particular computer.  In the case of 
  697.  
  698.         the  IBM-PC and compatibles,  most of these functions  allow 
  699.  
  700.         the  programmer  to use the BIOS services available  in  the 
  701.  
  702.         operating system,  or to write directly to the video monitor 
  703.  
  704.         or to any place in memory.  These will not be covered in any 
  705.  
  706.         detail  as  you will be able to study the unique aspects  of 
  707.  
  708.         your compiler on your own.  Many of these kinds of functions 
  709.  
  710.         are used in the example programs in chapter 14.
  711.  
  712.  
  713.  
  714.  
  715.  
  716.                                   Page 35    
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.                     Chapter 5 - Functions and variables
  727.  
  728.  
  729.                              WHAT IS RECURSION?
  730.  
  731.              Recursion  is another of those  programming  techniques 
  732.  
  733.         that  seem very intimidating the first time you come  across 
  734.  
  735.         it,  but  if  you will load and display the example  program 
  736.  
  737.         named RECURSON.C, we will take all of the mystery out of it.  
  738.  
  739.         This  is probably the simplest recursive program that it  is 
  740.  
  741.         possible  to write and it is therefore a stupid  program  in 
  742.  
  743.         actual  practice,  but for purposes of illustration,  it  is 
  744.  
  745.         excellent.
  746.  
  747.              Recursion  is  nothing more than a function that  calls 
  748.  
  749.         itself.   It is therefore in a loop which must have a way of 
  750.  
  751.         terminating.   In the program on your monitor,  the variable 
  752.  
  753.         "index"  is  set to 8,  and is used as the argument  to  the 
  754.  
  755.         function  "count_dn".   The function simply  decrements  the 
  756.  
  757.         variable, prints it out in a message, and if the variable is 
  758.  
  759.         not  zero,  it calls itself,  where it decrements it  again, 
  760.  
  761.         prints it,  etc.  etc. etc. Finally, the variable will reach 
  762.  
  763.         zero,  and the function will not call itself again. Instead, 
  764.  
  765.         it  will  return  to the prior time it  called  itself,  and 
  766.  
  767.         return  again,  until  finally it will return  to  the  main 
  768.  
  769.         program and will return to DOS.
  770.  
  771.              For  purposes  of understanding you can think of it  as 
  772.  
  773.         having 8 copies of the function "count_dn" available and  it 
  774.  
  775.         simply  called all of them one at a time,  keeping track  of 
  776.  
  777.         which  copy it was in at any given time.   That is not  what 
  778.  
  779.         actually  happened,  but it is a reasonable illustration for 
  780.  
  781.         you to begin understanding what it was really doing.
  782.  
  783.                               WHAT DID IT DO?
  784.  
  785.              A  better explanation of what actually happened  is  in 
  786.  
  787.         order.   When you called the function from itself, it stored 
  788.  
  789.         all  of the variables and all of the internal flags it needs 
  790.  
  791.         to  complete the function in a block  somewhere.   The  next 
  792.  
  793.         time it called itself,  it did the same thing,  creating and 
  794.  
  795.         storing  another  block of everything it needed to  complete 
  796.  
  797.         that  function call.   It continued making these blocks  and 
  798.  
  799.         storing them away until it reached the last function when it 
  800.  
  801.         started  retrieving the blocks of data,  and using  them  to 
  802.  
  803.         complete  each function call.   The blocks were stored on an 
  804.  
  805.         internal part of the computer called the "stack".  This is a 
  806.  
  807.         part  of  memory carefully organized to store data  just  as 
  808.  
  809.         described above.  It is beyond the scope of this tutorial to 
  810.  
  811.         describe the stack in detail,  but it would be good for your 
  812.  
  813.         programming  experience to read some material describing the 
  814.  
  815.         stack.   A stack is used in nearly all modern computers  for 
  816.  
  817.         internal housekeeping chores.
  818.  
  819.  
  820.  
  821.  
  822.                                   Page 36    
  823.  
  824.  
  825.  
  826.  
  827.  
  828.  
  829.  
  830.  
  831.  
  832.                     Chapter 5 - Functions and variables
  833.  
  834.  
  835.              In using recursion,  you may desire to write a  program 
  836.  
  837.         with  indirect recursion as opposed to the direct  recursion 
  838.  
  839.         described  above.    Indirect  recursion  would  be  when  a 
  840.  
  841.         function  "A"  calls the function "B",  which in turn  calls 
  842.  
  843.         "A",  etc.   This is entirely permissible,  the system  will 
  844.  
  845.         take  care of putting the necessary things on the stack  and 
  846.  
  847.         retrieving  them when needed again.   There is no reason why 
  848.  
  849.         you  could not have three functions calling each other in  a 
  850.  
  851.         circle,  or four,  or five,  etc.   The C compiler will take 
  852.  
  853.         care of all of the details for you.
  854.  
  855.              The thing you must remember about recursion is that  at 
  856.  
  857.         some  point,  something  must  go to  zero,  or  reach  some 
  858.  
  859.         predefined  point to terminate the loop.   If not,  you will 
  860.  
  861.         have  an  infinite  loop,  and the stack will  fill  up  and 
  862.  
  863.         overflow,  giving  you  an error and  stopping  the  program 
  864.  
  865.         rather abruptly.
  866.  
  867.                         ANOTHER EXAMPLE OF RECURSION
  868.  
  869.              The  program  named  BACKWARD.C is another  example  of 
  870.  
  871.         recursion,  so load it and display it on your screen.   This 
  872.  
  873.         program  is  similar to the last one except that it  uses  a 
  874.  
  875.         character array.  Each successive call to the function named  
  876.  
  877.         "forward_and_backward"  causes one character of the  message 
  878.  
  879.         to be printed.   Additionally,  each time the function ends, 
  880.  
  881.         one of the characters is printed again,  this time backwards 
  882.  
  883.         as the string of recursive function calls is retraced.
  884.  
  885.              Don't worry about the character array defined in line 3 
  886.  
  887.         or  the  other  new  material  presented  here.   After  you 
  888.  
  889.         complete chapter 7 of this tutorial,  this program will make 
  890.  
  891.         sense.   It  was  felt that introducing a second example  of 
  892.  
  893.         recursion was important so this file is included here.
  894.  
  895.              One additional feature is built into this program.   If 
  896.  
  897.         you observe the two calls to the function,  and the function 
  898.  
  899.         itself, you will see that the function name is spelled three 
  900.  
  901.         different  ways in the last few  characters.   The  compiler 
  902.  
  903.         doesn't  care how they are spelled because it only uses  the 
  904.  
  905.         first  8 characters of the function name so as far as it  is 
  906.  
  907.         concerned,  the function is named "forward_".  The remaining 
  908.  
  909.         characters  are simply ignored.   If your compiler uses more 
  910.  
  911.         that  8 characters as being significant,  you will  need  to 
  912.  
  913.         change  two  of  the  names  so that  all  three  names  are 
  914.  
  915.         identical. 
  916.  
  917.              Compile and run this program and observe the results.
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.                                   Page 37    
  925.  
  926.  
  927.  
  928.  
  929.  
  930.  
  931.  
  932.  
  933.  
  934.                     Chapter 5 - Functions and variables
  935.  
  936.  
  937.         PROGRAMMING EXERCISES
  938.  
  939.         1.   Rewrite  TEMPCONV.C,  from an earlier chapter, and move 
  940.  
  941.              the temperature calculation to a function.
  942.  
  943.         2.   Write a program that writes your name on the monitor 10 
  944.  
  945.              times by calling a function to do the writing. Move the 
  946.  
  947.              called function ahead of the "main" function to see  if 
  948.  
  949.              your compiler will allow it.
  950.  
  951.  
  952.  
  953.  
  954.  
  955.  
  956.  
  957.  
  958.  
  959.  
  960.  
  961.  
  962.  
  963.  
  964.  
  965.  
  966.  
  967.  
  968.  
  969.  
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.                                   Page 38    
  995.  
  996.