Global variables or scope

Post Reply
Ken
Posts: 29
Joined: Tue Jan 29, 2019 1:49 am
My devices: Ipad
Location: NSW, Australia

Global variables or scope

Post by Ken »

If a text array variable is defined in a function, say amount$(7,19), how do I refer to it outside the function. I have looked at the scope, and global stuff in the help but I don’t get it. Is there a simple syntax to create and or read a 2 dimensional text array like this?
Ken

matt7
Posts: 115
Joined: Sun Jul 12, 2015 5:00 pm
My devices: iPhone
Location: USA

Re: Global variables or scope

Post by matt7 »

funcname.amount$(7,19)

To refer to a global scope variable from within a function, use: .varname
Last edited by matt7 on Mon Feb 04, 2019 2:49 pm, edited 1 time in total.

matt7
Posts: 115
Joined: Sun Jul 12, 2015 5:00 pm
My devices: iPhone
Location: USA

Re: Global variables or scope

Post by matt7 »

Here is an example of how this might be useful:

I have an "INSERT1" function that inserts an element into a one-dimensional array at a specified index. However, since the output of this function is an array with a different size than the input, I have to read the output out of the function after calling the function.

Here is my entire "insert" library (INSERT1, INSERT2, and INSERT3):

Code: Select all

'g'
/*
    INSERT1 (a(), iI, nI)
    INSERT2 (a(,), d$, iI, nI)
    INSERT3 (a(,,), d$, iI, nI)


Functions for inserting elements into numeric arrays.

'==============================================================

NOTE: Because the output of these functions is an array that is a different size than the input array, the resulting array contents will have to be manually copied after calling the function. For example:

    DIM data(3, 4)                 ' data has 3 rows and 4 cols
    GET DIM data XSIZE nX YSIZE nY

    nIns = 2
    INSERT2(data, "x", 1, nIns)    ' insert 2 rows at x=1

    nX += nIns
    DIM data(nX, nY)               ' redim data (clears to 0s)

    FOR i = 0 TO nX-1              ' copy from INSERT2.array(,)
      FOR j = 0 TO nY-1
        data(i,j) = INSERT2.array(i,j)
      NEXT j
    NEXT i

'==============================================================

INSERT1 inserts one or more elements into a one-dimensional numeric array. Inserted elements have a value of 0.

  Inputs
  ------

    a()    Numeric array

     iI    Index in a() of insertion

     nI    Number of elements to insert into a()

'==============================================================

INSERT2 inserts one or more rows or columns into a two-dimensional numeric array. Inserted elements have a value of 0.

  Inputs
  ------

    a(,)    Numeric array

      d$    Dimension of a(,) to insert ("x" or "y")

      iI    Index in a(,) of insertion

      nI    Number of rows or cols to insert in a(,)

'==============================================================

INSERT3 inserts one or more rows, columns, or "aisles" into a three-dimensional numeric array. Inserted elements have a value of 0.

  Inputs
  ------

    a(,,)    Numeric array

       d$    Dimension of a(,,) to insert ("x", "y", or "z")

       iI    Index in a(,,) of insertion

       nI    Number of rows, cols, or aisles to insert in a(,,)

'==============================================================
*/
''

DEF INSERT1 (a(), iI, nI)

  GET DIM a XSIZE x
  DIM array(x + nI)

  iA = 0
  FOR i = 0 TO x-1
    IF i = iI THEN iA += nI
    array(iA) = a(i)
    iA += 1
  NEXT i

END DEF

'==============================================================

DEF INSERT2 (a(,), d$, iI, nI)

  dX = 0  !  dY = 0
  GET DIM a XSIZE x YSIZE y
  GET DIM a XSIZE xA YSIZE yA

  IF d$ = "x" THEN
    xA = x + nI
    dX = nI
  ELSE ' d$ = "y"
    yA = y + nI
    dY = nI
  END IF

  DIM array(xA, yA)

  iA = 0
  FOR i = 0 TO x-1
    IF i = iI THEN iA += dX

    jA = 0
    FOR j = 0 TO y-1
      IF j = iI THEN jA += dY

      array(iA,jA) = a(i,j)

      jA += 1
    NEXT j
    iA += 1
  NEXT i

