ROBOTC.net forums
http://www.robotc.net/forums/

Pass string to function?
http://www.robotc.net/forums/viewtopic.php?f=11&t=11712
Page 1 of 1

Author:  getgray [ Mon Jul 13, 2015 5:57 pm ]
Post subject:  Pass string to function?

Cortex, current firmware, current RobotC

I'm trying to pass a string to a function, can't get it to work. Can someone show me what I'm doing wrong?

Simple example:

Code:
void MonitorVars(string label1, int one)
{
clearLCDLine(0);
clearLCDLine(1);

displayLCDPos(0,0);
displayNextLCDString(label1);
displayNextLCDNumber(one);

}

task main()
...
      MonitorVars("1=", Ch1);
...


Errors with:
**Error**:'const' expressions does not fit. Call to 'MonitorVars2'. Parameter: 'unsigned string & label1' is '"1="' of type 'string'.

Author:  mightor [ Tue Jul 14, 2015 1:37 am ]
Post subject:  Re: Pass string to function?

Try this instead:
Code:
void MonitorVars(string *label1, int one)
{
..
..
}

= Xander

Author:  getgray [ Tue Jul 14, 2015 8:12 am ]
Post subject:  Re: Pass string to function?

Thank you. For my education, what does the asterisk mean?

Author:  getgray [ Tue Jul 14, 2015 8:23 am ]
Post subject:  Re: Pass string to function?

Also, while on the subject of passing parameters, is there a way to declare parameters such that they are not required? I woudl account for any missing by defaulting them in the function, but right now, if I don't send all parameters the compiler, won't compile.

Author:  mightor [ Tue Jul 14, 2015 9:19 am ]
Post subject:  Re: Pass string to function?

The asterisk denotes a pointer. To learn more about pointers check out my tutorial: http://botbench.com/blog/2013/01/14/tut ... in-robotc/

You can make default values for parameters, yes. Like this:
Code:
int addTwo(int one=1, int two=2)
{
  return one + two;
}

task main()
{
  int sum1 = addTwo(4, 8);
  writeDebugStreamLine("sum1: %d", sum1);
  displayTextLine(3, "sum1: %d", sum1);
 
  // this will make the "two" argument default to 2
  int sum2 = addTwo(4);
  writeDebugStreamLine("sum2: %d", sum2);
  displayTextLine(4, "sum2: %d", sum2);
 
  // this will use both default values for the argument.
  // You cannot tell it to use the default value for the "one" argument without defaulting the "two" argument
  int sum3 = addTwo();
  writeDebugStreamLine("sum3: %d", sum3);
  displayTextLine(5, "sum3: %d", sum3);

}

Regards,
Xander

Author:  getgray [ Tue Jul 14, 2015 9:26 am ]
Post subject:  Re: Pass string to function?

Thanks. So you can't do this right?
Code:
  int sum1 = addTwo(, 8);

That is, you can't "skip" a parameter? So if you had 6 parameters and wanted to default parameters 4 and 5, but tell it parameter six you can't do:
Code:
  int result = somefunction(1,2,3,,,6);

Author:  mightor [ Tue Jul 14, 2015 10:07 am ]
Post subject:  Re: Pass string to function?

No, you can't do that :)

Author:  getgray [ Tue Jul 14, 2015 10:10 am ]
Post subject:  Re: Pass string to function?

Oh well, thanks!

What about that asterisk preceding label1 below? What does it mean?
Code:
void MonitorVars(string *label1, int one)

Author:  mightor [ Tue Jul 14, 2015 10:24 am ]
Post subject:  Re: Pass string to function?

That's the pointer I was talking about. I "pointed" you to a tutorial about them on my website.

= Xander

Author:  getgray [ Tue Jul 14, 2015 11:20 am ]
Post subject:  Re: Pass string to function?

Oops, missed that. Thanks!

Author:  ronaldle [ Fri Aug 21, 2015 7:10 am ]
Post subject:  Re: Pass string to function?

displayTextLine produces error:

Undefined procedure 'displayTextLine'

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/