Re: Re: scope of a variable

From: Dennis Shea (shea AT cgd.ucar.edu)
Date: Tue Sep 14 2004 - 16:33:29 MDT

  • Next message: Takeshi Enomoto: "Re: Re: scope of a variable"

    NCL's scoping rules for are identical to Pascal.

    http://ngwww.ucar.edu/ngdoc/ng/ref/ncl/NclStatements.html
    See section: "The local statement and function and procedure scope"
    -----

    Sometimes an example is worth a thousand 'official' words. :-)

    Let COMMON.ncl be a file that contains frequently used constants.
    Specifically, the file's contents are:

    ; COMMON.ncl ; constants to be made glocally available
                                   ; use unique [uncommon] names
          GRAV_C = 9.80665 ; gravity at 45 deg lat used by the WMO
          OMEGA_C = 7.292e-05 ; earth's angular vel
          RE_C = 6.37122e06 ; average radius of earth
          PI_C = 4.*atan(1.)
          RAD_C = PI_C/180.

    This file contains no 'begin/end' statements

    Now consider the following script:

    load "./COMMON.ncl" ; contains defined constants

       foo1 = 1 ; more demo constants
       foo2 = 2
       foo3 = 3

    function coriolis (angle:numeric)
    begin
      return (2.*OMEGA_C*sin(angle*RAD_C)) ; use common variables
    end

    procedure demo_scope()
    local foo2 ; only available locally
    begin
       foo2 = 999 ; local foo2
       foo3 = -123 ; foo3 not declared local: change previous value
    end

    begin
      print ("main script: GRAV_C="+GRAV_C)

      angle = 60.
      cp = coriolis(angle) ; use variables located in common
      print ("cp="+cp)

      demo_scope()
      
      print ("foo1="+foo1+" foo2="+foo2+" foo3="+foo3)
    end

    ==== The output will be

    (0) main script: GRAV_C=9.80665
    (0) cp=0.000126301
    (0) foo1=1 foo2=2 foo3=-123

    ==== comments

    (a) All of COMMON.ncl, foo1, foo2 and foo3 are available to
        code that follows. Eg, GRAV_C is available to the main
        and all other units.
        
    (b) cp is calculated using constants in COMMON.ncl

    (c) in 'demo_script' foo2 is declared local.
        Setting foo2=999 is effective within the demo_script procedure.
        Upon exit, the local foo2 is deleted. It is only in existence
        for the duration of demo_script. The global foo2 is
        unchanged.
        
        foo3 is not declared local to demo_script. It exists previously
        so resetting foo3 to a new value will be 'permanent' unless
        it is subsequently reset.
        

    Hope this helps!

    D
    >
    >Hi,
    >
    >On 2004/09/13, at 19:18, Takeshi Enomoto wrote:
    >
    >
    >> In fact a is not found.
    >
    >With an additional script,
    >$ cat vars.ncl
    >begin
    > a = 0.
    >end
    >
    >loaded to func.ncl, variable a is found.
    >
    >> $ cat func.ncl
    >> undef("myfunc")
    >>
    >> function myfunc(x)
    >> begin
    >> return a*x
    >> end
    >>
    >> $ cat main.ncl
    >> load "./func.ncl"
    >>
    >> begin
    >> a = 5.
    >> print(myfunc(3.))
    >> end
    >
    >
    >
    >-----
    >Dr. Takeshi Enomoto
    >eno@jamstec.go.jp
    >Earth Simulator Center
    >
    >_______________________________________________
    >ncl-talk mailing list
    >ncl-talk@ucar.edu
    >http://mailman.ucar.edu/mailman/listinfo/ncl-talk

    _______________________________________________
    ncl-talk mailing list
    ncl-talk@ucar.edu
    http://mailman.ucar.edu/mailman/listinfo/ncl-talk



    This archive was generated by hypermail 2b29 : Tue Sep 14 2004 - 16:30:35 MDT