home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / PASCAL / PASTUT34 / PROCFUNC.TXT < prev    next >
Text File  |  1993-06-12  |  4KB  |  87 lines

  1.                 PROCEDURES AND FUNCTIONS. 
  2.                 ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
  3.  
  4.  Procedures and functions allow additional blocks to be nested in the main
  5.  program block.  Each procedure or function declaration has a heading
  6.  followed by a block.  A procedure is activated by a procedure statement; a
  7.  function is activated by the evaluation of an expression that contains its
  8.  call and returns a value to that expression. 
  9.  
  10.  A procedure heading consists of the reserved word PROCEDURE, followed by
  11.  the procedure identifier (or name) and followed optionally by any formal
  12.  parameters.  The procedure body consists principally of a block of Pascal
  13.  statements, but also can include assembler code and reserved words like
  14.  NEAR and FAR (to be discussed later). 
  15.  
  16.  A function heading consists of the reserved word FUNCTION, followed by the
  17.  function identifier, an optional parameter list, a colon and then the
  18.  result type.  The function body consists principally of a block of Pascal
  19.  statements, but also possibly assembler code and other reserved words. 
  20.  
  21.  For both procedures and functions, each parameter declared in a formal
  22.  parameter list is local to the procedure or function being declared and can
  23.  be referred to by its identifier in the block associated with the procedure
  24.  or function. 
  25.  
  26.  The formal parameter list is enclosed in parentheses and concluded by a
  27.  semi-colon.  There are three kinds of parameter: value, variable and
  28.  untyped variable. 
  29.  
  30.  A parameter group without a preceding VAR and followed by a type is a list
  31.  of value parameters (when the value of the parameter is changed it only
  32.  affects a temporary store and not the actual variable outside the
  33.  procedure). 
  34.  
  35.  A parameter group preceded by VAR and followed by a type is a list of
  36.  variable parameters.  Such parameters are called reference parameters and
  37.  changes made to the variable within the procedure remain even after the
  38.  procedure has ended. 
  39.  
  40.  A parameter group preceded by VAR and not followed by a type is a list of
  41.  untyped variable parameters. 
  42.  
  43.  See Chapter 8 of the Programmer's Guide (pp 95- 112) for further details.
  44.  
  45.  A simple example of a procedure is illustrated in the program PROC.PAS
  46.  which has a procedure TimesTable, which lists on the screen any chosen
  47.  part of a multiplication table, for multiplicands and multipliers all less
  48.  than 100. This procedure is listed below:
  49.  
  50.  procedure TimesTable(BaseValue,Lower,Upper: integer);
  51.  begin
  52.    writeln;
  53.    writeln(BaseValue,' Times Table');
  54.    writeln;
  55.    For i:=Lower to Upper do writeln(BaseValue:2,' * ',i:2,' = ',BaseValue*i:5);
  56.  end;
  57.  
  58.  In the main part of the program, the user is asked for values of the three
  59.  parameters and then effectively the procedure is called as follows:
  60.  
  61.        TimesTable(57,10,20);
  62.  
  63.  which then displays   57 * 10 = 570
  64.                        57 * 11 = 627
  65.               etc. to  57 * 20 = 1140
  66.  
  67.  An example of a function is illustrated in the program FUNC.PAS which
  68.  defines a function as follows:
  69.           function Power(Base, Exponent : real) : real;
  70.           begin
  71.              .....
  72.              Power := exp(Exponent * ln(Base))
  73.              .....
  74.           end;
  75.  
  76.  This enables the evaluation of any number to any power, provided the
  77.  result does not involve complex numbers. Thus if number is negative
  78.  and power is fractional, a warning is given.
  79.  
  80.  The function is effectively called as for example:   Result := Power(2,3);
  81.  although the example program involves a user interaction for the values.
  82.  
  83.  
  84.  PROCFUNC.TXT
  85.  14.1.93
  86.  
  87.