[ARCHIVE!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Can't go anywhere without you - 4. - page 513

 
hoz:


This is the simplest variant. But I am observing articles written by one programmer and nothing is declared in his start function at all.

You can declare a static variable outside the functions and then you won't have to pass anything by references at all.

hoz:

If we pass a variable by reference into a function, we are working with a buffer of the initial variable itself that we pass instead of its separate duplicate. This means it doesn't matter which function we previously declared it in. Isn't it?

You're not thinking correctly. If you pass a variable as an argument to a function, you don't have to declare it to the function body - it's already declared in the argument. You must declare the function where it is passed from.
 
hoz:


This is the easiest way. But I'm watching articles by one programmer and nothing is declared in his start function at all.

Here's a link to his advisor:

http://www.forextrade.ru/media/Image/MQLabs/181_ag/ChannelByMACross_Expert.mq4

In particular, I'm very interested in how he writes code intelligently. So, I want to learn how to write everything in separate functions, and add only the essentials tostart without declaring variables there.

If we pass a variable by reference into a function, we are working with a buffer of the original variable, not with its separate double. This means it doesn't matter which function we previously declared it in. Isn't it?


He also intelligently wrote

read

Yes, change the line

if(!Trade(signal, priceForBuy, priceForSell))

to

if(!GetPriceToInput (signal))                                                                                                         
 
Roman.:

When are you going to get your own personal camper, Diman?)

You haven't made any money in a year in the real world, have you? You've got a lot of START amounts allocated to you there, haven't you?)



Soon.
 
Reshetov:

You can declare a static variable outside of functions and then you won't have to pass anything by reference at all.

You are thinking incorrectly. If you pass a variable as an argument to a function, it must not be declared in the function body - it is already declared in the argument. The function must be declared where it is passed from.


I mean, it doesn't matter if we pass it from the start function or from some other function.

In this case, I declared variables in thedouble GetPriceToInput(int signal) function and passedpriceForSell andpriceForBuy variables declared in the same double GetPriceToInput(int signal) function by reference intobool Trade(int signal, double& priceForBuy, double& priceForSell) function.

I added this function to thestart function and see that they are already undefined there. I want to know how to fix it.

Of course, I can declare these 2 variables at once in start as you've just said, but I can pass them directly from thedouble GetPriceToInput(int signal) function. This is what I mean.

 
hoz:


I mean, it doesn't matter if we are passing them from the start function or from some other function.

In this case, I declared variables in thedouble GetPriceToInput(int signal) function and passed the variablespriceForSell andpriceForBuy declared in the same double GetPriceToInput(int signal) function by reference into thebool Trade(int signal, double& priceForBuy, double& priceForSell) function.

I added this function to thestart function and see that they are already undefined there. I want to understand how to fix it.

Where did you pass them and why are you assigning values where you didn't pass them?

 
Reshetov:

Where did you pass them and where do you assign values to them?


Well, I got the values of the variables in the functiondouble GetPriceToInput(int signal)

Here:

double priceForSell = secondBarClosed - deltaForSell;
double priceForBuy = secondBarClosed + deltaForBuy;

I return these values:

return(priceForSell);
return(priceForBuy);

I.e. the values have already been returned by the function.

Then I send these values by reference to function bool Trade(int signal, double& priceForBuy, double& priceForSell):

This is quite obvious!

 
hoz:

I.e. the values have already been returned by the function.

Where did it return them, i.e. where in the code is the exact place where the values of these variables are returned by the function? And how can a function return more than one value if it was not passed by reference?
 
hoz:


Well I got the values of the variables in the functiondouble GetPriceToInput(int signal)

Here:

I return these values:

I.e. the values have already been returned by the function.

I then send these values by reference to function bool Trade(int signal, double& priceForBuy, double& priceForSell):

Everything is clear here!


Just read a textbook. Especially, read the section about scope of variables. And start with simple examples.

SZS It looks like you need to start with programming basics.

 

Reshetov
:

Where did it return, i.e. show exactly where in the code these variable values are returned by the function? And how can a function return more than one value if they were not passed to it by reference?

Honestly, I'm already completely confused. I don't even have any examples to see how others do it.

I understand that, apart from getting the values, you have to assign them to some other variables?

VladislavVG:


Just read the textbook. Especially read the section about scope of variables. And start with some simple examples.

Looks like you need to start with the basics of programming.


I don't need the basics. I am well aware of variable visibility. Nothing in the textbook about passing values of variables by reference. Why are you sending me there? Are you mocking me?

 

hoz:

I don't need the basics. I am well aware of variable visibilities. There's nothing in the textbook about passing variable values by reference. Why are you sending me there? Are you kidding me?

Goodbye, then. It's useless to teach a scientist. So let's not mock you if you are already eating it all up.
Reason: