Recent

Author Topic: variable not initialized....  (Read 1340 times)

Prime

  • Jr. Member
  • **
  • Posts: 77
variable not initialized....
« on: November 16, 2021, 10:32:44 am »
Hi all,

At several places in my code I have variables, that are passed un-initialized to functions / procedures as var parameters, and recieve a valid value
returened in them which I subsequently use on returning.

However, this seems to cause a warning, is there a (correct) way of dealing with this?

Cheers.

Phill.

Prime

  • Jr. Member
  • **
  • Posts: 77
Re: variable not initialized....
« Reply #1 on: November 16, 2021, 10:43:34 am »
OK Replying to myself, declaring the parameter as OUT rather than VAR works for my own code where the called parameter only passes a value back.

But howabout things like :

Code: [Select]
PROCEDURE Process(Mem          : TMemoryStream);

VAR Current     : BYTE;

BEGIN;
  Mem.Read(Current,1);

Cheers,

Phill.

wp

  • Hero Member
  • *****
  • Posts: 13555
Re: variable not initialized....
« Reply #2 on: November 16, 2021, 10:52:03 am »
But howabout things like :
Code: Pascal  [Select][+][-]
  1. PROCEDURE Process(Mem : TMemoryStream);
  2. VAR Current: BYTE = 0;
  3. BEGIN;  // !!! No semicolon here!
  4.   Mem.Read(Current,1);
  5.  
Initialize the variable. Usually it is convenient to do this in its declaration.

In case of large records, call the Default(RecordType) function:
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyRecord = record
  3.     x, y: Integer;
  4.     s: String;
  5.     b: array[0..31] of Boolean;
  6.   end;
  7. var
  8.   rec: TMyRecord;
  9. begin
  10.   rec := Default(TMyRecord);
  11.   Memo.Read(rec, SizeOf(rec));
 

 

TinyPortal © 2005-2018