Announcement

Collapse
No announcement yet.

Dumb question on RobotC math syntax

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dumb question on RobotC math syntax

    A lot of math functions have this form:

    float cos(const float fRadians)

    I get float and fRadians, but what does the 'const' mean here?

  • #2
    The short answer is that it is a constant, it can't be changed. I can't find the documentation on it though.

    Comment


    • #3
      It means the function won't change the value passed.

      int times2 (int x){x=x*2; return x;}. c=3; times2(c); // c now has the value of 6

      int times2 (const int x){x=x*2; return x;}. c=3; times2(c); // c still has the value of 3

      Its really there for people passing in the parameters using pointers.
      Last edited by Foster; 06-17-2018, 06:41 AM.

      Comment


      • #4
        Thanks for the help Foster. Not sure what "passing in the parameters using pointers" means, but I guess I can ignore it for most purposes with math functions

        Comment


        • #5
          int times2 (int x){x=x*2; return x;}. c=3; times2(c); // c now has the value of 6

          int times2 (const int x){x=x*2; return x;}. c=3; times2(c); // c still has the value of 3
          Foster In both of these cases c will still have 3, neither one of the functions affects c because it is passed on the stack by value, not by reference.

          mmarquis The const variable modifier is used in a function call as a promise that the function will not change the value of what is passed. This is really more useful for the writers of the function in that the compiler will throw a warning if anyone tries to edit/write code within the function that tries to modify the passed variable.
          Last edited by TriDragon; 06-18-2018, 05:39 PM.

          Comment

          Working...
          X