Go Down

Topic: Confusion on passing variables to members (Read 460 times) previous topic - next topic

memotick

I suspect this is a fundamental programming concept, but I don't understand why in the code below the individual members don't affect the variables a, b and c:

Code: [Select]
uint16_t a,b,c;

void Member1(uint16_t a, uint16_t b, uint16_t c) {
  a=2;
  b=4;
  c=8;

  Serial.write("\nInside Member1:\na=");
  Serial.print(a);
  Serial.write("\nb=");
  Serial.print(b);
  Serial.write("\nc=");
  Serial.print(c);
}

void Member2(uint16_t a, uint16_t b, uint16_t c) {
  a=128;
  b=256;
  c=512;
  Serial.write("\nInside Member2:\na=");
  Serial.print(a);
  Serial.write("\nb=");
  Serial.print(b);
  Serial.write("\nc=");
  Serial.print(c);
}

void setup() {
  a=16;
  b=32;
  c=64;
  Serial.begin(115200); 

  Serial.write("\nbefore entering Member1:\na=");
  Serial.print(a);
  Serial.write("\nb=");
  Serial.print(b);
  Serial.write("\nc=");
  Serial.print(c);

  Member1(a,b,c);
  Serial.write("\nafter entering Member1:\na=");
  Serial.print(a);
  Serial.write("\nb=");
  Serial.print(b);
  Serial.write("\nc=");
  Serial.print(c);
  Member2(a,b,c);
  Serial.write("\nafter entering Member2:\na=");
  Serial.print(a);
  Serial.write("\nb=");
  Serial.print(b);
  Serial.write("\nc=");
  Serial.print(c);
}

void loop () {
}


Here is the output:

Code: [Select]
before entering Member1:
a=16
b=32
c=64
Inside Member1:
a=2
b=4
c=8
after entering Member1:
a=16
b=32
c=64
Inside Member2:
a=128
b=256
c=512
after entering Member2:
a=16
b=32
c=64


Should I be passing pointers of some flavor to have a, b and c's assignments within the members to be seen outside of the member?

Not sure it matters, but I'm using Arduino 1.05 with an Uno R3 Plus

Thanks for the help

Arrch


Nick Gammon

I agree with Arrch, but:

Code: [Select]

uint16_t a,b,c;

void Member1(uint16_t a, uint16_t b, uint16_t c) {


Why bother even passing in global variables? Plus having the same name is confusing.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

memotick


See "pass by reference"


Thanks, I needed that (I am not being sarcastic).  A search for "pass by reference" got me what I needed for my previous example.  I see how to use &variable when passing to a member now.  However, with arduino's "setup" and "void" functions it isn't clear to me as this seems to be a special case.   For example, let's say I don't want to use a global variable but I want one to be declared in setup which is passed by reference to loop.   How can this be done?  (if you are wondering why I don't just use global variables, it is my understanding that one should avoid using them if at all possible).  Here is an example showing my dilemma:

Code: [Select]
void setup() {
  uint8_t junk=8;
  Serial.begin(57600);
}

void member1(uint8_t &junk) {
  junk=22;
}

void loop() {

  Serial.println(junk);
  member1(junk);
  Serial.println(junk);
}

Arrch

For example, let's say I don't want to use a global variable but I want one to be declared in setup which is passed by reference to loop.


Global variables aren't declared in a function, so a variable declared in setup() wouldn't be global. You can declare the variable global, assign it a value in setup() and then use it loop(). Also, setup() isn't calling loop(), so you can't pass anything between the two. You could re-write the main() function to pass the variable reference to both the functions, but that's convoluted and unnecessary. If you want the variable to be accessible by both functions, make it global.

it is my understanding that one should avoid using them if at all possible


Your understanding is baseless; there is nothing wrong with using global variables when it makes sense to use them.

memotick


For example, let's say I don't want to use a global variable but I want one to be declared in setup which is passed by reference to loop.


Global variables aren't declared in a function, so a variable declared in setup() wouldn't be global. You can declare the variable global, assign it a value in setup() and then use it loop(). Also, setup() isn't calling loop(), so you can't pass anything between the two. You could re-write the main() function to pass the variable reference to both the functions, but that's convoluted and unnecessary. If you want the variable to be accessible by both functions, make it global.

it is my understanding that one should avoid using them if at all possible


You're understanding is baseless; there is nothing wrong with using global variables when it makes sense to use them.


ok, thanks for the clarification

PaulS

Quote
Your understanding is baseless; there is nothing wrong with using global variables when it makes sense to use them.

The key part of that statement is "when it makes sense to use them". Way too much of the code posted here uses global variables for everything.

econjack

PaulS is right. The correct idea is to use globals only when you have to. Also, programmers used the terms define and declare as though they are the same. They are not, and knowing the difference may help you understand what's been posted here for you. You might read:

http://www.informationweek.com/computer-programming-and-precise-termino/208808373?pgno=1

to see if it helps.

Go Up