How to define a vector in a custom function if you don't know the length of it

Hi,

I have a custom function called get_xs() that returns a linspaced_vector() (documentation)
In addition, I have another custom function that uses the returned output of get_xs().

 real p_a_wins(real p_a, real p_b, int n) {
    vector xs;

    for (i in 0:n) {
       xs = get_xs(i, n, 2);
       for (x in xs) {
       // do something with x 
       }

I need to define xs otherwise it is not in scope. But how should I define xs if I don’t know the length of it?

Thanks

Assuming the only thing you do with the result of get_xs is iterate over it, you should be able to write

for (i in 0:n) {
  for (x in get_xs(i, n, 2)) {
    // do something with x 
  }
}

Otherwise, you might have an issue if the size of get_xs depends on n, because it will be changing each iteration. To fix that, you’d need to 1) move the declaration vector xs inside the loop, and 2) calculate the size based on whatever get_xs is doing internally (I assume the size is some function of the arguments)