Subroutine Local Program Vars

For making suggestions and/or requests for new features you'd like added to EViews.

Moderators: EViews Gareth, EViews Moderator

madm
Posts: 12
Joined: Mon Oct 23, 2017 1:13 pm

Subroutine Local Program Vars

Postby madm » Fri Oct 20, 2023 9:26 am

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.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13288
Joined: Tue Sep 16, 2008 5:38 pm

Re: Subroutine Local Program Vars

Postby EViews Gareth » Fri Oct 20, 2023 9:34 am

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.
Follow us on Twitter @IHSEViews

madm
Posts: 12
Joined: Mon Oct 23, 2017 1:13 pm

Re: Subroutine Local Program Vars

Postby madm » Sat Oct 21, 2023 12:01 pm

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.)


Return to “Suggestions and Requests”

Who is online

Users browsing this forum: No registered users and 3 guests