Page 1 of 1

Optional function parameters

Posted: 03 Feb 2012, 22:43
by mattallen37
I know that you can have optional parameters for functions like this:

Code: Select all

int foo(int x, int y, int z = 12){
  return x+y+z;
}
and you could call it with something like this:

Code: Select all

NumOut(0, LCD_LINE1, foo(25, 7));
or

Code: Select all

NumOut(0, LCD_LINE1, foo(25, 7, 73));
And I assume you can do something like this as well:

Code: Select all

void txt_out(string text = "Hi!"){
  TextOut(0, LCD_LINE1, text);
}
and call it like this:

Code: Select all

txt_out(); //Should display "Hi!"
or

Code: Select all

txt_out("Hello!"); //Should display "Hello!"
What if you have a function that optionally "sets" information upon return. I want an option to supply a variable that gets written to. Something like this:

Code: Select all

int add(int x, int y, int & d_result){
  d_result = (x+y)*2;
  return d_result/2;
}
Normally you would call it like this:

Code: Select all

int Double;
NumOut(0, LCD_LINE1, add(13, 2, Double));//Should display 15
NumOut(0, LCD_LINE2, Double);            //Should display 30
However, what if I don't want to provide a variable for "d_result"? What if all I care about is the returned value? How can I do something like this?

Code: Select all

NumOut(0, LCD_LINE1, add(13, 2));

Re: Optional function parameters

Posted: 03 Feb 2012, 22:46
by mattallen37
It's sort of like the idea of calling a function, but not giving it a place to return the value. Like

Code: Select all

foo(25, 7);
I know that In this case it would be pointless to call the function, as it doesn't do anything helpful.

Re: Optional function parameters

Posted: 04 Feb 2012, 17:14
by nxtboy
Does this work?

Code: Select all

int TEMP = 2;

int add(int x, int y, int &d_result = TEMP){
  d_result = (x+y)*2;
  return d_result/2;
}
If not, http://stackoverflow.com/questions/1059 ... rence-in-c is relevant

Re: Optional function parameters

Posted: 05 Feb 2012, 09:43
by pepijndevos
I heard that NXC implements pass-by-reference by copying a global variable, so if you just do that yourself, you can decide to copy or not to copy.

Re: Optional function parameters

Posted: 05 Feb 2012, 10:23
by HaWe
matt,
could you pls explain what you're trying to do, especially what for?
E.g., are you trying to implement function overloading?
Or are you trying to pass functions as variable function parameters?

Re: Optional function parameters

Posted: 05 Feb 2012, 15:48
by mattallen37
I can't really test it now.

What I want, is the ability to choose if I want to provide a string to get written to. Like this:

Code: Select all

int AddString(string x, string y, string & z){
  z = x + y;
  return StrLen(z);
}
I could call that by using something like this:

Code: Select all

string full;
NumOut(0, LCD_LINE1, AddString("Hello", " World!", full));
TextOut(0, LCD_LINE2, full);
but what if I don't want the resulting string, just the length? I want to be able to use the function like this:

Code: Select all

NumOut(0, LCD_LINE1, AddString("Hello", " World!"));

Re: Optional function parameters

Posted: 05 Feb 2012, 19:15
by HaWe
did you already try strcat instead of "+"?

Code: Select all

strcat(z,x);
strcat(z,y);
return strlen(z);
IIRC there was an issue about adding strings bei "+".

edit:
it works for me! (shown is "12"):

Code: Select all

int AddString(string x, string y, string & z){
  strcat(z,x);
  strcat(z,y);
  return strlen(z);
}

task main() {
  string full="";
  int l=AddString("Hello ", "world!", full );
  printf("%d", l);
  while(1);
}
using NumOut it works also inline (I wish it did also with printf):

Code: Select all

int AddString(string x, string y, string & z){
  strcat(z,x);
  strcat(z,y);
  return strlen(z);
}

task main() {
  string full="";
  int l;
  NumOut(0,0,AddString("Hello ", "world!", full ));
  while(1);
}

Re: Optional function parameters

Posted: 05 Feb 2012, 19:43
by afanofosc
Matt,

I would simply try it and see what happens. Like this:

Code: Select all

int AddString(string x, string y, string & z = ""){
  z = x + y;
  return StrLen(z);
}

task main()
{
  string testing;
  NumOut(0, LCD_LINE1, AddString("hello", "world"));
  NumOut(0, LCD_LINE2, AddString("hello", "world", testing));
  TextOut(0, LCD_LINE3, testing);
}
Does it compile? Does it work? Does the F12 code look right? In my case the answer is Yes to all three questions. This is not valid C/C++ code, however.

John Hansen

Re: Optional function parameters

Posted: 05 Feb 2012, 20:13
by HaWe
aah, now I understand what matt was trying!
not returning just the length was the point but passing 2 or 3 strings alternatively.
That string & z = "" thing is a good trick, I'll have to keep that in my mind.

Re: Optional function parameters

Posted: 06 Feb 2012, 18:10
by mattallen37
Yes it does work! Thank you very much!