Home > Enterprise >  Is there a function that can jump over outside of a function?
Is there a function that can jump over outside of a function?

Time:05-28

Is there a function like example: goto that can jump over functions? Lets say I have this code :

#include <iostream>

void function1() {
    //dothis1
    //dothis2
    //jump to other function
}

int main() {
    std::cout<<"a";
    //go to here (jump)
    std::cout<<"b";
} 

CodePudding user response:

You can just call the another function to which you want to jump to as shown below. In the below program we call function1 from inside main and when function1 finishes the control will be automatically returned to the calling function main.

Then from inside function1 we call dothis1 and dothis2 and when dothis1 and dothis2 finishes control will be automatically returned to the calling function function1.

void dothis1()
{
    std::cout<<"entered dothis1"<<std::endl;
}
void dothis2()
{
    std::cout<<"entered dothis2"<<std::endl;
}
void function1()
{
    std::cout<<"entered function1"<<std::endl;
   //call dothis1 
   dothis1(); 
   
   //now call dothis2
   dothis2();
}


int main()
{
    //call function1 
    function1();
    return 0;
}

The output of the above program can be seen here:

entered function1
entered dothis1
entered dothis2
  •  Tags:  
  • c
  • Related