1
votes

I have searched the internet/documentation and it seems that this is not possible. But I have a large number of variables that need to be passed through a function. The function is working properly.

Example of variable list:
st1mrks = 94
st2mrks = 34
st3mrks = ...

test1 = "student has collected " st1mrks " marks. "
test2 = "student has collected " st2mrks " marks. "
test3 = "student has collected " st3mrks " marks. "
test4 = ...
            Loop % totaltestnumber{

                    dofunction(test%A_Index%)

            }

Alternatively, I tried using while

i=0
while (i < totaltestnumber){
     dofunction(test%i%)
     i++
}

But this obviously won't work.

Is there anyway to do this?

1

1 Answers

2
votes

Let's look at some definitions:

  • Functions take parameters
  • Parameters must always be fully defined
    • i.e. You have to specify every parameter ahead of time.
    • i.e. If you have 100 variables you have to define 100 parameters
    • This is because parameters become copies to local variables inside the function, and so need to be fully defined ahead of time.
  • Pseudo Arrays are not arrays. They are a "collection of sequentially numbered variables".
    • i.e. You have 100 individual variables.
    • Pseudo Arrays, while convenient, are also not recommended to use.
  • Object-Based Arrays on the other hand are arrays. They are an object that holds a collection of stuff.
    • i.e. You have 1 variable with 100 elements.

I see 2 options:

Option 1:

If you are using Pseudo Arrays, and have 100 variables, then you are out of luck. You have to define all 100 variables as parameters that need to be passed individually. There is no easy way to iterate through them dynamically. Functions and parameters don't work that way.

Option 2

Change to use Object-Based Arrays, and not different variables. This way you will only pass one object. If we strictly use your example, you can do:

Example of variable list:
st1mrks = 94
st2mrks = 34
st3mrks = ...

test1 = "student has collected " st1mrks " marks. "
test2 = "student has collected " st2mrks " marks. "
test3 = "student has collected " st3mrks " marks. "
test4 = ...


; Create the array, initially empty:
Array := []

Loop % totaltestnumber{
  Array.Push(test%A_Index%)
}

dofunction(Array)

....

dofunction(ByRef Array)
{
    Loop % Array.MaxIndex()
    {
      ...
    }
}

This code can be further optimized if you use Object-Based Arrays right from the start.