"Return" question

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.

bibre:
Just define your function as follows: (assume you need to return an Int from a function)

Big assumption; most people will want to return an int.

To:WizenedEE

Could you give example by defining one of my function?
I much learn by example.
Thank you best!

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 );

what is the different &result and *result?

I thought void setup() should be in the beginning

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:

increment();

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

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?

Hmm, how to make it has return value:

void show(int no)
{
  if (g_numberToDisplay < 10)
  {
    g_registerArray [4] = g_digits [0];
    g_registerArray [3] = g_digits [0];
    g_registerArray [2] = g_digits [0];
    g_registerArray [1] = g_digits [0];
    g_registerArray [0] = g_digits [g_numberToDisplay];
  }
  else if (g_numberToDisplay < 100)
  {
    g_registerArray [4] = g_digits [0];
    g_registerArray [3] = g_digits [0];
    g_registerArray [2] = g_digits [0];
    g_registerArray [1] = g_digits [g_numberToDisplay / 10];
    g_registerArray [0] = g_digits [g_numberToDisplay % 10];
  }
  else if (g_numberToDisplay < 1000)
  {
    g_registerArray [4] = g_digits [0];
    g_registerArray [3] = g_digits [0];
    g_registerArray [2] = g_digits [g_numberToDisplay / 100];
    g_registerArray [1] = g_digits [(g_numberToDisplay % 100) / 10];
    g_registerArray [0] = g_digits [g_numberToDisplay % 10];
  }
  else if (g_numberToDisplay < 10000)
  {
    g_registerArray [4] = g_digits [0];
    g_registerArray [3] = g_digits [g_numberToDisplay / 1000];
    g_registerArray [2] = g_digits [(g_numberToDisplay % 1000) / 100];
    g_registerArray [1] = g_digits [(g_numberToDisplay % 100) / 10];
    g_registerArray [0] = g_digits [g_numberToDisplay % 10];
  }
  else
  {
    g_registerArray [4] = g_digits [g_numberToDisplay / 10000];
    g_registerArray [3] = g_digits [(g_numberToDisplay % 10000) / 1000];
    g_registerArray [2] = g_digits [(g_numberToDisplay % 1000) / 100];
    g_registerArray [1] = g_digits [(g_numberToDisplay % 100) / 10];
    g_registerArray [0] = g_digits [g_numberToDisplay % 10];    
  }
  sendSerialData (g_registers, g_registerArray); [color=red]// I would like to replace it by return value, so I can have more structured code[/color]
  delay (1000);
}

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.

could you show me how you modify it?

Change your current declaration from

void show(int no)
{
  //your code
}

to something like

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

the reason I want to return because I want copy the structure of this program, very neat and structured:

mulai:
hserout [dec jml]
portc 0# = 1
portc 0.1 = 0
portc 0.2 = 0
portc 0.3 = 0
portb=%00000000
porta = dt11
portb=%00000001
portb=%00000000
porta = dt21
portb=%00000010
portb=%00000000
porta = dt31
portb=%00000100
portb=%00000000


portc 0# = 0
portc 0.1 = 1
portc 0.2 = 0
portc 0.3 = 0
portb=%00000000
porta = dt12
portb=%00000001
portb=%00000000
porta = dt22
portb=%00000010
portb=%00000000
porta = dt32
portb=%00000100
portb=%00000000


portc 0# = 0
portc 0.1 = 0
portc 0.2 = 1
portc 0.3 = 0
portb=%00000000
porta = dt13
portb=%00000001
portb=%00000000
porta = dt23
portb=%00000010
portb=%00000000
porta = dt33
portb=%00000100
portb=%00000000


portc 0# = 0
portc 0.1 = 0
portc 0.2 = 0
portc 0.3 = 1
portb=%00000000
porta = dt14
portb=%00000001
portb=%00000000
porta = dt24
portb=%00000010
portb=%00000000
porta = dt34
portb=%00000100
portb=%00000000
porta = dt44
portb=%00001000
portb=%00000000


tombol:
if portd.0=1 and t1=0 then
GoSub waktu1
t1 = 1
elseif portd.0=0 and t1=1 then
t1 = 0
End If

if portd.2=1 and t2=0 then
GoSub waktu2
t2 = 1
elseif portd.2=0 and t2=1 then
t2 = 0
End If

if portd.4=1 and t3=0 then
GoSub waktu3
t3 = 1
elseif portd.4=0 and t3=1 then
t3 = 0
End If

if portd.1=1 and t1=0 then
dt11 = 0
dt21 = 0
dt31 = 0
End If
if portd.3=1 and t1=0 then
dt12 = 0
dt22 = 0
dt32 = 0
End If
if portd.5=1 and t1=0 then
dt13 = 0
dt23 = 0
dt33 = 0
End If

if portd.6=1 and portd.0=0 and portd.2=0 and portd.4=0 then
dt11 = 0
dt21 = 0
dt31 = 0
dt12 = 0
dt22 = 0
dt32 = 0
dt13 = 0
dt23 = 0
dt33 = 0
dt14 = 0
dt24 = 0
dt34 = 0
End If

GoSub jumlah
GoTo mulai

waktu1:
dt11 = dt11 + 1
If dt11 = 10 Then
dt11 = 0
dt21 = dt21 + 1
End If
If dt21 = 10 Then
dt31 = dt31 + 1
dt21 = 0
End If
If dt31 = 10 Then
dt31 = 0
End If
Return

waktu2:
dt12 = dt12 + 1
If dt12 = 10 Then
dt12 = 0
dt22 = dt22 + 1
End If
If dt22 = 10 Then
dt32 = dt32 + 1
dt22 = 0
End If
If dt32 = 10 Then
dt32 = 0
End If
Return

waktu3:
dt13 = dt13 + 1
If dt13 = 10 Then
dt13 = 0
dt23 = dt23 + 1
End If
If dt23 = 10 Then
dt33 = dt33 + 1
dt23 = 0
End If
If dt33 = 10 Then
dt33 = 0
End If
Return

jumlah:
j1 = dt11 + dt12 + dt13
j2 = (dt21 + dt22 + dt23) * 10
j3 = (dt31 + dt32 + dt33) * 100
jml = j1 + j2 + j3
dt4 = jml / 1000
dt44 = dt4
If dt44 > dt4 Then dt44 = dt44 - 1
dtt44 = dt44 * 1000
dttt34 = (jml - dtt44)
dt3 = dttt34 / 100
dt34 = dt3
If dt34 > dt3 Then dt34 = dt34 - 1
dtt34 = dt34 * 100
dttt24 = dttt34 - dtt34
dt2 = dttt24 / 10
dt24 = dt2
If dt24 > dt2 Then dt24 = dt24 - 1
dtt24 = dt24 * 10
dt1 = dttt24 - dtt24
dt14 = dt1
If dt14 > dt1 Then dt14 = dt14 - 1
Return

it is in Basic. I hope you can get the idea why I always want return. so you can help me solve my code =(

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.

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.

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

No, return is not required in void functions.

//No return

void AddNumbers( int a, int b, int &result )
  {
    result = a + b;
  }

//Side effects of return.

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

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'

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. many errors when compiling the C program - #5 by pYro_65 - Programming Questions - Arduino Forum