Guys, I'm having trouble creating a function, I want to call the function with one parameter for example VAR1 and load it inside the function the other variables .. like so:
int xVAR1 = 1;
int yVAR1 = 2;
int zVAR1 = 3;
void setup () {
...}
int set (PARAMETER)
{
xPARAMETER = 31 / / I would like the word "PARAMETER" to be replaced by VAR1
yPARAMETER = 50;
zPARAMETER = 80;
...
}
void loop () {
set (VAR1);
...
}
can someone help me? would create a similar function to create these variables? In case the function would receive the word VAR1 and would create variables xVAR1, yVAR1 and zVAR1. is this possible?
Thanks in advance.
wizarcl:
can someone help me? would create a similar function to create these variables? In case the function would receive the word VAR1 and would create variables xVAR1, yVAR1 and zVAR1. is this possible?
Thanks in advance.
May just be a poor word choice, but your code is not going to create variables it is only going to assign a value to variables (which are assumed to have been declared elsewhere). I suspect you're going about solving your problem the wrong way. Of course it's hard to be sure because you don't say why you're doing this, but for example it might make more sense to define a struct type with fields x, y, z and then declare instances of that struct named VAR1 etc (hopefully using more meaningful names!). Then you can just pass the address of your struct into your function to initialise it:
typedef struct {
int x;
int y;
int z;
} Position;
Position VAR1;
function set(Position *p)
{
p->x = 31;
p->y = 50;
p->z = 80;
}
void loop()
{
set(&VAR1);
}
Uncompiled and untested, but hopefully enough to give you the general idea.
If your magic position (31, 50, 80) has some global significance you could even declare a variable with this value and simply assign it in one go:
const Position InitialPosition = { 31, 50, 80 };
Position VAR1;
void loop()
{
VAR1 = InitialPosition;
}