Re: coerced parameter ........

From: Mary Haley <haley_at_nyahnyahspammersnyahnyah>
Date: Wed Feb 23 2011 - 09:03:17 MST

The best way to demonstrate what this document means is with an example.

Basically, if you define an NCL function where one of the input values gets changed, and you call this function with a value that is not the type that the function expects, then the function will try to coerce the value to the correct type, but it won't allow the value to be changed once it comes back out of the function.

Consider the NCL script below. The function "addit" simply adds a constant value to a 1D float array, "x". "x" is changed inside the function, so it should show up as changed in the calling code as well.

However, if you pass an integer 1D array to "addit", you will get a warning and "x" will NOT be changed. NCL can't safely coerce the input float values to an integer array without possibility losing precision.

Try running the code below, and try uncommenting one of the "ispan" lines below, to change "y" from a float to an integer. You will see that "y" will not be affected after "addit" is called, if it went in as an integer.

--Mary

;---This function adds a constant to x, and attempts to change x directly.
function addit(x:float[*],constant[1]:numeric)
begin
  x = x + constant
  return(x)
end

begin
 y = ispan(1,10,1) ; y is an integer
; y = ispan(1,10,1)*1. ; y is a float

;
; If "y" is an integer, it will be coerced to a float
; for the "addit" function, AND the "y" values will
; not be changed after the function is called.
;
; You will also see this warning:
; warning:Argument 0 of the current function or procedure was
; coerced to the appropriate type and thus will not change if the
; function or procedure modifies its value
;
; If "y" is a float", the values will be changed after the
; function is called.
;
  print("before 'addit': y = " + y)
  z = addit(y,10)
  print("------------------------------------------------------")
  print("y and z should be the same, unless y had to be coerced")
  print("------------------------------------------------------")
  print("after 'addit': y = " + y + " (z = " + z + ")")
end

On Feb 23, 2011, at 5:31 AM, gibies george wrote:

> I have read the following information in NCL Reference Manual
> http://www.ncl.ucar.edu/Document/Manuals/Ref_Manual/NclExpressions.shtml#Functions
> _______________________________________________________________
>
> One very important thing to note is that all parameters are passed by reference in NCL, meaning a change of a value in a variable within the function changes the value of the parameter.
>
> However, if a parameter is coerced before the function is called, changes within the function will not be reflected in the parameter, because coercion can only be applied in a single direction. A warning message is given in this instance.
>
> ________________________________________________________________
>
> Can anybody give details about the statement ".... if a parameter is coerced....."
>
> Thanking you in advance.
>
> --
> Gibies George, CSIR-RF,
> Climate and Global Modelling Division,
> Indian Institute of Tropical Meteorology,
> Dr. Homi Bhabha Road,
> NCL (P. O.), Pashan,
> Pune 411008, India.
>
> http://sites.google.com/site/gibiesge/
>
> Think about the environment. Save paper; Save Trees. Please don't print this e-mail unless it is necessary.
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Wed Feb 23 09:03:23 2011

This archive was generated by hypermail 2.1.8 : Wed Feb 23 2011 - 16:47:57 MST