END DEF

'==============================================================

DEF INSERT3 (a(,,), d$, iI, nI)

  dX = 0  !  dY = 0  !  dZ = 0
  GET DIM a XSIZE x YSIZE y ZSIZE z
  GET DIM a XSIZE xA YSIZE yA ZSIZE zA

  IF d$ = "x" THEN
    xA = x + nI
    dX = nI
  ELSE ! IF d$ = "y" THEN
    yA = y + nI
    dY = nI
  ELSE ' d$ = "z"
    zA = z + nI
    dZ = nI
  END IF ! END IF

  DIM array(xA, yA, zA)

  iA = 0
  FOR i = 0 TO x-1
    IF i = iI THEN iA += dX

    jA = 0
    FOR j = 0 TO y-1
      IF j = iI THEN jA += dY

      kA = 0
      FOR k = 0 TO z-1
        IF k = iI THEN kA += dZ

        array(iA,jA,kA) = a(i,j,k)

        kA += 1
      NEXT k
      jA += 1
    NEXT j
    iA += 1
  NEXT i

END DEF

'==============================================================
And here is an example of using the library (note the line "data(i,j) = INSERT2.array(i,j)" inside the FOR loops at the end):

Code: Select all

{{/lib/arrays/insert}}
DIM data(3, 4)                 ' data has 3 rows and 4 cols
GET DIM data XSIZE nX YSIZE nY

FOR i = 0 TO nX-1              ' fill data with some values
  FOR j = 0 TO nY-1
    data(i,j) = i + j + 1
  NEXT j
NEXT i

nIns = 2
INSERT2(data, "x", 1, nIns)    ' insert 2 rows at x=1

nX += nIns
DIM data(nX, nY)               ' redim data (clears to 0s)

FOR i = 0 TO nX-1              ' copy from INSERT2.array(,)
  FOR j = 0 TO nY-1
    data(i,j) = INSERT2.array(i,j)
  NEXT j
NEXT i
This works because after the function is finished executing, SB remembers the values of all of its variables and allows them to be accessed after the function is called using the notation: funcname.varname

Ken
Posts: 29
Joined: Tue Jan 29, 2019 1:49 am
My devices: Ipad
Location: NSW, Australia

Re: Global variables or scope

Post by Ken »

Ok thanks. That works well.
How about if I define a variable in the main part of a program (not in a function). Say amount = 30. How do I now refer to this in a function.
Cheers

matt7
Posts: 115
Joined: Sun Jul 12, 2015 5:00 pm
My devices: iPhone
Location: USA

Re: Global variables or scope

Post by matt7 »

From within any function use: .amount

Basically, if a variable is defined/used in the main section of a program (not inside a function), it has a scope of "" (that's an empty string).

So the following works:

Code: Select all

DEF FUNC()
  var = 123
END DEF

FUNC()
var = 456

PRINT "var = " & var
PRINT ".var = & .var
PRINT "FUNC.var = " & FUNC.var

IF var = .var THEN
  PRINT "var = .var when not inside a function!"
END IF
Note that all scopes are accessible from any other scope (functions can read the values out of other functions):

Code: Select all

DEF FUNC1()
  var = 123
END DEF

DEF FUNC2()
  var = 456
END DEF

DEF FUNC_COMPARE()
  PRINT "FUNC1.var = " & FUNC1.var
  PRINT "FUNC2.var = " & FUNC2.var
END DEF

FUNC1()
FUNC2()
FUNC_COMPARE()

Ken
Posts: 29
Joined: Tue Jan 29, 2019 1:49 am
My devices: Ipad
Location: NSW, Australia

Re: Global variables or scope

Post by Ken »

Ok thanks. That sounds very straight forward. Very helpful.
I will try it later.

Cheers

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Global variables or scope

Post by Dutchman »

See also viewtopic.php?f=24&t=539&p=2196#p2176
You can use explicit naming using scope variables.
For example:

Code: Select all

user.age = 20
user.name$="John"
user.account=123.45
here scope variables with scope "user" serve as structure with named parameters.

Post Reply