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 support@ccsinfo.com

Different data types in/out of functions.

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



Joined: 13 Feb 2009
Posts: 71

View user's profile Send private message

Different data types in/out of functions.
PostPosted: Thu Mar 05, 2009 3:51 am     Reply with quote

Hi, I want to write a function(to adjust settings increment or decrement them), I would want to carry the current setting into the function, then return the adjusted value. I have different variables, int8's, signed and unsigned int16's so the question is do I need a different function for each variable, or how do I do it with one? Thanks.
Ttelmah
Guest







PostPosted: Thu Mar 05, 2009 10:19 am     Reply with quote

First thing is that for many 'related' types, a single function may well work fine. For example, if you had a function to decrement a number, and wrote it to handle an int32, then called it with an int8 value, the latter will automatically be converted to an int32, the function will execute, and return an int32, which if you put the result into an int8, will automatically be converted back to an int8.
The second part, is that if the function needs to behave differently with different sized values, then you can use function overloading. With this, you write two (or more) different functions, with exactly the same name, but with each declared for a different variable type. When a function is called, the compiler automatically checks the variable type, and calls the function that matches that type.
The third is to manually override the operation of a single function, by declaring it to accept a pointer to your data, and a value saying what the type of the data is. So if you declared an enum, with values for int8, int16, int32 etc., and then called a function, with a pointer to an intxx value, and this enum, then you can test in the target function, what type the data being pointed to is, by looking at the enum, and adjust the code accordingly.
Which will suit your target best, will depend on how the code needs to change with different data types.

Best Wishes
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Mar 05, 2009 10:42 am     Reply with quote

Function overloading wasn't introduced until C++, does CCS C support it ?

Ttelmah is correct in that you could declare a function to take a pointer of type void and cast it, the problem is that the function won't know what type the original var is without passing another parameter to specify the type as Ttelmah stated.

Another option is to use a variable argument function definition as this can take multiple arguments of different types. http://publications.gbdirect.co.uk/c_book/chapter9/stdarg.html explains it.
Ttelmah
Guest







PostPosted: Thu Mar 05, 2009 10:48 am     Reply with quote

Yes. Function overloading is supported in CCS.
Function overloading is in ANSI C, it appeared befoe C++.
V4, gives quite a few parts of ANSI compatibility (with various degrees of success), but this actually worked qute early.
It is used quite a bit in the V4 code, for things like the maths library, where there are 64bit versions for the PCD compiler, that replace the 32bit versions, if 64bit variables are passed.

Best Wishes
Sydney



Joined: 13 Feb 2009
Posts: 71

View user's profile Send private message

PostPosted: Thu Mar 05, 2009 1:32 pm     Reply with quote

Thanks guys, seems to be working fine using the 1st method Smile
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