-1

I'm guessing that the answer to this is "no," but I'm curious if there is any way to save writing some code by specifying a function to do the opposite of whatever another function does. For example the inverse of:

int FW (int in)
{
  int out=2+in;
  Return out
}

would be

int RV (int in)
{
  int out=in-2;
  Return out
}

However, would there be any way to call the inverse of FW() if it was a more complicated function that still had one and only one input for each output?

closed as off-topic by gre_gor, Code Gorilla, Enric Blanco, jfpoilpret, KIIV May 19 '17 at 19:42

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Arduino, within the scope defined in the help center." – gre_gor, Code Gorilla, Enric Blanco, jfpoilpret, KIIV
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    Ask this question on stackoverflow and use c tag .You will get much better answers there – Sniper May 16 '17 at 15:32
  • 2
    You could crate a more general function like; MOVE(int in, int direction){} – Gerben May 16 '17 at 15:41
  • 4
    @Sniper, this is hardly a C question. More of a theory question. Per Halting Problem on wikipedia, “Alan Turing proved in 1936 that a general algorithm to solve the halting problem for all possible program-input pairs cannot exist”, providing an easy answer of No to the question in its general form, and with more effort probably to the “one and only one input for each output” subquestion as well. ¶ ATE-ENGE, of course there are lots of ways to get function inverses for particular functions, but from Turing's result not in general – James Waldby - jwpat7 May 16 '17 at 17:02
  • lol! Well if Turing said I can't do it, I suppose I shouldn't try XD – ATE-ENGE May 16 '17 at 17:11
  • Now if it could be used to figure out the original two factors multiplied together to produce a big number, then you would be on to something. – SDsolar May 17 '17 at 8:39
3

No. A function does what it does and that is all. No other function can know what some other function does.

You could write a function that does different things depending on what parameters you pass to it, but that's the closest you can get, and is pretty pointless when you can just write different functions anyway.

  • That's what I had thought. Thanks for confirming. – ATE-ENGE May 16 '17 at 14:32
  • You can trivially see this to be true: imagine a function which returns 1 if the integer argument is odd, 0 if it is even. It's impossible to reverse this in reality, regardless of what you know about the code and what the code knows about the code. – Mark Smith May 16 '17 at 15:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.