Society of Robots - Robot Forum

Software => Software => Topic started by: junior000 on February 21, 2008, 08:32:43 AM

Title: using GOTO statement!!
Post by: junior000 on February 21, 2008, 08:32:43 AM
is using GOTO statement not recommended in c language???




Title: Re: using GOTO statement!!
Post by: BANE on February 21, 2008, 11:54:40 AM
what program are you using?

bane
Title: Re: using GOTO statement!!
Post by: junior000 on February 22, 2008, 01:34:25 AM
to jump from one loop to another
Title: Re: using GOTO statement!!
Post by: Tsukubadaisei on February 22, 2008, 03:18:32 AM
It is not recommended if you abuse it. Your code will become difficult to read and thus difficult to debug. It is called Spaghetti code.
Title: Re: using GOTO statement!!
Post by: Ro-Bot-X on February 22, 2008, 04:00:10 AM
GOTO is used to jump to a subroutine in Basic. When that subroutine ends, there is no turning back to the place the jump was called. If one needs to jump back, the GOSUB is used.

In C you can define functions and instead of GOTO or GOSUB you will just call the function. So you can have a few functions that you can call from different places in the main program. If you need to return from the place you call the function, use RETURN at the end of the function. If not, just call another function that will continue the program, or call Main function. The nice part of C is that you can return variable values at the end of the function. This way you can make a ReadSensor(s) function and return the value(s) to the Main program.
You can also group all the motors and sensors functions in a header file that you can import right at the beginning of the program, in the "#include" section.