Click here to Skip to main content
15,892,927 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Is main() a callback function? Pin
PIEBALDconsult22-Jan-20 5:43
mvePIEBALDconsult22-Jan-20 5:43 
GeneralRe: Is main() a callback function? Pin
ZurdoDev22-Jan-20 5:52
professionalZurdoDev22-Jan-20 5:52 
GeneralRe: Is main() a callback function? Pin
Rob Philpott22-Jan-20 6:05
Rob Philpott22-Jan-20 6:05 
GeneralRe: Is main() a callback function? Pin
honey the codewitch22-Jan-20 7:06
mvahoney the codewitch22-Jan-20 7:06 
GeneralRe: Is main() a callback function? Pin
Gerry Schmitz22-Jan-20 7:06
mveGerry Schmitz22-Jan-20 7:06 
GeneralRe: Is main() a callback function? Pin
Gary Wheeler22-Jan-20 7:14
Gary Wheeler22-Jan-20 7:14 
GeneralRe: Is main() a callback function? Pin
Mark_Wallace22-Jan-20 7:22
Mark_Wallace22-Jan-20 7:22 
GeneralRe: Is main() a callback function? Pin
Stefan_Lang23-Jan-20 1:35
Stefan_Lang23-Jan-20 1:35 
Only if used as such, e. g. in this code:
C++
#include <iostream>

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)

GeneralRe: Is main() a callback function? Pin
Slow Eddie23-Jan-20 7:01
professionalSlow Eddie23-Jan-20 7:01 
GeneralThought of the Day Pin
OriginalGriff22-Jan-20 4:50
mveOriginalGriff22-Jan-20 4:50 
GeneralRe: Thought of the Day Pin
Mike Hankey22-Jan-20 4:54
mveMike Hankey22-Jan-20 4:54 
GeneralRe: Thought of the Day Pin
lopatir22-Jan-20 5:00
lopatir22-Jan-20 5:00 
GeneralRe: Thought of the Day Pin
Richard Deeming22-Jan-20 5:02
mveRichard Deeming22-Jan-20 5:02 
GeneralRe: Thought of the Day Pin
DRHuff22-Jan-20 5:25
DRHuff22-Jan-20 5:25 
GeneralRe: Thought of the Day Pin
W Balboos, GHB22-Jan-20 5:27
W Balboos, GHB22-Jan-20 5:27 
GeneralRe: Thought of the Day Pin
Daniel Pfeffer22-Jan-20 6:06
professionalDaniel Pfeffer22-Jan-20 6:06 
Generalbetter than expected Pin
lopatir22-Jan-20 4:29
lopatir22-Jan-20 4:29 
GeneralRe: better than expected Pin
OriginalGriff22-Jan-20 4:52
mveOriginalGriff22-Jan-20 4:52 
GeneralRe: better than expected Pin
DRHuff22-Jan-20 5:29
DRHuff22-Jan-20 5:29 
GeneralRe: better than expected Pin
dandy7222-Jan-20 6:08
dandy7222-Jan-20 6:08 
GeneralRe: better than expected Pin
DRHuff22-Jan-20 6:23
DRHuff22-Jan-20 6:23 
GeneralRe: better than expected Pin
honey the codewitch22-Jan-20 7:11
mvahoney the codewitch22-Jan-20 7:11 
GeneralRe: better than expected Pin
Jacquers22-Jan-20 19:56
Jacquers22-Jan-20 19:56 
GeneralA million of Martians Pin
kalberts22-Jan-20 4:01
kalberts22-Jan-20 4:01 
GeneralRe: A million of Martians Pin
Daniel Pfeffer22-Jan-20 4:18
professionalDaniel Pfeffer22-Jan-20 4:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.