Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Turbo Pascal - <b> procedure declaring and using a subroutine pp 131</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 PROCEDURE             Declaring and Using a Subroutine               pp 131

 Syntax:  Procedure Name (Formal Parm List) ;

 Type:    All types

 Form:    Declaration

 Purpose: A procedure defines a subroutine.  It may be either pre-declared
          as a part of the TURBO system, or user declared as defined by
          the programmer.

          The reserved word 'Procedure' is followed by an identifer which
          becomes the name of the procedure, optionally followed by a
          list of formal parameters.

          All identifiers in the formal parameter list and the declaration
          part of the procedure are local to the procedure.
          These identifiers are not visible outside the procedure.
          This is termed the scope of the identifier.

 Parms:   The formal parameters are used to define the procedure.  The
          names assigned to them are only identifiers, not actual variables.
          The formal parameters only identify the number and type of
          variables that will be used by the procedure.

          Define:  Procedure BOX (X1,Y1,X2,Y2 : Integer) ;
          Usage:   BOX (1,1,10,10) ;   or    BOX (Col,Row,Col2,Row2) ;

          This example indicates to the compiler that when BOX is called,
          it will be called with four parameters of type Integer.


 Types:   Formal parameters can be of two types:  Value or Variable.
          Value parameters are constants or actual values of variables.
          The BOX procedure uses Value parameters in both examples.
          The first instance uses constants and the second instance copies
          the values from the named variables and passes them when the
          procedure is called.


          Variable parameters are the actual addresses of the variables.
          The variables passed when the procedure is called will be
          acted upon by the procedure.

          Define:  Procedure SWAP (VAR Dummy1, Dummy2 : Integer) ;
                Var
                   Temp   : Integer ;  { Local scope temporary variable }
                Begin
                   Temp   := Dummy1 ;  { Assign 1st var to local Temp   }
                   Dummy1 := Dummy2 ;  { Assign 1st var = 2nd var       }
                   Dummy2 := Temp   ;  { Assign 2nd var = 1st var       }
                   End              ;

          Usage:   SWAP (Hi,Lo) ;

          Dummy1, Dummy2 are Variable formal parameters.  They indicate
          that when SWAP is called, the two parms passed with the procedure
          will be actual variable addresses.  The two variables will then
          be acted upon by the procedure.
 Examples:
       TYPE
          Str80   = String [80];    { Declare a string type of 80 bytes     }
          Str01   = String [01];    { Declare a string type of 01 bytes     }
       CONST
          Int1    : Integer = 1;    { Initialize Integer type to 1          }
          Int2    : Integer = 2;    { Initialize Integer type to 2          }
       VAR
          Str1    : Str80      ;    { Declare variable of type String [80]  }
          Str2    : Str01      ;    { Declare variable of type String [01]  }


          (* Variables Int1,Int2 addresses
             are passed and Int1 is changed *)

       PROCEDURE VarChange (VAR Addr1,Addr2 : Integer);
          Begin
          Addr1 := Addr1 + Addr2;   { Addr1 formal parm is Int1 address     }
          End;


          (* Variable Int1, Int2 copies are
             passed.  Int1, Int2 not changed *)

       PROCEDURE VarCopy (Copy1,Copy2 : Integer);
          Begin
          Copy1 := Copy1 + Copy2;   { Only local variable Copy1 is changed  }
          End;


       PROCEDURE StrChange (VAR Parm1 : Str80 ; Parm2 : Str01);
          Begin
          Parm1 := Parm1 + Parm2;   { Only local variable Parm1 is changed  }
          End;


       PROCEDURE StrCopy (Copy1 : Str80 ; Copy2 : Str01);
          Begin
          Copy1 := Copy1 + Copy2;   { Only local variable Copy1 is changed  }
          End;

       BEGIN
          VarCopy   (Int1,Int2);    { Copy Int1,Int2 values to procedure    }
          VarChange (Int1,Int2);    { Pass Int1,Int2 addresses to procedure }
          StrCopy   (Str1,Str2);    { Copy Str1,Str2 strings to procedure   }
          {$V+}                     { Strict type checking enabled          }
          StrChange (Str2,Str2);    { Error 44 - Types don't match          }
          {$V-}                     { Turn off strict type checking         }
          StrChange (Str1,Str2);    { Mismatched types will now pass OK     }
       END.

See Also: External Forward Function Inline

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson