|
|
View previous topic :: View next topic |
Author |
Message |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
1 Function 2 different call parameters |
Posted: Thu Jun 21, 2018 5:04 pm |
|
|
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
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Fri Jun 22, 2018 12:57 am |
|
|
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
|
|
Posted: Fri Jun 22, 2018 6:56 am |
|
|
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 |
|
|
|
|
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
|