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

1 Function 2 different call parameters

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



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

1 Function 2 different call parameters
PostPosted: Thu Jun 21, 2018 5:04 pm     Reply with quote

Hi,

I have a function as such:
Code:
void Function_Name(int NUMBER1 ,int NUMBER2, int16 LONG_NUMBER1 ,int16 LONG_NUMBER2)


However i would like to call that same function with an extra parameter from time to time like this:
Code:
void Function_Name(int NUMBER1 ,int NUMBER2, int16 LONG_NUMBER1 ,int16 LONG_NUMBER2, float DECIMALS)


I believe this is possible, i just dont remember the syntax.
I remember something like declaring it with all parameters but then just calling the shorter version would work just fine.

thanks for the help

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 21, 2018 6:51 pm     Reply with quote

Look up this topic in the CCS manual:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
Quote:
Variable Argument Lists


See this post by Jeremiah:
http://www.ccsinfo.com/forum/viewtopic.php?t=54635&start=4


In the CCS help file, it's listed under this heading in the index:
Quote:
Variable_Parameters
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Fri Jun 22, 2018 12:57 am     Reply with quote

Actually what he wants might better possibly be done with overloading.

CCS supports function overloading. You can have two (or more) declarations of the same function, with different numbers of arguments. The compiler calls the one whose argument list matches what you call it with.

Now you can cheat this for your situation with something like:
Code:

void Function_Name(int NUMBER1 ,int NUMBER2, int16 LONG_NUMBER1 ,int16 LONG_NUMBER2, float DECIMALS)
{
    //Main actual function
    //Have your code doing what you want
}

void Function_Name(int NUMBER1 ,int NUMBER2, int16 LONG_NUMBER1 ,int16 LONG_NUMBER2)
{
    Function_Name(NUMBER1, NUMBER2, LONG_NUMBER1, LONG_NUMBER2, 2);
    //call the main function, with the default number of decimals
    //(2 shown)
}

//Then if you call the function with only four variables, the second version
//is called, setting the 'DECIMALS' variable to a default value of '2'


Now you can if you wish code two completely separate functions, but obviously it is more code efficient, if you can generate a 'wrapper' as I show to provide the missing variable.

So calling:

Function_Name(1,2,4,5);

Results in

Function_Name(1,2,4,5,2);

actually being called.

Again in the manual search for 'Overloaded Functions'.
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Fri Jun 22, 2018 6:56 am     Reply with quote

Hi all thank you for your reply.

I was actually thinking of Overloading but was having a mental lapse.
However, manual in tow, i can now see that overloading might be cumbersome since i would need to maintain/modify several function definitions if/when i change the code.

Looking at the manual, the "default parameter" seems like a good choice. Simply call the function with or without the last parameter.

You all might have objections to this which i would be happy to hear.

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
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