Pages: [1]   Go Down
Author Topic: Noob question about return  (Read 207 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Arduino programmers,

Im jazzy gerard 2 day old new to the programming world from arduino.
I alreay now how to program in JAL but I find the arduino programming and all types of boards
very interesting, user friendly and easy to use!

now I'm busy with making a summary of all the refrences on the arduino website. but i have e problem with the return.

let me state my question with an example

int checkSensor() {
if (analaogRead(0) > 400){

return 1;

else

return 0;
{
{

so if i understand the explanation this means if the value from the reading on analog pin 0 is grater then 400 the program goes back to the beginning and starts over from there and a 1 is returned. What returns the 1? the checksensor? the analog reading? 

probably for you guys this is easy but i'm just a beginner! smiley

best regards!
Logged

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey JazzyGerard.   Enjoying your LED modules?  smiley-wink

Right...  The return statement sets the value that is returned from the function call.  The function terminates at that point.

So, if you create the function:

Code:

int checkSensor() {
  if (analaogRead(0) > 400) {
    return 1;
  } else {
    return 0;
  }
}

you can then call that function and use the return value in some way.  When you call the function, either 1 or 0 will be returned to where you called the function.  The function is in effect replaced by the return value.

For example, if you then had the code:
Code:
int sensorHigh = checkSensor();
the variable sensorHigh would either contain a 0 or a 1 depending on the value of analog pin 0.  In effect the code is equivalent to one of:
Code:
int sensorHigh = 1;
or
Code:
int sensorHigh = 0;
depenging on the value of analog pin 0.

The definition of the checkSensor() function (int checkSensor()) includes the "return type" - int in this case.  This is the kind of variable that the function can return - whole numbers between -32768 and 32767 for this example.
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hahaha!

yes they save me a lot of work during prototyping!!!!!
is there also a 12V version?

thanks for the detailed explanation I totally understand it now!!!

best regards
Logged

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hahaha!

yes they save me a lot of work during prototyping!!!!!
is there also a 12V version?
I have done 12V ones before, yes.  I have 560Ω resistor arrays here that I use for them.  I can do any custom combinations you want too smiley
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

okey thanks! i will contact you on ebay with my desires!!  smiley

best regards!
Logged

Pages: [1]   Go Up
Jump to: