CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Multiple Return Function

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rhaguiuda



Joined: 05 Sep 2007
Posts: 46
Location: Londrina - Brazil

View user's profile Send private message

Multiple Return Function
PostPosted: Fri Jan 02, 2009 10:55 am     Reply with quote

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.
Ttelmah
Guest







PostPosted: Fri Jan 02, 2009 11:18 am     Reply with quote

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
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jan 02, 2009 11:21 am     Reply with quote

See these threads:
http://www.ccsinfo.com/forum/viewtopic.php?t=34093
http://www.ccsinfo.com/forum/viewtopic.php?t=19437
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Fri Jan 02, 2009 11:33 am     Reply with quote

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.
Ttelmah
Guest







PostPosted: Fri Jan 02, 2009 3:14 pm     Reply with quote

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
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Fri Jan 02, 2009 4:02 pm     Reply with quote

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.
Ttelmah
Guest







PostPosted: Fri Jan 02, 2009 4:08 pm     Reply with quote

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
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Jan 03, 2009 4:49 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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