Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Goto function across different functions

Tags:

c++

c

goto

How can I use goto across different functions? For example:

int main() {
    // ....
    REACH:
    // ....
}
    
void function() {
    goto REACH;
}
like image 412
Bhushanam Bhargav Avatar asked Jun 28 '13 04:06

Bhushanam Bhargav


People also ask

Can goto be used in between functions?

Restrictions in Goto Statement in PythonOne cannot use either of these statements to jump between functions and or modules. It cannot be used to jump into an except line, because there is no exception line in the first place.

Can goto jump from one function to another?

We cannot use “goto” to jump from main to other function. In fact we cannot use it jump from any function other than main to any other function.

Can goto statement transfer program control from one function to another function?

Goto statement helps in transferring the execution of the program from one line to another. That is why it is a jump statement, as it enables us to jump from one part of our program to another. This is done by using the goto statement and a label name as defined above in the syntax.

Why goto is not recommended?

NOTE − Use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Any program that uses a goto can be rewritten to avoid them.


1 Answers

You can't in Standard C++. From $6.6.4/1 of the C++ Language Standard

The goto statement unconditionally transfers control to the statement labeled by the identifier. The identifier shall be a label (6.1) located in the current function.

...or in Standard C. From $6.8.6.1/1 of the C Language Standard

The identifier in a goto statement shall name a label located somewhere in the enclosing function. A goto statement shall not jump from outside the scope of an identifier having a variably modified type to inside the scope of that identifier.

like image 97
Captain Obvlious Avatar answered Sep 22 '22 05:09

Captain Obvlious