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

Calling Functions with both Motor Controls and Returns
http://robotc.net/forums/viewtopic.php?f=11&t=11707
Page 1 of 1

Author:  MichaelMedford [ Sat Jul 11, 2015 4:03 pm ]
Post subject:  Calling Functions with both Motor Controls and Returns

Hello there! First time poster. I am playing around with creating functions in ROBOTC and have a couple of questions I was hoping the community could help answer. I didn't want to overload this one post though since each question is a little different, so I'm going to break it up into a few posts

All of the examples I can find of ROBOTC functions online contain simple arithmetic and return the result of these calculations. Am I able to execute a function which both sends signals to the motor ports as well as does a calculation? For example:

Code:
bool moveforwards() {
   motor[port1] = 63;
   motor[port2] = -63;

   bool forwards_temp = true;
   return forwards_temp;


If I then call the function as follows, will I simultaneously turn the motors on AND return the boolean value?
Code:
forwards = moveforwards();


Or do I instead have to execute each of those parts of the function separately in order to (1) turn on the motors and (2) return the variable?
Code:
moveforwards();
forwards = moveforwards();


Thanks for the help!

Author:  Tabor473 [ Sat Jul 11, 2015 10:30 pm ]
Post subject:  Re: Calling Functions with both Motor Controls and Returns

So a function that is called executes all of its behavior. This means it will turn the motors on and return the value.

In other words the
Code:
forwards = moveforwards();
works as intended.

With the code you were showing it wouldn't be necessarily bad to call the function twice because you only turn the motors on but if your function went forward for 2 seconds and stopped the following code would make that action happen twice.
Code:
moveforwards();
forwards = moveforwards();

Author:  MichaelMedford [ Sun Jul 12, 2015 4:24 pm ]
Post subject:  Re: Calling Functions with both Motor Controls and Returns

Thanks Tabor473, that is just what I needed to know!

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