How to return or extract more than one value from a function?

4
integerPower( int y ){
    int base2;
    int base3;
    int base4;
    int total;

    base2 = pow( y , 2);
    base3 = pow( y, 3);
    base4 = pow( y, 4);

When I call this function (I did not type return because that's the question) I would like all 3 bases to be returned. The intention is for me to type a number as input and after that the program would call that function and calculate with that number being the base, in the end I want the output to be the my number, base 2, calculated at the power of 2, 3, and 4.

I tried return base2, base3, base4 and it did not work. Why can not this be done?

    
asked by anonymous 13.12.2016 / 00:44

1 answer

6

There are some possibilities for passing data from one function to another.

A simple one is to "return" an array with the values. This works if all the values taken are of the same type.

#include <stdio.h>
#include <math.h>

void integerPower(int y, int ret[4]) {
    ret[0] = pow(y, 2);
    ret[1] = pow(y, 3);
    ret[2] = pow(y, 4);
    ret[3] = ret[0] + ret[1] + ret[2];
}
int main(void) {
    int valores[4];
    integerPower(5, valores);
    printf("%d\n", valores[0]);
    printf("%d\n", valores[1]);
    printf("%d\n", valores[2]);
    printf("%d\n", valores[3]);
}

See running on ideone and on CodingGround .

You can also allocate in heap and return only the pointer to the values. I do not like the solution because it makes asymmetric allocation and release, but it's still an alternative:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int *integerPower(int y) {
    int *ret = malloc(4 * sizeof(int));
    ret[0] = pow(y, 2);
    ret[1] = pow(y, 3);
    ret[2] = pow(y, 4);
    ret[3] = ret[0] + ret[1] + ret[2];
    return ret;
}
int main(void) {
    int *valores = integerPower(5);
    printf("%d\n", valores[0]);
    printf("%d\n", valores[1]);
    printf("%d\n", valores[2]);
    printf("%d\n", valores[3]);
    free(valores);
}

See running on ideone and on CodingGround .

You can also only pass the values as pointers, at the end of execution the original variables used as arguments will have the desired values. It has the advantage of being able to use heterogeneous types:

#include <stdio.h>
#include <math.h>

void integerPower(int y, int *base1, int *base2, int *base3, int *total) {
    *base1 = pow(y, 2);
    *base2 = pow(y, 3);
    *base3 = pow(y, 4);
    *total = *base1 + *base2 + *base3;
}
int main(void) {
    int base1, base2, base3, total;
    integerPower(5, &base1, &base2, &base3, &total);
    printf("%d\n", base1);
    printf("%d\n", base2);
    printf("%d\n", base3);
    printf("%d\n", total);
}

See working on ideone and on CodingGround .

And you can also create a struct to return the values in a set and with heterogeneous types. It's a bit more bureaucratic and it does not pay to do it if you do not use this struct elsewhere:

#include <stdio.h>
#include <math.h>

typedef struct integerPowerTuple {
    int base1;
    int base2;
    int base3;
    int total;
} IntegerPowerTuple;

IntegerPowerTuple integerPower(int y) {
    IntegerPowerTuple tupla = { pow(y, 2), pow(y, 3), pow(y, 4), tupla.base1 + tupla.base2 + tupla.base3 };
    return tupla;
}
int main(void) {
    IntegerPowerTuple tupla = integerPower(5);
    printf("%d\n", tupla.base1);
    printf("%d\n", tupla.base2);
    printf("%d\n", tupla.base3);
    printf("%d\n", tupla.total);
}

See working on ideone and on CodingGround .

Not possible because a function only returns a value, this is a mathematical definition and adopted by the languages. It is rare to return more than one value and if you do it probably they are very closely linked and probably part of something larger. Today there are languages that simulate this possibility, but they are more complex languages because of this.

    
13.12.2016 / 02:26