Question

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!

Was it helpful?

Solution 2

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 */

OTHER TIPS

You can define a struct holding those values, and return that from your function. (Check your C reference of choice for documentation of structs.)

BTW, you don't return anything "in a header", you only return something "from a function". And defining a function in a header file is asking for trouble. The idea is to declare the function in the header, and to define it in a source (.c) file.

Since I didn't really understand what you're trying to do, excuse me for using my own example.

Header:

#ifndef POINT_H_
#define POINT_H_

struct point_t
{
    int x;
    int y;
};

struct point_t move_horizontal( struct point_t point, int offset );
struct point_t move_vertical( struct point_t point, int offset );

#endif

Source:

#include "point.h"

struct point_t move_horizontal( struct point_t point, int offset )
{
    point.x += offset;
    return point;
}

struct point_t move_vertical( struct point_t point, int offset )
{
    point.y += offset;
    return point;
}

Main:

#include "point.h"

int main()
{
    struct point_t some_point = { 0, 0 };
    struct point_t other_point = move_horizontal( some_point, 42 );
    return 0;
}

Non-sensical of course, but you might get the idea. Source and Main are two distinct compilation units, both of which include the header to know what they're talking about. The linker then puts them together, adds some runtime support, and generates your binary. (Your C book of choice should really have told you as much.)

Typcially you would define the function prototype in the header file and the implementation in the .c file.

As @DevSolar said you could define a struct and return this from the function.

struct Boat
{
    double V1, V2; 
    double Ug1, Ug2, Vg1, Vg2;
};

Alternatively you could pass in arguments by reference and modify the variable directly in memory through pointers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top