Pages: [1]   Go Down
Author Topic: Confusion on passing variables to members  (Read 425 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 158
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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
Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

See "pass by reference"
Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19338
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I agree with Arrch, but:

Code:
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.
Logged

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

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Offline Offline
Full Member
***
Karma: 0
Posts: 158
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
}
Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 158
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Cincinnati, OH
Offline Offline
God Member
*****
Karma: 50
Posts: 914
I'm not bossy...I just know what you should be doing.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: