Thank you, Allin,
Everything is clear now for me
with this issue.
You have got me right with efficiency
issues and "const" modifier advice
is very helpful.
Oleh
25 листопада 2018, 23:29:20, від "Allin Cottrell" <cottrell(a)wfu.edu>:
On Sun, 25 Nov 2018, oleg_komashko(a)ukr.net wrote:
Dear all,
To reproduce
nulldata 100
set seed 13
series serie = cum(normal())
eval mean(serie)
eval serie[1]
function series sc_sp (series *serie)
return (serie - mean(serie))/sd(serie) + 1
end function
function void funzione(series *serie)
serie = sc_sp(&serie)
comando = "arima 1 0; serie"
@comando -q
end function
funzione(&serie)
eval mean(serie)
eval serie[1]
No non-void function was called from the main script.
but serie become standardized and shifted by one
Not a bug. If you pass a series to a function in "pointer" form, you
must expect that it will (or at least, may) be modified when the
function exits, even if there's no explicit return value. The two
layers of functions in the example above are not required for this
effect; here's a shorter version:
<hansl>
nulldata 100
set seed 13
function void funzione (series *serie)
serie = (serie - mean(serie))/sd(serie) + 1
comando = "arima 1 0; serie"
@comando -q
end function
series serie = cum(normal())
eval mean(serie)
eval serie[1]
funzione(&serie)
eval mean(serie)
eval serie[1]
</hansl>
As a function writer, if you want the efficiency of receiving an
argument "by reference" (without the overhead of copying) but do not
intend to modify the argument, you can use the "const" modifier
instead of pointer form. But then, of course, you can't modify the
argument. The following function would give you "serie" without
copying, but would provoke an error:
function void funzione (const series serie)
# Illegal modification of const argument!
serie = (serie - mean(serie))/sd(serie) + 1
comando = "arima 1 0; serie"
@comando -q
end function
So in the case under consideration one would use neither the
"pointer" nor the "const" form:
function void funzione (series serie)
serie = (serie - mean(serie))/sd(serie) + 1
comando = "arima 1 0; serie"
@comando -q
end function
And "serie" would not be modified in the caller.
Allin
_______________________________________________
Gretl-devel mailing list
Gretl-devel(a)lists.wfu.edu
http://lists.wfu.edu/mailman/listinfo/gretl-devel