From: Pavitra on
Say you have 15 parameters you use in multiple functions - some functions use 5 of the parameters, others use all 15, etc.

Is it better to declare the parameters as global and call them within the function OR create a struct that contains all the parameters which is passed as an input variable to the functions that need the parameters?

Or is there an even better way (besides passing individual parameters as input variable to the functions)?
From: Jan Simon on
Dear Pavitra,

> Say you have 15 parameters you use in multiple functions - some functions use 5 of the parameters, others use all 15, etc.
>
> Is it better to declare the parameters as global and call them within the function OR create a struct that contains all the parameters which is passed as an input variable to the functions that need the parameters?
>
> Or is there an even better way (besides passing individual parameters as input variable to the functions)?

I definitely prefer a struct. GLOBALs are a famous source for errors and in case of problems it is very hard to control, where the values of the global data have been changed.

Using 15 different variables does not have this problem, but it seems to be easy to confuse the order when calling a function. In the variables are packed in a struct, the order does not matter.

Submitting a struct to a subfunction does not copy the struct, but a pointer to the struct is delivered. Therefore structs are fast for transporting data. Although sometimes individual variables could be faster, the overall program time consists of:
program time = programming time + debug time + run time.
Using structs reduces the parts 1 and 2 and the 3rd part in a lot of cases.

Good luck, Jan
From: Pavitra on
Thanks Jan, that's very helpful. I was thinking along similar lines but wasn't sure if the struct would slow things down.