Go Down

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

pYro_65

Here is a few ways of returning values from a function

Code: [Select]

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

creativen

what is the different &result and *result?

I thought void setup() should be in the beginning

Code: [Select]

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?

pYro_65

#7
Mar 06, 2012, 03:25 am Last Edit: Mar 06, 2012, 03:28 am by pYro_65 Reason: 1
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.


WizenedEE


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

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

Quote
Code: [Select]

int AddNumbers( int a, int b )
  {
    return a + b;
  }


This is the best way to solve your problem.

Quote
Code: [Select]
{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:
Code: [Select]

increment();

creativen


Quote

I thought void setup() should be in the beginning


Quote

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:
Code: [Select]

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

Go Up