you will not be able to access variables declared inside the start() function from inside another function. You would need to declare them global (outside of all functions).
However, it is generally considered good style to always avoid global variables whenever possible (you can't avoid some things like external parameters or indicator buffers but everything else should not be global), declare your variables local to the functions where they are used and always pass needed variables to other functions as function arguments. Don't be afraid of long function argument lists. Keep everything as local and as self-contained as possible, never let your functions interact with the outside world through something other than the arguments list and the return value to minimize side effects, decouple the different parts of your code, avoid a whole class of nasty bugs and improve modularity and reusability.
However, it is generally considered good style to always avoid global variables whenever possible (you can't avoid some things like external parameters or indicator buffers but everything else should not be global), declare your variables local to the functions where they are used and always pass needed variables to other functions as function arguments. Don't be afraid of long function argument lists. Keep everything as local and as self-contained as possible, never let your functions interact with the outside world through something other than the arguments list and the return value to minimize side effects, decouple the different parts of your code, avoid a whole class of nasty bugs and improve modularity and reusability.
Yup, good point 7bit. I like that way of thinking. I would be going with option 2 only because the variables already exist.
McKeen, also just to be sure, a static variable is something totally different from a variable declared with a global scope. Actually reading your first sentence again, perhaps option 1 would be better. I missed the bit about the variables being in the start function.
Thank you both for your comments, makes me understand it all much clearer...
/ McKeen
/ McKeen
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Suppose I am calling a function which will need 3 variables already defined in the start-function.
Is there any reason for using theese variables when calling the function to replace them with function-defined variables?
For example,
I have double Price, datetime Time and bool LongTrades == true already defined in start-function when calling "Funtion1".
Shold I use:
void Function1 (Price, Time, LongTrades); (creating new local variables in the function)
or just call it with:
void Function1 (); ( and then use the already defined variables within the function )
What is to be preffered?
Thanks in advance for any ideas...
/ McKeen