Just define your function as follows: (assume you need to return an Int from a function)
Int myFunction (any arguments)
{
if (condition is true)
{
return 1; // or any other value you want
}
else
{
return 2; // or any other value you want
}
}
So if the condition is true you get a 1 returned to the calling Sketch otherwise you get a 2. You may y return as many values as you need by using several return statements or just one return using a variable with the right value.
Here is a few ways of returning values from a function
void AddNumbers( int a, int b, int &result )
{
result = a + b;
return;
}
void AddNumbers( int a, int b, int *result )
{
*result = a + b;
return;
}
int AddNumbers( int a, int b )
{
return a + b;
}
void setup()
{
int a = 4;
int b = 5;
int result;
//Version 1
AddNumbers( a, b, result );
//Version 2
AddNumbers( a, b, &result );
//Version 3
result = AddNumbers( a, b );
void setup()
{
int a = 4;
int b = 5;
int result;
//Version 1
AddNumbers( a, b, result );
//Version 2
AddNumbers( a, b, &result );
//Version 3
result = AddNumbers( a, b );
void AddNumbers( int a, int b, int &result )
{
result = a + b;
return;
}
void AddNumbers( int a, int b, int *result )
{
*result = a + b;
return;
}
int AddNumbers( int a, int b )
{
return a + b;
}
The code execute the version 1 first and return to it again and execute the version 2,etc, am I right?
setup can be at the start, the IDE generates the function prototypes at the top of the file when you compile.
in a function paramater list:
& means pass by reference, rather than by value. ( if the function modifies the variable, it modifies the original passed variable, not a copy )
means the variable is actually a pointer. ( address to variable ), the * in the code has different meanings, it converts/dereferences the address to the variable ( original passed variable )
when the function is called 'AddNumbers( a, b, &result );' the & operator gets the address of result ( pointer ), the compiler will call 'void AddNumbers( int a, int b, int *result )' as the result paramater is a pointer
Each version produces the same result, just different ways of doing it.
creativen:
what is the different &result and *result?
If you want to know more, you should read up on pointers and references. However, they're more "advanced" topics and are definitely not necessary for what you're trying to do.
I thought void setup() should be in the beginning
It doesn't matter where functions are defined. Functions are only executed when they are called. The exception is the main function, which then calls setup and loop
int AddNumbers( int a, int b )
{
return a + b;
}
This is the best way to solve your problem.
{increment();}
This is technically legal, but not how you should be doing things. Braces ( these: { ) are only needed to constrain a "block" -- usually an if, while or for statement. You should just do this:
It doesn't matter where functions are defined. Functions are only executed when they are called. The exception is the main function, which then calls setup and loop
Hmm, so for example in my code void increment(), so it will be executed when it is called? I thought all the codes will be executed from above all to down?
The code inside a function runs one line at a time from top to bottom.
Functions can be called in any order.
What do you want that function to return. It modifies variables that are available elsewhere and sends data.
You also don't use the 'int no' passed to the function.
bool show( void ) //You don't use 'int no'
{
//your code
if( 'success' ){ //Replace 'success' with some real code
return true;
}else{
return false;
}
}
But like I asked, what do you want to return, an unused return value is a waste.
The function modifies data that you can already access, if you can check that the send happened correctly, you could return like my example above. A void function ( method ) is fine.
The type before the name of your function is what it returns. "void" means "no type," so saying "void loop()" means "I'm defining a function which takes no arguments and returns nothing" and "char read()" means "I'm defining a function which takes no arguments and returns a char" and "bool isWithinRange(int x)" means "I'm defining a function which takes an integer and returns a bool"
So to make your function return something, change the void to bool. Then, in the body of the function, you have to honor your promise and actually return something. (Say: "return var;" assuming var is an int. You could also say "return 42;" if you wanted to return a constant.)
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?
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.
what's wrong with my modifying code so I can return:
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:
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'