Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Is main() a callback function?

Is main() a callback function?

Scheduled Pinned Locked Moved The Lounge
question
44 Posts 15 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Rob Philpott

    I can't make up my mind.

    Regards, Rob Philpott.

    S Offline
    S Offline
    Stefan_Lang
    wrote on last edited by
    #41

    Only if used as such, e. g. in this code:

    #include

    using namespace std;

    static int call_countdown = 3;
    typedef int (*MyCallbackFun)();
    int foo(MyCallbackFun cb, int count)
    {
    static int foo_counter = 0;
    ++foo_counter;
    cout << "enter foo[" << foo_counter <<"]: " << count << endl;
    int result = cb();
    cout << "exit foo[" << foo_counter << "]: " << result << endl;
    return result;
    }
    int bar()
    {
    static int bar_counter = 0;
    ++bar_counter;
    cout << "bar[" << bar_counter << "]" << endl;
    return -1;
    }
    int main()
    {
    static int call_counter = 0;
    ++call_counter;
    cout << "enter main[" << call_counter << "]" << endl;

    int result = 0;
    if (call\_counter < 5)
    {
        result = (call\_counter>2)
            ? foo(bar, call\_counter)
            : foo(main, call\_counter);
    }
    cout << "exit main\[" << call\_counter << "\]: " << result << endl;
    
    return call\_counter;
    

    }

    You can test it here: https://www.onlinegdb.com/online_c++_compiler[^] or trust me that the output is:

    enter main[1]
    enter foo[1]: 1
    enter main[2]
    enter foo[2]: 2
    enter main[3]
    enter foo[3]: 3
    bar[1]
    exit foo[3]: -1
    exit main[3]: -1
    exit foo[3]: 3
    exit main[3]: 3
    exit foo[3]: 3
    exit main[3]: 3

    The tricky bit about this is that by using main() as a callback function, you're also using it recursively, which complicates matters considerably: it's easy to mess up the code and get an endless recursion. (that's why I added counters and output in every function) It's doable, but definitiely not a good idea.

    GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

    1 Reply Last reply
    0
    • H honey the codewitch

      created from the silk of butterfly cocoons :-D

      Real programmers use butterflies

      T Offline
      T Offline
      TheGreatAndPowerfulOz
      wrote on last edited by
      #42

      lol, you got me there.

      #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

      1 Reply Last reply
      0
      • L Lost User

        I suspected this might be the case, but I have not had access to the source for a number of years.

        R Offline
        R Offline
        Rick York
        wrote on last edited by
        #43

        There were only minor changes. For the most part, what you wrote is correct.

        "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

        1 Reply Last reply
        0
        • R Rob Philpott

          I can't make up my mind.

          Regards, Rob Philpott.

          S Offline
          S Offline
          Slow Eddie
          wrote on last edited by
          #44

          No. It's a state in the Northeast. :laugh:

          Oh Well.....

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups