View unanswered posts | View active topics It is currently Fri Dec 14, 2018 8:01 pm






Reply to topic  [ 11 posts ] 
Pass string to function? 
Author Message
Rookie

Joined: Mon Oct 21, 2013 12:22 pm
Posts: 20
Post 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'.


Mon Jul 13, 2015 5:57 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: Pass string to function?
Try this instead:
Code:
void MonitorVars(string *label1, int one)
{
..
..
}

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Jul 14, 2015 1:37 am
Profile WWW
Rookie

Joined: Mon Oct 21, 2013 12:22 pm
Posts: 20
Post Re: Pass string to function?
Thank you. For my education, what does the asterisk mean?


Tue Jul 14, 2015 8:12 am
Profile
Rookie

Joined: Mon Oct 21, 2013 12:22 pm
Posts: 20
Post 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.


Tue Jul 14, 2015 8:23 am
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post 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

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Jul 14, 2015 9:19 am
Profile WWW
Rookie

Joined: Mon Oct 21, 2013 12:22 pm
Posts: 20
Post 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);


Tue Jul 14, 2015 9:26 am
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: Pass string to function?
No, you can't do that :)

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Jul 14, 2015 10:07 am
Profile WWW
Rookie

Joined: Mon Oct 21, 2013 12:22 pm
Posts: 20
Post 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)


Tue Jul 14, 2015 10:10 am
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post 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

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Jul 14, 2015 10:24 am
Profile WWW
Rookie

Joined: Mon Oct 21, 2013 12:22 pm
Posts: 20
Post Re: Pass string to function?
Oops, missed that. Thanks!


Tue Jul 14, 2015 11:20 am
Profile
Rookie

Joined: Tue Aug 19, 2014 12:15 pm
Posts: 37
Post Re: Pass string to function?
displayTextLine produces error:

Undefined procedure 'displayTextLine'


Fri Aug 21, 2015 7:10 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 11 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


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 post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.