returning values from function

I am calling this function clockWise, and I notice that after the function executes itself once then values from c and d are against reset to 0.

if(b > a)
{
clockWise(c,d);
Serial.print("+");
Serial.print("c =");
Serial.println(c);
Serial.print("d =");
Serial.println(d);
while(d >= c)
{clockWise(c,d);}
  while(d < c)
{anticlockWise(c,d);}
}

At the end of the function "clockWise(c,d)" I did write "return c,d" but c and d continue to be 0 after going out from clockWise function to the Serial.print values.
If someone can explain me how to solve this... thanx!

Post the whole code. It's not apparent from here what you mean.

If c and d are local to this function then you won't have their values outside the function. If c and d are global then they don't need to be passed as parameters, they can simply be updated from inside the function.

If you want c and d to be parameters and the variables you pass to the function to be updated then pass them by reference.

You can't return two values from a function. A function can only return one value, but it isn't clear that that is even what you want to do. Post the whole code so it will be clearer what you are trying to accomplish. I think you may have a fundamental misunderstanding of what it means to return a value.

See here as well for more information on functions in C++.

At the end of the function "clockWise(c,d)" I did write "return c,d"

You can only return one value from a function. However, in the code snippet that you posted you are not using any returned values anyway.

Please read this before posting a programming question and follow the instructions about posting a programming question.

Hi, thanx!
this is the entire code:

int a,b,c,d;
int LDR=0;
int dirPin=8;
int stepPin=9;
int microSecDelay=2000;
int counter=0;
int pause=150;
int steps=20;
int loopcount;

void setup() 
{
  pinMode(LDR, INPUT);           // Assign input for photoresistor
  pinMode(dirPin, OUTPUT);       // Assign output mode to pin for direction
  pinMode(stepPin, OUTPUT);      // Assign output mode to pin for setp
  digitalWrite(dirPin, HIGH);    // Initialize dir pin 
  digitalWrite(stepPin, LOW);    // Initialize step pin
  Serial.begin(9600);
  
  delay(8000);
}
int lightDir()                                          //Decides direction of light source
{
  a = analogRead(LDR);
  Serial.print("a= ");
  Serial.println(a);
  delay(pause);
  digitalWrite(dirPin, HIGH);
for(counter=0; counter<50; counter++)        // Decides lenght of the step
    {
    digitalWrite(stepPin, HIGH);                // Steps motor
    delayMicroseconds(microSecDelay);           // Wait microseconds 
    digitalWrite(stepPin, LOW);                 // 
    delayMicroseconds(microSecDelay);           // 
    }
    
  b = analogRead(LDR);
  Serial.print("b= ");
  Serial.println(b);
  Serial.print("\r");
  delay(pause);
}

int clockWise(int c, int d) 
{
c = analogRead(LDR);
  Serial.print("c+= ");
  Serial.println(c);
  delay(pause);
  digitalWrite(dirPin, HIGH);
  for(counter=0; counter<steps; counter++)      // Decides lenght of the step
    {
    digitalWrite(stepPin, HIGH);                // Steps motor
    delayMicroseconds(microSecDelay);           // Wait microseconds 
    digitalWrite(stepPin, LOW);                 // 
    delayMicroseconds(microSecDelay);           // 
    }
d = analogRead(LDR);
  Serial.print("d+= ");
  Serial.println(d);
  delay(pause);
  Serial.print("\r");
}

int anticlockWise(int c, int d)
{
c = analogRead(LDR);
  Serial.print("c-= ");
  Serial.println(c);
  delay(pause);
  digitalWrite(dirPin, LOW);
  for(counter=0; counter<steps; counter++)      // Decides lenght of the step
    {
    digitalWrite(stepPin, HIGH);                // Steps motor
    delayMicroseconds(microSecDelay);           // Wait microseconds 
    digitalWrite(stepPin, LOW);                 // 
    delayMicroseconds(microSecDelay);           // 
    }

d = analogRead(LDR);
  Serial.print("d-= ");
  Serial.println(d);
  delay(pause);
  Serial.print("\r");
  
  return c,d;
}

void loop() {
for(loopcount = 0; loopcount < 100; loopcount++)
{
Serial.print("loopcount= ");
Serial.println(loopcount);
if(loopcount == 99)
Serial.print("loopcount= ");
Serial.println(loopcount);
 {lightDir();}
if(b > a)
{
clockWise(c,d);
Serial.print("+");
Serial.print("c =");
Serial.println(c);
Serial.print("d =");
Serial.println(d);
  while(d >= c)
{clockWise(c,d);}
  while(d < c)
{anticlockWise(c,d);}
}

if(b < a)
{
anticlockWise(c,d);
Serial.print("-");
Serial.print("c =");
Serial.println(c);
Serial.print("d =");
Serial.println(d);
  if(d >= c)
{anticlockWise(c,d);}
  if(d < c)
{clockWise(c,d);}
  } 
 }
}

The idea is that a sensor attached to a stepper follows a light source :slight_smile:

OK, since c and d are global variables, there is no need to pass them as arguments to the function. If those are the only two variables the function is ever supposed to deal with, then make the function take no arguments and return nothing. The variables will simply be updated by the function.

By having them as arguments, it isn't the actual variables c and d that are going into the function, but instead just their values. The c and d inside the function is not the same as the global variables c and d. So the global variables aren't ever touched by that function.

Rewrite the function to take no arguments and return nothing and your code will work the way you want.

Usually it's a good idea to minimize the number of global variables in a program...they make the code harder to debug. If you move the definitions of c and d into loop() and use these calls to the functions:

  while(d >= c)
 {
    clockWise(&c, &d);            // Pass in the lvalues for the variables
  }
  while(d < c)
 {
    anticlockWise(&c, &d);}     // same here
 }

and then change the first line of each of your supporting functions to:

void anticlockWise(int c, int d) 
{   
   // the function code as written
}

void clockWise(int &c, int &d)
{   
   // the function code as written
}

and get rid of the return statements, it should work as you wanted.

 while(d >= c)
 {
    clockWise(&c, &d);            // Pass in the lvalues for the variables
  }

You don't put the & on the variables in the call. That only goes in the function definition.

The call would be written as:

 while(d >= c)
 {
    clockWise(c, d);            // Pass in the lvalues for the variables
  }

Gees...that's the second time in less than a day where copy-and-paste has got me into trouble...

econjack:
Gees...that's the second time in less than a day where copy-and-paste has got me into trouble...

Happens to the best of us.

Thanx for the information guys! I was applying it and it was working :slight_smile: