On 10.04.2015 01:29, Joep van Delft wrote:
> [...]
>
> The question is: How to make a function "productive" on an array
> (possibly complicated by the repeated advice not to abuse global
> variables). I do not believe that this question is explicitly
> answered in the documentation. This is one of the few places where my
> lack of formal CS education has hindered me locating the conclusive
> hint from the documentation.
>
> Rereading, the most conrete clue is:
>
> [W]hen arrays are the parameters to functions, they are not
> copied. Instead, the array itself is made available for
> direct manipulation by the function. This is usually termed
> call by reference. Changes made to an array parameter inside
> the body of a function are visible outside that function.
>
> NOTE: Changing an array parameter inside a function can be
> very dangerous if you do not watch what you are doing. (...)
>
> What I take from this: *DANGER ZONE*.
There's a "concept" in CS and programming that's called "side effects";
and the general wisdom is to avoid "side effects" in programming.
The point is, though, that awk is full of side effects built into the
language _by design_ (global access to $0, $1, etc., global variables
like FS, RSTART, etc.)
The warning that you found in the documentation (as I suspect) is to
make folks explicitly aware that there's a cal-by-reference semantics
for arrays, and changing an array will affect the respective array in
the calling context. No more and no less; really nothing to get excited
about. :-)
A "safe" way in that respect is using any of the Functional Languages.
But prevalent imperative languages often allow such Side Effects. You
should certainly know what you're doing.
In awk, since it's a (UI wise) terse language, it may not be obvious
that simple types and arrays are passed with different concepts to a
function; there's no specific syntactic construct (like C++'s '&', or
C's '*', or Pascal's 'var'). Therefore an explicit hint, or warning,
is not bad to be present in the documentation.
> It would have saved me a trip
> to $SEARCH_ENGINE and some experimentation if the official,
> recommended way to `return an array' would be explicit (-ly
> referenced) here.
(I don't understand what you are saying in that sentence.)
Janis
>
>
> Kind regards,
>
>
> Joep
>