# Returning multiple values in header file

user3460758

I have a couple of questions all relating to the same code. In this code, I am trying to return the value for V, Ug1, Ug2, Vg1, and Vg2 from the "submerged_volume" function. Then, I want to use these values in the "centre_of_buoyancy" function. From that function, I want to return two values: Uc, and Vc. Finally, I want to call these functions using the header file in my main, and use the returned values from the functions for further calculations! I have not included the main body as it just has long calculations, so for the sake of space, here's a summarised version of my code:

``````#ifndef DATE_H_
#define DATE_H_

double submerged_volume(double L1, double L2, double Lavg, double H) {

//Boat parameters
double V1, V2;
double Ug1, Ug2, Vg1, Vg2;      //lengths in U and V direction in relation to gravity
double V;                       //Submerged volume

//Initialising V, the value to calculate
V = 0;

//Volume Calculations
....
....

return V, Ug1, Ug2, Vg1, Vg2, V1, V2;
}

double centre_of_buoyancy(double Ug1, double Ug2, double Vg1, double Vg2, double V1, double V2);
//Calculations for Uc and Vc
.....
.....
return Uc, Vc;

}

#endif
``````

I understand that this won't work as I can't return multiple variables. My question is, is there some way that I can do this? I'm very new to C and am not sure exactly how to use things like this!

Shahbaz

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, &params);  /* less nice, but efficient and still short */
``````

Collected from the Internet

edited at