Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 1 month ago

How do I return more than 1 integer value in C?

The program calls the Square function in the main function. Then in the Square function, the program will ask the user for an integer value (n) and it will calculate the square of that value (sq).

I want the program to return the integer and squared value back to the main function so that in the main function, it can print out a final message, print("The square of %d is %d.", n, sq); I am only able to return one of the two values.

Here's the function prototype - int Square(void);

2 Answers

Relevance
  • 1 month ago
    Favorite Answer

    If you insist on that prototype, the only way to get another value out is to use a global variable. 

    Global variables cause problems and are best avoided; but if there's no choice then define the extra return value as a variable at the top of your source code, not in a function and before any function that uses it.  Somewhere near the prototypes is a good spot.

    static int unsquared_value;

    ...

    int Square()

    {

        unsquared_value = 42; // replace this with code to read the value from the user

        return unsquared_value*unsquared_value;

    }

    ...then in main() or somewhere:

        int the_square = Square();

        printf("The square of %d is %d.\n", unsquared value, the_square);

    Besides being ugly, globals make functions harder to use.  A more normal thing to do is to use pointer arguments to the function:

    void InputAndSquare(int *original, int *squared)

    {

        *original  = 42;

        *squared = (*original) * (*original);

    }

    ... then in main(), create local variables for the return values and call with pointers:

        int value, squared_value;

        InputAndSquare(&original, &square);

        printf("The square of %d is %d.\n", value, squared_value);

    The div() library function (defined in <stdlib.h>) uses a different way to return multiple values.  That's a function to divide two int values and return both a quotient and remainder.  It defines a struct type (div_t) and return an instance of that as a function result.  You could take that idea:

        typedef struct {int original; int squared} SquareType; // define the struct

        SquareType Square(); // new prototype, must be after type definition

    ....

        SquareType answer = Square();

        printf("The square of %d is %d.\n", answer.original, answer.squared);

    I mentioned a design problem at the beginning.  You're asking one function to do two apparently unrelated things and return the values of each.  Generally, it's better to have a function do one specific thing; and have the name of the function give a clue as to what that specific thing is. 

        int original = GetInputValue();

        int squared = Square(original);

    Your design had a function named Square, with a primary purpose of reading an input value.  The name doesn't tell anyone reading your code about what that function really does.  Using separate functions (one to get the value, and another to square a value) gets rid of the two-return-value issue AND lets you give each function a name that tells the reader of your code what that function does.

  • 1 month ago

    Why not just do this:

    cin << num;

    numSq = Square(num);

    print("The square of %d is %d.", num, sq);

    Otherwise, you are going to have more code and more confusing code.

Still have questions? Get your answers by asking now.