My understanding is that if a procedure or function parameter is to both convey information on entry and potentially be modified on exit, then it must be declared as var.
My understanding is also that if a procedure or function parameter is to convey information on entry but will not be modified, then it should be declared as const.
What is the correct declaration of a parameter which will not be changed by anything the function does, but may be changed asynchronously by a unix-style signal?
C/C++ would call it volatile, and the statements above would imply that it should be const, but would this guarantee that even a simple variable would be passed by address rather than being copied?
(* Wait until we can be reasonably confident that we're not still in the middle
of an input line.
*)
procedure WaitInputGap(handle: TSerialHandle; var termFlag: boolean);
var
avail: integer;
begin
SerFlushInput(handle);
repeat
if fpIoctl(handle, FIONREAD, @avail) <> 0 then
break;
Sleep(50);
SerFlushInput(handle)
until (avail = 0) or termFlag
end { WaitInputGap } ;
As a subsidiary question and noting that I think this was discussed a few weeks ago, is it possible for a program's main unit to expose the termination flag as a variable or (better) a function or (best) a read-only unit property?
MarkMLl
if I understand your post...
You are concerned about a variable changing while you are handling the last value of it within a function from the parameter entry ?
if that is the case, you don't want to use CONST, CONSTREF, Var or OUT because CONST will free for the compiler to optimize the code so it could be what ever the compiler see's fit when it constructed the function.
CONSTREF ensures that it's always a pointer (VAR) referenced but you still can only read from it.
and of course VAR and OUT is a pointer reference.
If you want a value that remains constant within the function then just pass it directly with NO CONST, VAR or OUT.. All simply types will be Register or stacked pushed and there for remain constant within your function and you are free to change it of course
If you are in a case where you need a locked value and also need to write back then you need to pass it as two parameters, one as I have said to prevent change and the other as a VAR so you can write back
in C/C++ the Volatile is only a compiler optimizer to instruct the compiler that the value may change so it needs to may a choice about how it references it, from a Register loaded once or always referenced from the original source. This depends on the code, for example a control loop that must not have its value changed during the intrinsic of the loop etc..