Can you call a function main
inside a switch - of course, you can call any function you want. It would be very limiting if you couldn't.
Is your syntax valid? no. You aren't calling a function given your syntax, you are declaring a function called main
that returns an int. C++ doesn't support local functions (and even if it did, not inside a case statement). The correct syntax would be.
case WM_LBUTTONDOWN:
{
MessageBox(hWnd, L"test", L"test", MB_OKCANCEL | MB_ICONINFORMATION);
main();
break;
return 0;
}
Now the actual question is when this would ever make sense. It wouldn't. Let's be very clear here - main is the entry point to your program. It is called automatically by the runtime when your app starts and should initialize and run your app. Until it returns your program will continue to run. In this regard it would never make sense to call the entry point while your program is already running. Firstly it would reinitialize your app. Secondly it would block until your program starts its termination process and then it would "continue" anyway. Hence this wouldn't logically make any sense.
Note that it does make sense that there might be logic inside main
that you want to call somewhere else in your code. That is exactly what functions are for. Move any logic you want to use outside of main
into one or more helper functions and then call the helper functions wherever you need them. main
should initialize, run and clean up your app - that's it.
Finally, it looks like you are creating a Windows app, not a console app. You appear to be inside a message pump. Therefore it wouldn't make sense to call main
as that is for console apps. Windows apps use Winmain
instead. Going back to what was stated previously, if you are converting an existing console app to a WIndows app and there is functionality in the original app's main
that you want to reuse then move that logic into helper functions. Then call the helper functions from your message pump. This will make your app easier to understand and more flexible.