0

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";
} 
AbhiDevs
  • 23
  • 3
  • Just call the another function to which you want to jump to? – Jason May 28 '22 at 10:43
  • You can 'call' another function from within a function. Look up what this means. – user438383 May 28 '22 at 10:43
  • 3
    Call `function1` in `main`, when it returns it will jump back to `main`. – Paul Rooney May 28 '22 at 10:45
  • @AnoopRana nonono, i mean like the function have several code on it and i just want it to jump over the specific code, not the entire function – AbhiDevs May 28 '22 at 10:50
  • Just for a start, what control-flow elements of C++ are you aware of? Like `if`, `else`, `return` and function calls? – Ulrich Eckhardt May 28 '22 at 10:51
  • @UlrichEckhardt if – AbhiDevs May 28 '22 at 10:52
  • 1
    Wouldn't you just add another parameter to the function and use that to decide where to really start execution(jump/goto to)? Not that it really makes any sense to do so. What are you trying to achieve? (We know how you're trying to do it - not helping.) What problem do you see this scheme as solving? – enhzflep May 28 '22 at 10:57
  • If that's all, keep studying your C++ tutorial. Sorry, but SO can't (and doesn't want to) teach the basics of a language. BTW: Type "[c++] essential resources" into the search bar above. – Ulrich Eckhardt May 28 '22 at 10:58
  • 1
    Divide the function you want to jump into into several separate functions. Then you can just call the one specifically. – Sebastian May 28 '22 at 10:59

1 Answers1

0

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
Jason
  • 36,170
  • 5
  • 26
  • 60
  • wait, gonna test it! – AbhiDevs May 28 '22 at 10:56
  • @AbhiDevs Sure, i have also given a [demo link](https://www.onlinegdb.com/Ni9Hy6lgt) where you can directly try out the code. – Jason May 28 '22 at 10:56
  • can u do it in another function outside main()? – AbhiDevs May 28 '22 at 10:57
  • @AbhiDevs In the example, `dothis1` and `dothis2` are separate functions that are different from `main`. I thought this is what you wanted when you said "another function outside main". If not, can you clarify what you mean by "another function outside main". Also you might want to refer to a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Jason May 28 '22 at 10:59
  • hmm can i do it like : in function 1 it will call function 2 and in function 2 it will call function 1. I got an error when compiling with that style. – AbhiDevs May 28 '22 at 11:02
  • @AbhiDevs Yes, you can call the functions recursively. See [demo](https://onlinegdb.com/G0WChCSvz). But note you have make sure that the recursion ends for a given condition otherwise the two functions will keep on calling each other and the program will keep on printing "entered function1" , "entered function2" as shown in [this demo](https://onlinegdb.com/G0WChCSvz). – Jason May 28 '22 at 11:06
  • I get a compiler error :/ – AbhiDevs May 28 '22 at 11:10
  • @AbhiDevs To make sure that you don't have a typo in your code, just copy/paste the code that i have given at [here](https://onlinegdb.com/G0WChCSvz). I have tested the program and it compiles fine. You must be getting some other error. Can you share the error description here. – Jason May 28 '22 at 11:15
  • nonono, i read the error and the error is like this : function1 was not declared in this scope (this in on void function2()) – AbhiDevs May 28 '22 at 11:19
  • may i show you my code? do you have a discord or smthing? – AbhiDevs May 28 '22 at 11:22
  • @AbhiDevs You can just paste you code [here](https://onlinegdb.com/CKiU8BDdi) and then click on share and a link will be presented to you and then you can share that link with me here. – Jason May 28 '22 at 11:23
  • [link](https://onlinegdb.com/q21ARPEnh) here – AbhiDevs May 28 '22 at 11:29
  • @AbhiDevs As i suspected, you did not exactly copy/pasted the code that i gave in the link. In particular, the problem is that you were missing the 2 function declarations that i provided at the very starting of my code. Look closely at line `8` and `9` in [this demo](https://onlinegdb.com/yGohTtZNR) link. I have added some comment there so that you can spot it easily. – Jason May 28 '22 at 11:35
  • wait a second... – AbhiDevs May 28 '22 at 11:35