Function that returns an Array?

 

Hello all,

With all the questions I've been posting, it's obvious I'm a newbie... thanks for all the help.

I'd like to create a FUNCTION that returns an ARRAY, but not sure of the syntax. I tried this:

double function_name[] (double var1, double var2)

{

// do something to create array here

}

But it didn't like the square brackets.

Also, how do you DECLARE and INIAILIZE a 2 dimentional array?

Thanks for the help!

-charliev

 

hi

Check out

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkarraystutorial.asp

and the lessons here in this forum.

I don't know if you can declare an array function - you sure can pass arrays to functions and modify them in the function.

a 2 dimensional array,

int my_array[2,2]

 

Return an array

charliev:

Hello all,

With all the questions I've been posting, it's obvious I'm a newbie... thanks for all the help.

I'd like to create a FUNCTION that returns an ARRAY, but not sure of the syntax. I tried this:

double function_name[] (double var1, double var2)

{

// do something to create array here

}

But it didn't like the square brackets.

Also, how do you DECLARE and INIAILIZE a 2 dimentional array?

Thanks for the help!

-charliev

charliev,

You can not return an ARRAY the way you wrote your code:

To return an array use code like this:

void function_name (double& MyArray[], double var)

{

//Fill MyArray with the elements you want.

//Don't use RETURN keyword

}
 

Thank you CodersGuru!!!

I've gotten so much help from you and other members, this is really a great board!! Your MQL4 Course has made all the difference!

Thanks agian.

-charliev

 

Keep Programming!

charliev:
Thank you CodersGuru!!!

I've gotten so much help from you and other members, this is really a great board!! Your MQL4 Course has made all the difference!

Thanks agian.

-charliev

charliev,

You're welcome! . KEEP PROGRAMMING!

 
codersguru:
charliev,

You can not return an ARRAY the way you wrote your code:

To return an array use code like this:

void function_name (double& MyArray[], double var)

{

//Fill MyArray with the elements you want.

//Don't use RETURN keyword

}

very cool...

 
codersguru:
charliev,

You can not return an ARRAY the way you wrote your code:

To return an array use code like this:

void function_name (double& MyArray[], double var)

{

//Fill MyArray with the elements you want.

//Don't use RETURN keyword

}

Dont forget to declare the MyArray[] as a global array as your passing it by referance to the function.

 
cockeyedcowboy:
Dont forget to declare the MyArray[] as a global array as your passing it by referance to the function.

cowboy,

I did not know that mq4 could use pointers until recent. I ran some tests, and found that you don't need to declare the var as global because it uses the pointer to change the variables in memory. So:

int start()

{

int a=1;

addone(a);

}

void addone(int& var)

{

var++;

}

// a is now 2[/PHP]

is the same as doing...

[PHP]

int start()

{

int a=1;

a=addone(a);

}

int addone(int var)

{

return(var+1);

}

// a is now 2
 

Nick

Nice hearing from you.

Nicholishen:
cowboy,

I did not know that mq4 could use pointers until recent. I ran some tests, and found that you don't need to declare the var as global because it uses the pointer to change the variables in memory.

Yes, but not really, what you did in the second example was to send the function the value of 'var a' when you called the function and re-asigned the 'var a' a new value of the return of function call. not quite the same.

Try this have a = 1; b=0; and call the function and pass it 'a' and have b= the return from the function. you will see that 'a' at the end will still equal 1 and 'b' will now equal 2. as 'a' was passed as value not referance and the function can not change its value. only if its passed by referance(&). If you are right then both 'a' and 'b' should equal 2 in the above case.

Arrays are handled a little differently then simple parameters. and declearing functions in a seperate libaray, You will find that it handles it differently expecially with arrays. If you are giving any of the parameters default values watch out if you are introducing the function from a libaray or include file as these defaults are not accepted in those cases.

If you look at codersguru post he has placed the '&' symble in the parameter declaration which passes the array by referance with out it complete arrays cannot be passed. You can pass single elements by value, but cannot pass complete arrays by value.

There is one more thing that cought my eye, your construction of the two procedural calls themselfs. I have not seen to meny people use the void type. There are two different types of Procedural Calls, MT only supports one directly the second one is more or less indirectly supported with the use of the void type declaration. The two procedural calls are Function Procedures and Routine Procedures.

your first example is a routine procedures as it returns nothing but performs a duty when called. It can be a stand alone statement as in your example or an element of a compound statement. It can not be assigned to a variable. The second example is a function procedure that returns a value and must be assigned to a variable, it cannot be used as a stand alone statement. The only thing that MT did was to introduce the void type to indirectly impliment routine procedures.

Leaving out the return statement although acceptible in your first example is not a good programing habit to get into. when useing the void type you should end the procdure with the 'return;' statment without the () just the return key word. This will make your code forwardly adaptable if MT impliments the full rules for procedural calls.

I must say you have learned a lot on the programing end of things sence your first posts here. If everone would give only half the efford you have given they would be better off,

The CockeyedCowboy

 

what is the proper way to declare the function and prototype when using separate libraries?

 

OK, I am successfully adding functions with pointers to libraries, and it works as long as I am passing an array. When I try to do it just referencing a single variable, I does not work in a library but works when the function is in the same source file as the start(). Can anyone advise as to why?

Reason: