Optional function parameters

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Optional function parameters

Post 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));
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Optional function parameters

Post 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.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
nxtboy
Posts: 29
Joined: 29 Sep 2010, 18:15
Location: Cambridge, UK
Contact:

Re: Optional function parameters

Post 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
pepijndevos
Posts: 175
Joined: 28 Dec 2011, 13:07
Location: Gelderland, Netherlands
Contact:

Re: Optional function parameters

Post 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.
-- Pepijn
http://studl.es Mindstorms Building Instructions
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Optional function parameters

Post 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?
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Optional function parameters

Post 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!"));
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Optional function parameters

Post 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);
}
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Optional function parameters

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Optional function parameters

Post 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.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Optional function parameters

Post by mattallen37 »

Yes it does work! Thank you very much!
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 1 guest