0

I create the following code but the function "Count" does not return the value "x" and "y" properly because when I use the instruction "printf("Personas Mayores de edad: %d \n", Mayor);" prints 0.

throws me the following error messages:

main.c:23:1: warning: expression result unused [-Wunused-value]
    Mayor, Menor =  Contar(Edades); 

main.c:48:13: warning: expression result unused [-Wunused-value]
    return (x, y);
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//Prototipado de 3 funciones
int Contar(int Edades[]);
void promEdad(int Edades[]);
void MasCantidad(int Mayor, int Menor);

//Funcion principal
int main(void) {
  int Edades[50];
  int i, Mayor, Menor;
  puts("*** Este programa simula la entrada de la edad de 50 personas (de forma aleatoria entre 1 y 50) ****\n\n");
  system("pause");

  srand(time(NULL)); //semilla para rand
  for (i = 0; i < 50; i++) { //Genera los valores aleatorios y los asigna al arreglo
    Edades[i] = rand() % 50 + 1;
    printf("Edad generada: %d \n", Edades[i]);
  }

  Mayor, Menor = Contar(Edades); //llamado a funcion
  promEdad(Edades); //llamado a funcion

  printf("Personas Mayores de edad: %d \n", Mayor);
  printf("Personas Menores de edad: %d \n", Menor);

  MasCantidad(Mayor, Menor);

  system("pause");
  return 0;
}

//Definicion de funcion 1
int Contar(int Edades[]) {
  int i, x = 0, y = 0;

  for (i = 0; i < 50; i++) { //Cont
    if (Edades[i] > 17) {
      x = x + 1;
    } else {
      y = y + 1;
    }
  }
  return (x, y);
}

//Definicion de funcion 2
void promEdad(int Edades[]) {
  int i;
  int suma = 0;
  for (i = 0; i < 50; i++) {
    suma = suma + Edades[i];
  }
  printf("El promedio de edad es: %d \n", suma / 50);
}

//Definicion de funcion 3
void MasCantidad(int Mayor, int Menor) {
  if (Mayor == Menor) {
    puts("Es la misma cantidad de menores y mayores");
  } else if (Mayor > Menor) {
    puts("Hay mas mayores de edad");
  } else {
    puts("Hay mas menores de edad");
  }
}
2
  • C does not have multiple return values. Commented Aug 24, 2020 at 1:38
  • Next time you can save some trouble by googling for question title site:stackoverflow.com first before writing the question, i.e. return multiple function values to main in C site:stackoverflow.com, then opening the first result. Commented Aug 24, 2020 at 5:10

3 Answers 3

2

return (x, y) will execute the expression x, throw away the result, execute the expression y, and then return the value of the expression y.

To return multiple values, you will either want to use pointer arguments:

void foo(int *a, int *b) {
  *a = 3;
  *b = 4;
}

int main() {
  int x, y;
  foo(&x, &y);
  assert(x == 3);
  assert(y == 4);
}

Or use a struct:

struct Object {
  int a;
  int b;
};

struct Object foo() {
  struct Object obj;
  obj.a = 3;
  obj.b = 4;
  return obj;
}

int main() {
  struct Object obj = foo();
  int x = foo.a;
  int y = foo.b;
}
Sign up to request clarification or add additional context in comments.

2 Comments

To add to this, if you use a struct you can also pass a pointer to the struct and have your function edit it, e.g. void foo(struct Object *ptr){ ptr->a = 3; ptr->b=4; } This avoids dynamically allocating extra memory if the struct is large.
@ChristianGibbons: Whoops. Thanks for noting. Fixed.
0

Most of the time you want to return only one value in C.

If you want to return two values you have two choices:

1/ Clean one: create a struct that contains x and y and return the struct.

2/ Dangerous: Make use of the bits contain in one int:

// In the callee function:
return x | (y << 16)

And then in the caller function

x = ret & 0xffff
y = ret >> 16

1 Comment

Note that option 2) depends on the size that your platform has decided to make int, so is not very portable.
0

You can use an array to return both variables, it is really the use of pointers but with this example I simplify it:

#include <stdio.h>

int* Contar(int Edades[], int results[]) {
    int i, x = 0, y = 0;

    for (i = 0; i < 3; i++) { //Hasta 3 pues solo hay 3 valores
        if (Edades[i] > 17){
            x = x + 1;
        }else{ 
            y= y + 1;
        }
    }
    results[0] = x;
    results[1] = y;
    return results;
}

int main(){
    int arrayPrueba[3];
    arrayPrueba[0] = 15; 
    arrayPrueba[1] = 18; 
    arrayPrueba[2] = 19;

    int value[2];//En este arreglo se guardaran los resultados
    //Primer array tiene las edades y en el segundo se guarda el resultado
    Contar(arrayPrueba, value);  
    printf("Mayores de Edad: %d\n", value[0]);
    printf("Menores de Edad %d\n", value[1]);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.