CBFalconer wrote:
Prof Abimbola Olowofoyeku wrote:
On 13 Oct 01, at 18:00, CBFalconer wrote:
[...]
I am new here, but an old hand at Pascal and Pascal implementation.
Will somebody please explain why this 'const' extension exists at all? A non VAR parameter is always passed by value, so the original cannot be modified.
It's not about whether the original (i.e., the actual parameter), but rather the formal parameter in the routine can be modified.
Thus I see absolutely no purpose to it.
There is a point. 'const' parameters are a Borland extension that allows you to pass a parameter by reference, but in a read only format.
In fact, passing by reference is *not* guaranteed (just like it isn't in BP).
I see. Seems like the wrong way, to me. One more Borland attempt to C-ify the language.
Maybe, but OTOH, Extended Pascal has something very similar, viz `protected' and `protected var' parameters. The only difference is that passing by value or reference, respectively, is guaranteed there.
My approach (I think) would be to check the usage within the function block, and if not modified and the size is above the size of a pointer, convert the usage to a VAR.
That's not quite the same. By declaring a parameter `protected' or `const', you are telling the compiler that you don't want to modify it, and if you ever do it accidentally, it should tell you about it, rather than just changing its way of passing.
There's another problem: In a module (or unit) interface, the compiler doesn't know the implementation code yet. It therefore would always have to assume it might be modified, and could never optimize it by passing it by reference.
I suggest to use `protected [var]' if
- you want to be standard-conformant,
- you need to interface a C routine (ironically, though BP is usually closer to C, its `const' can't be used there because you can't be sure if it will pass the variable by value or reference, cf. the comments in the beginning of gpc.pas), or
- it makes a real difference (apart from performance) whether is it passed by value or reference (this can happen if the actual parameter is modified from the routine which is, of course, bad practice, and should be avoided generally, anyway).
In other cases, I usually use `const' to let the compiler pick the most efficient way of passing (e.g., for a small record, passing by value might be more efficient, but when I add a few fields later, it will be more efficient to pass it by reference; I don't want to have to bother about that).
This in turn means conformant arrays have to be VAR (which I vaguely recall is already the specification)
ISO 7185, 6.6.3.6 e) mentions both value and variable conformant-array-specification.
Frank