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

Asked

Viewed 1,353 times

6

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 the 3 bases to be returned. The intention is for me to enter a number like input and after that the program would call this function and calculate with that number being the basis, at the end I want the output be my number say, base 2, calculated at the power of 2, 3 and 4.

I tried to return base2, base3, base4 and it didn’t work. Why can’t this be done?

1 answer

7


There are some possibilities to pass data from one function to another.

A simple is to "return" a array values. This works if all 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]);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

You can also allocate to heap and return only the pointer to the values. I don’t like the solution because it makes the allocation and release asymmetrical, 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);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

You can also only pass the values as pointers, at the end of the execution the original variables used as arguments will be with the desired values. 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);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

And can also create a struct to return the values in a set and with heterogeneous types. It is a little more bureaucratic and does not pay to do if you are not using 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);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

It is not possible because a function only returns a value, it is a mathematical definition and adopted by languages. It is rare to return more than one value and if you do so they are probably very tied to each other and probably part of something bigger. Today there is language that simulates this possibility, but they are more complex languages because of this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.