Pages: [1]   Go Down
Author Topic: Passing and return variables  (Read 489 times)
0 Members and 1 Guest are viewing this topic.
Riverside, ca
Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I want to pass the value of i to half(i) and return the adjusted value of i. Code will not compile in Arduino but runs in the Arduino simulator

Code:
 
void setup(){
Serial.begin (9600);
}
void loop(){
  
  int i=4;
  Serial.println ("I = input");
  half (i);
  Serial.println (i);
  return ;
}

void half(int i){
  
  i=i/2;
  Serial.println (i);
  return(i);
}
Logged

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

Code:
  return(i);
But, you've said that the function doesn't return a value:
Code:
void half(int i){
Which is correct?
Logged

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

Code:
half (i);

You aren't using the return value anyway.

You can return the value:

Code:
void setup()
  {
  Serial.begin (9600);
  }

void loop()
  {
  int i=4;
  Serial.println ("I = input");
  Serial.println (half (i));
  delay (500);
  }

int half (int i)
  {
  return i / 2;
  }

Or, you can half the argument, if you pass it by reference:

Code:
void setup()
  {
  Serial.begin (9600);
  }

void loop()
  {
  int i=4;
  Serial.println ("I = input");
  half (i);
  Serial.println (i);
  delay (500);
  }

void half (int & i)
  {
  i = i / 2;
  }
Logged

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

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

South Texas
Offline Offline
Edison Member
*
Karma: 8
Posts: 1025
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your are getting confused with localization of variables.

When you have something like this -

void fun1() {
   int i;
   i=4;
   half(i);    // result just going to the bit bucket...
   print (i);
}

int half(int i) {
  i = i/2;
  return i;
}

you have 2 instances of i. one exists in fun1() and the other one exists in half(int i). and neither is global. and both are completely different variables.

if you want the return value to make it to fun1 you need to call half this way i = half(i). Otherwise the return value just goes to the bit bucket in the shy. So - your return value was just getting thrown out.

For i to have been a global variable it would have had to be defined before any functions and then your code would have worked. Globals have their place, where you need a value available everywhere. but it is poor practice to define all variables as globals, but we can often get away with it because our programs are small.

Logged

Riverside, ca
Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the help.
Logged

Pages: [1]   Go Up
Jump to: