Re: Help: "local list" does not work in defining a new function?

From: Dennis Shea <shea_at_nyahnyahspammersnyahnyah>
Date: Sun Apr 08 2012 - 10:50:53 MDT

Arguments to NCL functions are 'passed by reference'. (Same as fortran)
This means if you change a value in the function or procedure, it
will be changed upon return to the calling code.

If you do not want this to happen, make a temporary copy and operate
on the copy.

;------------------------l------xf-----------------------
; Contour the number of the integers of the input numeric value.
undef ("numInt")
function numInt(int[1]:integer)
local intLoc,numI
begin
    intLoc = int ; duplicate
    numI = 0
    do while (intLoc .ne. 0)
       intLoc = intLoc/10
       numI = numI+1
    end do

    return(numI)
end

;---------------
; Main program
begin
   INT= 1980
   print(INT) ; The value in the main program is 1980!
   num_nt = numInt(INT) ; Call a function.
   print (" Number of the integers of "+INT+" is "+num_nt)
   print(INT) ; The value changed to 0. Oberviously, it is
changed by the local variable in function "numInt".
end

On 4/7/12 11:58 PM, heaven_bird334 wrote:
> Hi,
> Today I wrote a function "numInt( int[1]: integer)" to count the number
> of an integer number "int".
> I have placed the variable "int" into the local list in this
> self-defined function.However, the "local list" seems not working for
> me, because my self-defined function("numInt") changed the value of
> "int" , and assigned that new value back to the variable "INT" in
> themain program. This means, I will definitely get a wrong result if I
> usethe variable "INT"in the main program a second time after calling the
> function "numInt( int[1]: integer)" .
>
> I simplified the script and attached it behind this letter. The detailed
> information are also as the following:
>
> >ncl -x t.local.ncl
> Copyright (C) 1995-2011 - All Rights Reserved
> University Corporation for Atmospheric Research
> NCAR Command Language Version 6.0.0
> The use of this software is governed by a License Agreement.
> See http://www.ncl.ucar.edu/ for more details.
> + ;------------------------l------xf-----------------------
> + ; Contour the number of the integers of the input numeric value.
> + undef ("numInt")
> + function numInt(int[1]:integer)
> + local int,numI,intTem ; Placing the local variable's name "int" in the
> local list !
> + begin
> + numI=0
> + do while (int .ne. 0)
> + int = int/10
> + numI = numI+1
> + end do
> +
> + return(numI)
> + end
> +
> + ;---------------
> + ; Main program
> + begin
> + INT= 1980
> + print(INT) ; The value in the main program is 1980!
> + num_nt = numInt(INT) ; Call a function.
> + print (" Number of the integers of "+INT+" is "+num_nt)
> + print(INT) & nbsp; ; The value changed to 0. Oberviously, it is
> changed by the local variable in function "numInt".
> + end
>
>
> Variable: INT
> Type: integer
> Total Size: 4 bytes
> 1 values
> Number of Dimensions: 1
> Dimensions and sizes:[1]
> Coordinates:
> (0)1980
> (0)Number of the integers of 0 is 4
>
>
> Variable: INT
> Type: integer
> Total Size: 4 bytes
> 1 values
> Number of Dimensions: 1
> Dimensions and sizes:[1]
> Coordinates:
> (0)0
> +
> +
>
> Any suggestions or comments are appreciated.
>
> Best Regards,
> Li
>
>
>
>
>
>
> _______________________________________________
> 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 Sun Apr 8 10:51:03 2012

This archive was generated by hypermail 2.1.8 : Mon Apr 09 2012 - 13:43:03 MDT