Download MetaTrader 5

Returning values from functions

To add comments, please log in or register
Sign up for MQL5.community to post your Forum topic
Ingvar Engelbrecht
4499
Ingvar Engelbrecht 2014.09.22 11:58 

Returning a simple bool works except that if something went wrong an error messge would be nice to return too.

Returning a struct from a called function does not work if it contains objects (like string)  (Can not be copied)

I could pass a struct to the called function by reference containing string variables and update them in the called function

I could make the function as a class and inquire  some public variables in the class. (a bit more complex )

Maybe a way to simplify the class version would be to do all processing in the instamcing call?

Any other suggestions?

Alain Verleyen
Moderator
30640
Alain Verleyen 2014.09.22 12:41  
ingvar_e:

Returning a simple bool works except that if something went wrong an error messge would be nice to return too.

Returning a struct from a called function does not work if it contains objects (like string)  (Can not be copied)

I could pass a struct to the called function by reference containing string variables and update them in the called function

I could make the function as a class and inquire  some public variables in the class. (a bit more complex )

Maybe a way to simplify the class version would be to do all processing in the instamcing call?

Any other suggestions?

A suggestion for what exactly ? It's not clear what you are trying to do.
Ingvar Engelbrecht
4499
Ingvar Engelbrecht 2014.09.22 18:42  
angevoyageur:
A suggestion for what exactly ? It's not cleat what you are trying to do.

How to return multiple values including string from the called function to the calling function.

My first try was returning a struct that contained a string variable.

that does not work. What works is to pass a struct by reference to the called function and let the function pass results back that way.

That works.  Will probaby standardize on that unless there is a more formal or elegant method.

Laszlo Tormasi
2710
Laszlo Tormasi 2014.09.22 18:59  
ingvar_e:

How to return multiple values including string from the called function to the calling function.

My first try was returning a struct that contained a string variable.

that does not work. What works is to pass a struct by reference to the called function and let the function pass results back that way.

That works.  Will probaby standardize on that unless there is a more formal or elegant method.

You can use static char array instead of string. Not really elegant but this is the only way.
Alain Verleyen
Moderator
30640
Alain Verleyen 2014.09.22 19:32  
ingvar_e:

How to return multiple values including string from the called function to the calling function.

My first try was returning a struct that contained a string variable.

that does not work. What works is to pass a struct by reference to the called function and let the function pass results back that way.

That works.  Will probaby standardize on that unless there is a more formal or elegant method.

Either as a parameter by reference as you said, or as a pointer to a class (structure doesn't support pointer).
Ingvar Engelbrecht
4499
Ingvar Engelbrecht 2014.09.22 23:29  

Thanks  angevoyageur.

I  have also tried another method.  The functions I use are stored in a library. The library seems a bit similar to a class and it is "persistent"

If I declare a string object globally in the library (not inside one of my library functions ) I can set it in one function and retrieve it in another.

I can store the results from a function in these "global" variables and then use simple library functions to retrive them later (like "Set"  and "Get"). The library acts a bit like a class.

This is a bit more complex metod but workable.

Libraries are really useful since this feature means that it is possible to keep data in the library between calls to various library functions.

I have a separate library for logging to disk that is started in the init of the EA. The File Handle is stored in a global variable in the logging library so that

functions in another library can log without knowing the file handle

To add comments, please log in or register