VEX: How to define a global variable and pass it to a function

   8094   4   2
User Avatar
Member
4 posts
Joined: July 2017
Offline
I'm quite new to coding and I'm trying to pass variables to my user-defined function, but I get the error “Variable is not accessible in this function”. I want to define global variables on top, create local variables in the function and then pass the calculated value to the global variables. The function also returns another value, but I need actually three values in my main program that will be calculated in the function. Below is a simplified example of what I mean:

// define global variables
float ao = 0;
float bo = 0;

int mand(float x, y){ //user-defined function
int a = x/2;
int b = y/3;
int i = a+b;
ao = a; // pass values a to global variable ao
bo = b; // pass values b to global variable bo
return(i);
}

v@Cd = set(ao, bo, mand(1, 0.3)); // use variables ao and bao in main program, as well as i, for example in color attribute

Is it somehow possible to access global variables in my function? It worked like this in Javascript. Or is there a possibility to return three values from my function and use them all later?

Thanks!
User Avatar
Member
2036 posts
Joined: Sept. 2015
Offline
As far as I know you can't access a variable outside a function directly.

It needs to be passed to the function if you want to change it, which is what I usually do.

I've also changled your code a bit for a and b, casting them as ints.

// define global variables
float ao = 0;
float bo = 0;


int mand(float x, y, ao, bo){ //user-defined function
int a = int(x/2);
int b = int(y/3);
int i = a+b;
ao = a; // pass values a to global variable ao
bo = b; // pass values b to global variable bo
return(i);
}

v@Cd = set(ao, bo, mand(1.0, 0.3, ao, bo)); // use variables ao and bao in main program, as well as i, for example in color attribute
User Avatar
Staff
4159 posts
Joined: Sept. 2007
Offline
Perhaps constants would work:

#define MY_INT            123
#define otherInt 999

int foo( float ax; ) {
int val = (ax > 0) ? MY_INT : otherInt;
return(val);
}

i@a = foo(@P.x);
I'm o.d.d.
User Avatar
Member
4 posts
Joined: July 2017
Offline
BabaJ
As far as I know you can't access a variable outside a function directly.

It needs to be passed to the function if you want to change it, which is what I usually do.

I've also changled your code a bit for a and b, casting them as ints.


Great approach to pass them. Thanks a lot!

Now I still had the problem that the ao/bo variables are not changed to a/b for the main program when I set the colors. Is it actually possible to return three values? I now figured it out to do it by returning a vector and then accessing each value in it seperately in the main program and it works.
Edited by viviana_aline - July 30, 2017 08:44:04
User Avatar
Member
2036 posts
Joined: Sept. 2015
Offline
Now I still had the problem that the ao/bo variables are not changed to a/b for the main program when I set the colors.


a/b are ‘changed’…I think what is happening and you didn't realize is that your x/2 and y/3 are divisions by integers.

I assumed you were doing this intentionaly creating your own ‘floor’.

That is why I added the casting of int to your two lines of code.( just to get rid of Houdinis' ‘green warnings’ )

Change your int variables in the function to floats and do your divisions by a float:

... 
int a = int(x/2.0);
int b = int(y/3.0);
...

Is it actually possible to return three values?

As far as I know it isn't….

I now figured it out to do it by returning a vector and then accessing each value in it seperately in the main program and it works.

Yeah that's a good way to do it…also…in the future if you want to ‘return’ more than 3 values, you could also return an array of values and post process individual items with indexing.
Edited by BabaJ - July 30, 2017 09:34:22
  • Quick Links