1) if the main function type made int for example, the return value of the function cannot be used at all as the execution is terminated after it directly.
2) any function we make to be executed must be called by the main function, or called by 2nd function called by main, or called by 2nd function called by 3rd function called by 4th function etc... called by main.
3) if the main function is called by itself or by other function this will result in an infinite loop.
1) Since main() is a function with a return type, it typically should have a return value. The return value of main() is only used to indicate whether or not the program executed successfully. return 0; is used to indicate that the program reached the end of main() and ran successfully.
2) Every program must start at main(). As such, to execute any function other than main(), the function must first be called from main() or other functions called from main().
3) The function main() should not be called from any function, including itself. Having other functions call themselves however, is perfectly legal and is known as recursion. If no base case is provided in the function definition of the function calling itself, it will result in an infinite loop, eventually crashing your program due to running out of available memory.
1) Yes, you (user) cannot use return value of main (which is guaranteeed to be EXIT_SUCCESS if you do not return manually). It is passed to runtime library and then to OS.
2) There is a caveat — functions called by static object constructors: static objects are either constructed before main() is called (usually by some bootsrapping initialization sequence which construct static objects and calls main()) or code to do that is inserted into main() before first line of it.
3) main is a special case and it is explicitely prohibited to call it by standard. Infinite loop is not a real problem, as functions called itself are commonly used: google recursion
i think this is impossible to do with the main function.
It is possible: there is nothing technically preventing you to do that. Aside from standard requirement:
2011 Standard wrote:
3.6.1/3 [basic.start.main]
The function main shall not be used within a program.
This is for a good reason: main() is a special function. It internal signature might not match declared one (standard requires that you declare main() with conforming signature, not what compiler will actually generate), have non-standard calling convention, etc. Or have some code which does construction/destruction of static objects done in it. So when you call main() recursively, you might find that your global variables were reset suddenly, or your cout stopped working as code destroying it was inserted at the return point of main() which was called inside your program and returned.
It is undefined behavior according to standard.
That means literally can happens. It might work as expected. It might not work as expected. It might crash. It might format your hard drive. It might do anything. And it will be acceptable behavior.
What makes wors, that actual behavior might cahnge if other person compiles your program, if you compile your program tomorrow, if some seemingly unrelated thing changes or if you simply run your program again.
Using undefined behavior is like speeding in your car down the street while drunk: you think that everything all right and often it will be alright. But eventually you will find either a policeman stopping you or wall you crash into.
EDIT:
@ne555 I would say that it is not actually value returned by main, but that value processed by OS (at least, some program startup/shutdown code is likely to touch this too)