There is a way to do this, and it's called struct
. With struct
, you aggregate data and look at them as a single type. That's very nice, since you can give a name to each part of the data:
struct boat_params
{
double submerged_volume;
double length_in_U1,
length_in_U2,
length_in_V1,
length_in_V2;
/* etc */
};
and then you fill this information and return it:
struct boat_params submerged_volume(double L1, double L2, double Lavg, double H) {
//Boat parameters
struct boat_params params;
//Initialising V, the value to calculate
params.submerged_volume = 0;
//Volume Calculations (params.<fields>)
....
....
return params;
}
It makes sense to create a struct for the arguments of the function too, since they are many and they all look similar (all double
). Creating a struct
when you have many parameters helps the users of your function (and yourself) stay sane by easily assigning values to the parameters by their name, rather than some arbitrary order.
For example:
struct boat_data
{
double L1, L2; /* of course, give better names */
double Lavg;
double H;
};
struct boat_params submerged_volume(struct boat_data boat);
and the user would do:
struct boat_data boat;
struct boat_params params;
boat.L1 = ...;
boat.L2 = ...;
....
params = submerged_volume(boat); /* look how nice this looks */
Once you learn about pointers, you can make it more efficient by passing pointers rather than copying a bulk of data:
void submerged_volume(struct boat_data *boat, struct boat_param *params);
//Initialising V, the value to calculate
params->submerged_volume = 0;
//Volume Calculations (params-><fields>)
....
....
}
and later:
struct boat_data boat;
struct boat_params params;
boat.L1 = ...;
boat.L2 = ...;
....
submerged_volume(&boat, ¶ms); /* less nice, but efficient and still short */