'Conditional Branching - 'GoTo' Kluge ? - page 3

 
FourX:

As far as I have been able to figure out so far Raptor, their is nothing such as GoSub / Return commands or the ability to make such a function in MQL4. Hence my trying to figure out a kluge in MQL4 to carry out this capacity.

A function does the same as GOSUB/RETURN . . .

start()
  {
  Print("About to call a function called, a_function . . .");
  a_function();                                               //  GOSUB
  Print("Just returned from a function called, a_function");
  return(0);
  } 
   
   
   
void a_function()
   {
   Print("a_function has been called . . . ");
   return;                                          // RETURN
   }
   
   
   
 
RaptorUK:

A function does the same as GOSUB/RETURN . . .

I recognize that innumerable standard and custom functions can be 'called' & utilized in any one (appropriate) spot Raptor, but their is nothing in MQL4 such as a GoSub &/or GoTo any specific place within the program such as GoTo Line 185 or a specific Label/function etc, such as you yourself described. Then run it and then go back to the spot in the program that it was called from; or perhaps continue on from that called SubRoutine &/or program section/Function depending on the conditional outcome of that subroutine. As you stated, you used GoSub yourself for years and making a conditional jump past a couple of sequential lines of code is not the same thing at all. I don't want to keep debating this distinction that I we are already aware of that does not perform the same function at all. Thus this posting. We still haven't figure out how to utilize a true 'GoSub' that as an example perhaps jumps to an earlier spot in the program and re-utilizes the same needed function already established in the program rather than having to code it in repeatedly every time it is needed.

FYI: The MetaEditor crashes about 85% of the time that I copy text in it. This is on MANY different instances of MT4 and numerous different computers and on 3 different versions of windows. Without including both 32 & 64 bit systems that I have used MQL4 on. MT4 is fine, the MQL4 editor is not. Thus copying and pasting is not really a workable option for me. Even if it is, this still does not offer the same capacities as a 'GoSub' or GoTo' does.

 

Is there really any point to this ? It is a fact that mql4 is mql4, therfore when you code in mql4 you have to think mql4 there is no point trying to code in one language while considering how you would have solved it in another that supports different constructs. If you have a coding problem in mql4 that you believe could only be solved by the use of GoSub/Return and GoTo's I would like to see it, because I think you will find there is always more than one way to skin a cat.

 

FourX:

Even if it is, this still does not offer the same capacities as a 'GoSub' or GoTo' does.

I'm sorry, I am having a really hard time understanding your point of view that an MQL4 function and a GOSUB are different in any way. (Goto is whole other kettle of fish.)

Let's just stick with this Gosub thing. An MQL4 function is a subroutine. It may or may not return a value. All subroutine calls return to the next line in the main code from where they were called. I 100% agree with Raptor on this.

(I also agree with SDC's viewpoint above).

 

The while point of the thread is to see if it is possible to do conditional branching to ANYWHERE within the program, or even externally from other programs, to re-utilize program modules/functions repeatedly such as are provided for in other environments such as the ones stated early on: fGoTo, fGoSub. I already know that MQL4 does not directly provide for these. That is the whole point of this thread: is it possible to make a 'work-around (SubRoutine?)' etc to accomplish this functionality? Conceptually this also has similarities to the 'include' files in MQL4.

Similar to the work-around that we all use to compensate for the limited capacities of the MQL4 debugger by 'Alerting' or 'Printing' of the GetLastError() function values.

This is nothing at all like the normal MQL4 functions that contain a number conditional booleans such as if, while, loop, continue, return etc that can skip ahead of loop back within the function in that the workaround that I am seeking to provide for the capabilities such as the examples given from other environments where one can reuse any programming module / function repeatedly, often from (much) earlier in the program. Declare it once: use it may times over for the most part anywhere else within the program that one needs the functionality has already been provided for. The fGoSub in some environments allows users to import and utilize such other fSubRoutines, often from separate small programs that are modules that carry out a specific function. Perhaps somewhat similar to MQL4's 'Include' files.

Just because it isn't there now, does that mean that we shouldn't utilized this powerful programming functionality if we can create a 'work around' for it?

As we all know, there are numerous ways to program something to get the same result. This isn't a case of 'Show me your problem and I'll tell you how to do it without a fGoTo nor a fGoSub.' It is one of: IF we can create the functionality, then we have this additional programming flexibility and capacity that we can utilize in many different ways in many different situations.

 

I don't think you understand what a custom function in mql4 is . . . perhaps you can give an example of why a mql4 custom function does not do exactly what you are describing ? I think it does . . . I have plenty of functions that I reuse in many other EAs, Indicators and Scripts.

Think of start() as the program . . from anywhere within start I can call a custom function . . . when the function is done the program returns to the line after the line where the custom function was called . . . just like a GOSUB.

 

FourX:

The while point of the thread is to see if it is possible to do conditional branching to ANYWHERE within the program, or even externally from other programs, to re-utilize program modules/functions repeatedly such as are provided for in other environments such as the ones stated early on: fGoTo, fGoSub.

I am seeking to provide for the capabilities such as the examples given from other environments where one can reuse any programming module / function repeatedly, often from (much) earlier in the program.

Mql4 is an old style language, it's level is about 1990. But your goto and gosub are even older constructions, from about 1970. What is the point to make your programming style older than mql4? Much better to use a real programming language, like C#, through dll-s. Most inventions from 1990's and 2000's (like class, interface, object, design patterns like event and listener, programming methods like test driven programming, etc.) are for preventing code repetition, and helping you to quickly write reusable, reliable and readable codes. I think you should forget all this goto project.
 
FourX:

I recognize that innumerable standard and custom functions can be 'called' & utilized in any one (appropriate) spot Raptor, but their is nothing in MQL4 such as a GoSub &/or GoTo any specific place within the program such as GoTo Line 185 or a specific Label/function etc, such as you yourself described. Then run it and then go back to the spot in the program that it was called from;

the return inside a function will make it return to the place the function was called from. It is a 100% substitute of the old gosub/return. and additionally the function allows for passing values and returning values, they even have their own namespace and stack frame, so no global variable mess is needed anymore either. I really don't understand your problem. Please provide a concrete example (maybe in pseudo-code or BASIC) of what you actually want to do and we could show you how the same example would be done in a much clearer and more reusable and maintainable way with if/else, switch/case and function calls.

The only thing that is really missing are exceptions. Are you trying to simulate exceptions? You can't do this with GOTO either, you would also need to properly care about (and unwind) the call stack when doing this.

You CAN have multiple return statements within the same function and you can also break out of loops with break, this is the closest thing you can get to a GOTO in a structured language and these things (multiple exits) are already frowned upon by some strict purists also.

 
erzo:
Much better to use a real programming language, like C#, through dll-s.

C# is not recommended for making DLLs that interact with this flat low level C calling convention API. C# is perfect if you want to interact with other C# and various microsoft stuff but its not as general purpose as other environments. Its very specialized.

If you want to make DLLs that easily interface with C-calling conventions in a modern programming language then I suggest ObjectPascal (Free Pascal) or Delphi.

 
7bit:

C# is not recommended for making DLLs that interact with this flat low level C calling convention API.

I know java better, and considered to use, but it is hard to make a bridge. But if you not recommend C#, that I thought it would be easier, I will try java however. (Pascal is not my style.) Thank you for your advice!
Reason: