![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
rhaguiuda
Joined: 05 Sep 2007 Posts: 46 Location: Londrina - Brazil
|
Multiple Return Function |
Posted: Fri Jan 02, 2009 10:55 am |
|
|
Sorry guys, I tried to search for this in the forums but returned 7000 results, so I'll ask here...
How It's possible to a function return multiple values?
When I need such a thing, I always create some global variables and update their values inside the function, but when the code grows up, it gets confusing to manage lots of variables.
Maybe using a array? In this case how the code will be?
For example, I need a function with one INT parameter, and 2 returns, Number1 and Number2. Their values are look up in a table.
Thanks for all help. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Fri Jan 02, 2009 11:18 am |
|
|
Yes.
You can either return an array, or possibly nicer a structure (since then you can have several different sized elements).
However, remember that this will involve _copying_ all these return values back to the calling function. A lot of overhead.
Alternative, give the function, the _address_ of a data storage container of the required type, that is in the main. Use pointer accesses to put the results directly into this container.
Other alternative, pass _back_ the address of the values in the subroutine. Remember though, that these will then need to be _static_ variables, otherwise the contents may be lost.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Fri Jan 02, 2009 11:33 am |
|
|
To keep the clarity of C language specification, I would prefer to speak of passing (multiple) variables by reference, which allows to change their value inside the function, rather than returning mutiple values. The latter is not provided by the C standard or CCS C, if didn't miss something important. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Fri Jan 02, 2009 3:14 pm |
|
|
By reference, is relatively 'new' (not part of the original C), and is only _limited_ in abilities in CCS. Better to use a pointer.
A search here will find threads only a very few weeks ago, with people trying to use 'by reference', and finding it does not work as expected. Except for single varables, stick with pointer passing.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Fri Jan 02, 2009 4:02 pm |
|
|
In my view, by reference (the original term used by K&R, I believe) and pointer are simply synonymous. I've to think, what may be exactly the difference between both with CCS C. So I agree to use pointers for the time being. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Fri Jan 02, 2009 4:08 pm |
|
|
OK. Ansi C, uses the specific term 'by reference', for a hidden implicit reference 'back' to an external variable. Here you use a different syntax to declare the variable in the function (&val), and can refer to the value inside the function, as if it is 'local', changing the original version. It is more efficient in certain circumstances, but 'can go wrong', in CCS, if the variable is not a 'simple' type (so using a structure this way, leads to problems).
Hence it is better not to use the term 'by reference', unless you are refering to this type of useage.
C, does allow for returning multiple values, as a structure. This is specifically mentioned in K&R as legal.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sat Jan 03, 2009 4:49 am |
|
|
I see, that "by reference" may be confused with the CCS "reference parameter" construct. I tried to compile the "reference parameter" examples from the CCS C manual, but the code seems to have no effect, cause PCH and PCD V4.084 aren't performing a call to the function.
The "reference parameter" construct is generally a good idea, considering the fact that CCS C passes all parameters and returned values in static data memory.
A general point, when selecting an optimal parameter passing mechanism, is to avoid creation of multiple instances of the same data, causing a considerable copy overhead and excessive consumption of data memory.
Best regards,
Frank |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|