Use static variables or define new in loop, what is best?

 
Hi,

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
 

I would go with option 2.......just use the variables that are already defined......why create more variables that already exist somewhere else.

Also the only reason to pass arguments to a function is if you are going to call the function multiple times with different parameters.

 
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.
 

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