Go Down

Topic: "Return" question (Read 1 time) previous topic - next topic

pYro_65

That code is modifying global data ( ports ), which are available in all your functions.
No return values are needed, and aren't used in that code.

creativen

what do you mean it is available in all my functions?
There is return in the code I just post in Basic...why do you state there is no return used in that code?

pYro_65

#17
Mar 06, 2012, 04:52 am Last Edit: Mar 06, 2012, 04:58 am by pYro_65 Reason: 1
The arduino environment uses the AVR libraries, they define the registers like PORTA, DDRA, PINA. they are available for you to use anywhere.

That code is basically a single function with sub routines defined.
The sub routines modify data but aren't returning values.
The return signifies the end of the sub routine and execution continues from where the calling gosub was declared.

EDIT: without the return the execution would simply continue to the line after the return.

creativen

So according to that BASIC code, do I need to use "return" in my IDE arduino?
Or we have other way?

pYro_65

No, return is not required in void functions.

//No return
Code: [Select]
void AddNumbers( int a, int b, int &result )
  {
    result = a + b;
  }


//Side effects of return.
Code: [Select]
void AddNumbers( int a, int b, int &result )
  {
    result = a + b;
    return;
    result = result * 2; //Will never run due to return statement above.
  }

creativen

what's wrong with my modifying code so I can return:

Code: [Select]
void blinks(int a)
  {
  int a = random (1,3);
  if (a==1)
    {
    digitalWrite(9, HIGH);   // set the LED on
    return;
    }
  else
    {
    digitalWrite(9, LOW);    // set the LED off
    blinks(int a);
    }
}


it appears warning message:

Code: [Select]

sketch_mar06b.cpp: In function 'void loop()':
sketch_mar06b:2: error: too few arguments to function 'void blinks(int)'
sketch_mar06b:119: error: at this point in file
sketch_mar06b.cpp: In function 'void blinks(int)':
sketch_mar06b:127: error: declaration of 'int a' shadows a parameter
sketch_mar06b:136: error: expected primary-expression before 'int'

pYro_65

There is nothing wrong with that return, you just call the function with no paramater
blinks();
needs to be
blinks( some_value );

and you have int a in the paramaters. you cannot have int a in the local variables as well, which one is the code supposed to use?

Sorry I have to go to work, I can respond later.
I suggest you read the tutorials I posted in your other thread. http://arduino.cc/forum/index.php/topic,95100.msg714281.html#msg714281

Go Up