Forex
Google

Go Back   Forex Trading > Discussion Areas > Metatrader 4
Forex Forum Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Register in Forex TSD!
Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time).
Click here to register and get more information

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-05-2006, 03:38 PM
charliev charliev is offline
Junior Member
 
Join Date: Jan 2006
Posts: 25
charliev is on a distinguished road
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




Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-05-2006, 10:41 PM
cardio cardio is offline
Senior Member
 
Join Date: Sep 2005
Location: St Louis, MO, USA
Posts: 176
cardio is an unknown quantity at this point
hi

Check out

http://msdn.microsoft.com/library/de...ystutorial.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]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-06-2006, 09:42 AM
codersguru's Avatar
codersguru codersguru is offline
Senior Member
 
Join Date: Oct 2005
Posts: 987
codersguru has a spectacular aura aboutcodersguru has a spectacular aura aboutcodersguru has a spectacular aura about
Arrow Return an array

Quote:
Originally Posted by 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:

PHP Code:
void function_name (doubleMyArray[], double var)
   {
   
//Fill MyArray with the elements you want.
  //Don't use RETURN keyword
   

__________________
Hope it helps !
Coders' Guru
Senior MQL programmer:
www.xpworx.com/custom.htm
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-08-2006, 12:36 AM
charliev charliev is offline
Junior Member
 
Join Date: Jan 2006
Posts: 25
charliev is on a distinguished road
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-09-2006, 01:33 PM
codersguru's Avatar
codersguru codersguru is offline
Senior Member
 
Join Date: Oct 2005
Posts: 987
codersguru has a spectacular aura aboutcodersguru has a spectacular aura aboutcodersguru has a spectacular aura about
Thumbs up Keep Programming!

Quote:
Originally Posted by 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!
__________________
Hope it helps !
Coders' Guru
Senior MQL programmer:
www.xpworx.com/custom.htm
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 01-13-2007, 04:48 AM
Nicholishen's Avatar
Nicholishen Nicholishen is offline
Senior Member
 
Join Date: Dec 2005
Posts: 531
Nicholishen is on a distinguished road
Quote:
Originally Posted by codersguru
charliev,

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

To return an array use code like this:

PHP Code:
void function_name (doubleMyArray[], double var)
   {
   
//Fill MyArray with the elements you want.
  //Don't use RETURN keyword
   

very cool...
__________________
"Anyone who has never made a mistake has never tried anything new." -Albert Einstein
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-13-2007, 03:22 PM
cockeyedcowboy's Avatar
cockeyedcowboy cockeyedcowboy is offline
Senior Member
 
Join Date: Nov 2005
Posts: 267
cockeyedcowboy is on a distinguished road
Quote:
Originally Posted by codersguru
charliev,

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

To return an array use code like this:

PHP Code:
void function_name (doubleMyArray[], 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 01-13-2007, 07:16 PM
Nicholishen's Avatar
Nicholishen Nicholishen is offline
Senior Member
 
Join Date: Dec 2005
Posts: 531
Nicholishen is on a distinguished road
Quote:
Originally Posted by 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:

PHP Code:
int start()
{
   
int a=1;
   
addone(a);
}
void addone(int& var)
{
   var++;
}
// a is now 2 
is the same as doing...

PHP Code:
int start()
{
   
int a=1;
   
a=addone(a);
}
int addone(int var)
{
   return(var+
1);
}
// a is now 2 
__________________
"Anyone who has never made a mistake has never tried anything new." -Albert Einstein

Last edited by Nicholishen : 01-13-2007 at 07:20 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 01-13-2007, 08:31 PM
cockeyedcowboy's Avatar
cockeyedcowboy cockeyedcowboy is offline
Senior Member
 
Join Date: Nov 2005
Posts: 267
cockeyedcowboy is on a distinguished road
Nick

Nice hearing from you.

Quote:
Originally Posted by 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

Last edited by cockeyedcowboy : 01-13-2007 at 08:38 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 01-14-2007, 09:45 PM
Nicholishen's Avatar
Nicholishen Nicholishen is offline
Senior Member
 
Join Date: Dec 2005
Posts: 531
Nicholishen is on a distinguished road
what is the proper way to declare the function and prototype when using separate libraries?
__________________
"Anyone who has never made a mistake has never tried anything new." -Albert Einstein
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


Similar Threads
Thread Thread Starter Forum Replies Last Post
iCustom function homicida Questions 55 06-07-2008 04:36 AM
Data in Array converted to btw 0 and 1 ( normalisation) ! icm63 Metatrader 4 4 01-22-2007 01:25 AM
Array search helper functions for your use... cubesteak Metatrader 4 3 09-04-2006 04:55 AM


All times are GMT. The time now is 10:16 PM.