On Wed, 3 Mar 2021, atecon wrote:
Am 03.03.2021 15:38 schrieb Allin Cottrell:
>
> That's right. "const" doesn't have any effect when placed in front
of
> (non-pointerized) scalar arguments of any sort. Illustration:
>
> <hansl>
> function void simple (const int N[2::])
> printf "very simple (%d)\n", N
> # no error flagged here
> N = 24
> end function
Ah, I never checked that but simply assumed that the const qualifier would
flag an error inside a function.
> function void dimple (const matrix m)
> printf "very dimple\n"
> # this does give an error
> m = m + 5
> end function
>
> k = 7
> simple(k)
> print k # note: k is still 7
> dimple(I(3))
> </hansl>
>
> I guess this is a buglet: either "const" should be disallowed for
> plain scalar arguments, or else it should prevent changing the
> argument inside the function (although the caller will not inherit a
> change in any case).
Yes, either case I guess. Personally I would favor that const is allowed for
all data types (but there may be good reason why not to do so).
It's not so much that there's good reason NOT to do so -- rather
that there's little or no good reason to do so. Scalars are always
passed by value unless they're explicitly pointerized. So if you
mark a plain scalar argument as const, you're just tying your hands
as a function writer.
But... I guess in some contexts it could make sense as a guard
against forgetfulness: e.g. you get a scalar n as argument, then
forget that fact and assign a different value to n for some local
purpose, thereby losing track of the input value. (This should be
pretty much impossible in tightly modular code, but could be an
issue with a function of many lines.)
Allin