Pages: 1 [2]   Go Down
Author Topic: "Return" question  (Read 800 times)
0 Members and 1 Guest are viewing this topic.
North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 52
Posts: 1778
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 52
Posts: 1778
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: March 05, 2012, 10:58:46 pm by pYro_65 » Logged


universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 52
Posts: 1778
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No, return is not required in void functions.

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

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


universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 52
Posts: 1778
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Pages: 1 [2]   Go Up
Jump to: