Multiple return

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,
i deal with a atmega32 and I write the program in C.
I want to make a function which has multiple returns. I solve the problem like this:

void foo(int16_t &a, int16_t &b) {
a+=0x3211;
b+=a;
return;
}

Using other C compilers (like Dev-C++) this kind of solution works, but the AVR studio has an error, it expects a ';' or ',' or ')' before the '&'. Has anybody any idea how to solve this problem, or any advive how to deal with multiple returns?
thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You got that the wrong way round. The function should be:

void foo(int16_t * a, int16_t  * b) { 
*a+=0x3211; 
*b+=*a; 
return; 
}

then call it with:

foo(&a_to_be_updated, &b_to_be_updated);

The idea being that you pass the address-of the variables to be updated and then you use the values that 'a' and 'b' are "pointing at" in the function.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
void foo(int16_t * a, int16_t  * b)

isn't a valid construction in C, it's C++ construction.
In C multiple values can be returned by returning struct or, as wrote clawson, by passing to function pointers to vars which values may be changed.

Edit: This is valid quoted code:

void foo(int16_t &a, int16_t &b)

previous is wrong. My mistake. :oops:
Sorry

Last Edited: Thu. Apr 23, 2009 - 12:22 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

isn't a valid construction in C, it's C++ construction

What are you talking about - of course it's valid C. Are you saying C cannot have two parameters to a function or that they cannot be passed by reference rather than value? Where have you seen that written down?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

He quoted the wrong function declaration. ;-)
I assume he rather wanted to quote the original declaration of the OP (which is in fact C++).

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

the post by awit is kind of confusing, but i do agree with what clawson had written. Cheers :D