0

Is main(); function included as loop statement? for example i have a program with subprograms then if I finished a subprogram i will use main(); function to go back from the main program. is that considered as loop?
thanks!!

5
Contributors
10
Replies
12
Views
5 Years
Discussion Span
Last Post by _avishek
Featured Replies
  • 1
    WaltP 2,905   5 Years Ago

    > Calling main() from within main() would be an example of recursion, and not looping. > ------ > its not loop. Its just "calling the main function" we don't consider it a loop. Actually, it's called a *recursive loop*, so yes, recursion **is** a loop. > A loop is a … Read More

0

I am not sure that I understand your question completely.

A loop is a programming construct that repeatedly executes some code while a certain condition is true. Examples are the "for" and "while" constructs.

If you are asking whether something like this is possible:

for(;;) {
    main();
    program();
}

then I would have to say "No". The main() function is the entry point for an application; each program would have its own main() function. On the other hand, something like this should be possible:

#include <stdlib.h>

int main(void)
{
    for(;;) {
        system("program1");
        system("program2");
    }
}

where "program1" and "program2" are different programs.

0

I think I understand your question a bit better, now that I have re-read it.

Calling main() from within main() would be an example of recursion, and not looping. Even so, I don't think recursion on main() is a very good idea. From what I know, it is outright illegal in C++.

0

function to go back from the main program. is that considered as loop?

its not loop. Its just "calling the main function" we don't consider it a loop.

0

main() is the function from where compilation begins.
So if you call main() within main(), then it will go infinitely recursing, and is forbidden in C++.

0

yes, completely agree with np complete
calling main() in main() causes recursion and which never terminates the program.
some compilers don't even allow this.
If you try this out in Borland C you'll get an error saying

cannot call "main" from within the program in function main().
1

Calling main() from within main() would be an example of recursion, and not looping.

its not loop. Its just "calling the main function" we don't consider it a loop.

Actually, it's called a recursive loop, so yes, recursion is a loop.

A loop is a programming construct that repeatedly executes some code while a certain condition is true.

Which is exactly what recursion does.

But the rest of the information here (for the most part) is correct enough. You should never call main() as a function.

Edited by WaltP

0

Actually, it's called a recursive loop, so yes, recursion is a loop.

yeah! you are right. It forms a recursive loop.

0

so yes, recursion is a loop

Correct me if I'm wrong, but my understanding was that a new stack frame would be pushed on to the call stack for each recursive call to a function; this does not happen with a loop construct.

0

You are correct.

That still doesn't change the fact that recursion is a loop. The term loop depends on code executed, not on data addresses used

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.