home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.pascal.borland      Borland Pascal was actually pretty neat      2,978 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 2,027 of 2,978   
   Dr Engelbert Buxbaum to All   
   Re: Variables   
   25 Jan 06 18:48:48   
   
   From: engelbert_buxbaum@hotmail.com   
      
   > I make an example: ;o)   
   >   
   > Procedure subCalculate;   
   > Var:   
   >   SubResult: integer;   
   > Begin   
   >   some code which ends with   
   >   SubResult:= xxx (a number)   
   > End;   
   >   
   > Procedure Calculate;   
   > Var   
   >   Result:: integer;   
   > Begin   
   > In this procedure i want to use the variable "SubResult" fron the procedure   
   > subCalculate like this.   
   > Result:= SubResult + 1;   
   > End;   
   >   
   > How do i do that.   
   >   
   >   
      
   First, if you want to return a single number as result, it is better to   
   use a function than a procedure.   
      
   Second, a variable declared inside a function or procedure is visible   
   only within, any reference from outside will cause an error message from   
   the compiler. If a variable should be visible in several subroutines, it   
   needs to be declared in the next higher block, this may be the program   
   or unit or even a subroutine that contains both local subroutines, e.g.   
      
   procedure AAA;   
      
   var Subresult : real;   
      
      function SubCalculate (x, SubResult : integer) : integer   
      
      begin   
         { do something }   
         SubCalculate := SomeNumber;   
      end;   
      
      
      procedure Calculate (x : integer; var Result, SubResult : integer);   
      
      begin   
         Result := Subcalculate(x) + 1;   
         {or better: Calculate := succ(SubCalculate(X));}   
      end;   
      
   begin   
       { call your local routines }   
   end;   
      
   begin { main program or unit }   
   end.   
      
      
   Access to variables not in the parameter list of a subroutine is bad   
   programming style. If SubResult is changed in any of the subroutines, it   
   should be a var-parameter, meaning that subroutine should be a procedure   
   (because more than one parameter is changed). If it is only used, but   
   not changed, it should be a value parameter (that is, without the var in   
   front).   
      
   This is not purely an academic question, side-effects (assignments to   
   variables not mentioned in the parameter list) clutter the logic of a   
   program and can be the cause of hard to trace errors. Use of variables   
   not in the parameter list is not quite as bad, but still not good style.   
      
   Note that in the above example, declaration of SubResult in the   
   declaration block of AAA may not be required unless it is refered to in   
   its body.   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca