2

My team is writing code for RobotC, a language with C-like syntax. It is mainly used as an introductory to programming in robotics. It has the base C stuff: if statements, for and while loops, functions and it even has structs, arrays and pointers, but they aren't used often. The main difference is that it's interperted, has multithreading, isn't free, and you can only compile one file at a time.

We wanted to make a motor move. To do this, you would usually write.

 motor[motorA] = 50;

motor is an array provided by RobotC that contains all the values it will set the motors to. motorA is an enum for the wire port.
50 is the power level.

My team mate accidentally (or maybe purposely) wrote this instead

 motor(motorA) = 50;

I was about to correct him, when surprisingly, it compiled and ran fine!

I know Java and a little bit of normal C, and this just doesn't make sense to me. You can't set the value of a function return...can you?

Could someone please explain why this works?

user253751
  • 57,427
  • 7
  • 48
  • 90
Blubber
  • 1,375
  • 1
  • 15
  • 32
  • 1
    Sorry about the misleading tag, but I don't have enough rep to make a RobotC tag. – Blubber Mar 26 '15 at 23:08
  • I would say that because it is a student language the syntax isn't too strict about whether you use [] or () to get to an index in an array. You shouldn't think of it as a method called motor with a param motorA, under the covers it probably gets converted to [] (or () ) anyway, – Mathemats Mar 26 '15 at 23:17
  • I'll try doing some testing, but I don't think they would convert () to [], because they have both arrays and functions in the language. Maybe the compiler is smart enough to know when to convert, but it would be strange for them to add this feature when they could improve the compiler in a lot better ways....such as giving the correct line number where an error is. – Blubber Mar 26 '15 at 23:30
  • 1
    The language description is rather inadequate, it never even mentions arrays. You are much more likely to find help at the vendor's forum. – Hans Passant Mar 27 '15 at 00:56

1 Answers1

2

If you look in RobotCIntrinsics.c (when you have your code file open, just right click on one of the motor[] commands and select "Go to definition/declaration" from the context menu to see it), you'll find that the motor command is defined as a "property." Now, I can't say what exactly that means, but in my experience it means that you can use either [] or () to access the values in it. It's not an array, and it's not a function call. It's something else entirely that apparently allows both syntaxes to be used.

BurningLights
  • 2,387
  • 1
  • 15
  • 22