| How can I make my C function return itself?

Previous | TI-Basic and C |

Q: | Suppose I'm writing an ASM function `'foo(n)'` which accepts an argument (a string, for example). I want to make `'foo(n)'` return `'foo(n)'` (the call itself) when `'n'` is of type "VAR" (i.e. if nothing has been assigned to `'n'` yet)...

A:

This is quite easy, if you know in advance the name of the program. Suppose, that your program name is "example". Here is the demo (called "Function Returning Itself") which will return `'example(n)'` if you type `'example(n)'` if `'n'` is a variable, and which will return the string "blabla" (for example) if the argument is something else:

    // A function returning itself

    #define USE_TI89              // Compile for TI-89
    #define USE_TI92PLUS          // Compile for TI-92 Plus
    #define USE_V200              // Compile for V200

    #define RETURN_VALUE          // Return pushed expression
    #define MIN_AMS 101           // Compile for AMS 1.01 or higher
    #define SAVE_SCREEN           // Save/Restore LCD Contents

    #include <tigcclib.h>         // Include All Header Files

    // Main Function
    void _main(void)
    {
      ESI argptr = top_estack;
      if (GetArgType (argptr) <= VAR_Q_TAG)  // it means that arg is a variable
                                             // see Tags to see why...
        push_expr_quantum (SYMSTR ("example"), USERFUNC_TAG);
      else
        {
          while (ESTACK (top_estack) != END_TAG)
            top_estack = next_expression_index (top_estack);
          top_estack--;
          push_string (SYMSTR ("blabla"));
        }
    }

Note that this solution is not ideal: if you rename the program name to something else, the function will still return `'example(n)'`. It is possible to determine the real name of the program in the run time, but this is very awkward.