hockchai Lim wrote:
Hey Simon,
care to explain a bit more about the different between these three
prototype?
D proc1 pr
D data * const
D proc1 pr
D data * value
D proc1 pr
D data *
If I may jump in ...
* const: a pointer passed by reference. The called procedure is not
expected to change the parameter. Coding CONST is a promise from the
RPG programmer to the RPG compiler that the called procedure will not
change the parameter. If the called procedure is an RPG procedure, the
RPG compiler will enforce this.
* value: a pointer passed by value. If the called procedure changes the
parameter, the caller will not see the change.
* : a pointer passed by reference. If the called procedure changes the
parameter, the caller will see the change.
When a parameter is passed "by reference" means that the actual thing
passed to the called procedure is a pointer to the parameter. The
called procedure uses the pointer as a basing pointer under the covers.
The called procedure has direct access to the storage in the caller.
(For CONST parameters, it may be the storage of a compiler temporary in
the caller rather than the actual parameter coded on the call.)
When a parameter is passed "by value", the actual thing passed to the
called procedure is a copy of the parameter.
This can be confusing with pointer parameters. It's sometimes easier to
understand with non-pointer types first, then with that understanding,
mentally substitute a pointer.
Consider:
D proc pr
D constRefParm like(x) const
D refParm like(x)
D valueParm like(x) value
Say x is an integer. The called procedure gets passed a pointer, a
ponter and an integer.
In the called procedure, _under_the_covers_, the procedure access its
parameters like this (call the first pointer p1 and the second pointer p2):
D constRefParm s 10i 0 based(p1)
D refParm s 10i 0 based(p2)
D valueParm s 10i 0
Now, say x is a pointer. It's exactly the same except instead of 10i we
have *.
D constRefParm s * based(p1)
D refParm s * based(p2)
D valueParm s *
The only difference between constRefParm and refParm is that the called
procedure is not allowed to change constRefParm.
As an Amazon Associate we earn from qualifying purchases.