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?
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.