Page 1 of 1

Subroutine Local Program Vars

Posted: Fri Oct 20, 2023 9:26 am
by madm
Any chance we will get program variables scoped locally to a subroutine in a future EViews release? One source of bugs in our more complicated EViews programs is where strings or scalars defined in one subroutine nameclash with the same name defined in a subroutine that it calls. Example:

Code: Select all

logmode l

subroutine a(scalar !sum)
  !sum = 0
  for !k= 1 to 10 '!k is used for a different purpose in a than in b
    !sum = !sum + !k
  next
endsub

subroutine b
  !k= 1
  !sum = 0
  call a(!sum)
  logmsg k={!k} sum={!sum} 'Will display 10 instead of 1 for !k
endsub

call b


Specifically what I would like to see is a syntax like the following to be able to declare some program vars as being local to their subroutine:

Code: Select all

logmode l

subroutine a(scalar !sum)
  local !k ' <===== LOCAL PROGRAM VAR DECLARATION HERE THAT WON'T REFERENCE THE GLOBAL VARIABLE DEFINED IN b
  !sum = 0
  for !k = 1 to 10
    !sum = !sum + !k
  next
endsub

subroutine b
  !k= 1
  !sum = 0
  call a(!sum)
  logmsg k={!k} sum={!sum} 'Will display 10 instead of 1 for !k
endsub

call b


Would something like this be possible? I would imagine that it's non-trivial because it requires keep a local variable stack for each subroutine invocation.

Re: Subroutine Local Program Vars

Posted: Fri Oct 20, 2023 9:34 am
by EViews Gareth
Tricky to implement the hybrid approach between local subroutines and local variables.

Of course in your trivial examples, local subroutines will do what you want, but presumably your actual code is a tad more complicated.

Re: Subroutine Local Program Vars

Posted: Sat Oct 21, 2023 12:01 pm
by madm
EViews Gareth wrote:Tricky to implement the hybrid approach between local subroutines and local variables.

Of course in your trivial examples, local subroutines will do what you want, but presumably your actual code is a tad more complicated.


Thanks. Yes, more complicated although not necessarily that much more complicated. I'll have to give it more thought but I don't think local subroutines are helpful in this case because often the context is that we are generating or transforming data in a workfile. The most common nameclashes I see are from using the same counter variables across nested subroutines call (!i, !j, !k, !x, !y, %s, %v etc.)