Prayuktibid
Prayuktibid

Reputation: 189

How can I go to the beginning of main function from a sub function in C programing language?

I am writing a C code, this code consist with many sub functions and also within a sub function there is another sub function after execution of this sub sub function I need to go back in the beginning of main function.

My question is how can I exit from this function and come back main function?

As this code is too big That is why I have not include this code in here.I think return; cant do this thing because it returns only to the function where it got function call. I am beginner in C programming so please suggest what thing I have to do for this?

enter image description here

Upvotes: 0

Views: 1780

Answers (4)

Clifford
Clifford

Reputation: 93466

Jumping across function boundaries is contrary to structured programming, and while possible (using setjmp(), longjmp()) is inappropriate and unnecessary in this case.

You need not treat function calls as simple sub-routines - they take arguments and return values; the return value in particular is useful in this case for providing information to the caller for controlling program flow.

Based on your (somewhat confusing) diagram:

typedef enum tStatus
{
    STATUS_FAIL ;
    STATUS_SUCCESS ;
} tStatus;

void function1( void ) ;
tStatus function2( void ) ;
tStatus function1n( void ) ;

int main()
{
    for(;;)
    {
        // statement1
        // statement2

        function1() ;
        if( function2() == STATUS_SUCCESS )
        {
            // statement3
        }
    }

   return 0 ;
}

void function1( void )
{
    // do something
}

tStatus function2( void )
{
    // statement1
    // statement2

    tStatus status = function1n() ;
    if( status == STATUS_SUCCESS )
    {
        // statement n
    }

    return status ;        
}

tStatus function1n( void )
{
    tStatus status = STATUS_FAIL ;

    // statement1

    if( !condition)
    {
        status = STATUS_SUCCESS ;
        // statement n
    }

    return status ;
}

If you follow the code flow, you will see that when in function1n() condition is true then STATUS_FAIL is returned to function2(), which returns STATUS_FAIL to main() which then causes control flow to return to the top of the main() loop.

Note that most simple bare-metal embedded systems do not return from main() so an indefinite loop is the normal implementation when no OS or RTOS is used.

Upvotes: 1

Ed Heal
Ed Heal

Reputation: 59997

THIS ANSWER NEEDS A HEALTH HAZARD - THIS IS THE WRONG WAY TO PROGRAM

You can use setjmp and longjmp to do this.

But PLEASE do everything in your power to avoid this - by thinking about the design of the code beforehand

Upvotes: 2

pmg
pmg

Reputation: 108978

Make the inner function return a value rather than void. Use that value in the middle function to determine if you should return to main.

int main(void) {
    f2();
    return 0;
}

void f2(void) {
    if (f1()) return;
    /* ... */
}

int f1(void) {
    if (condition true) return 1;
    /* ... */
    return 0;
}

Upvotes: 0

Jeffrey Ross
Jeffrey Ross

Reputation: 141

C maintains a stack of nested functions. If your main program calls function one and that calls function two, you can only get back to the main program by unwinding the stack using a return statement in each function (therefore from two back to one, and then back to main). So I don't think you can do what you're wanting. You can terminate the program completely with the exit statement.

Upvotes: 2

Related Questions