0

First of all you might wonder why I don't just use 1 function: The reasoning behind this is that I need this for a bigger project and want to keep the functions as separated as possible. I also don't want to use any global variables. This is the code I used to test this:

#include <stdio.h>

int Variables (int a, int b, int c) /* Example of a function wich stores    
variables with user input */
{ 
printf("input variables");
scanf("%d", &a);
scanf("%d", &b);
scanf("%d", &c);
printf("%d:%d:%d\n", a, b, c);
return a, b, c;
}

int main (void) 
{
int a, b, c;
/* I want to use the variables with userinput from the Variables function   */ 
printf ("\n%d:%d:%d\n", Variables(a, b, c));
return 0;
}

Unfortunately the output always returns only 1 of the inputs I stored in the variables from function Variables () and then 2 completely unrelated random numbers. I wonder how this could be fixed. Also bear with me on this I am pretty new to programming and c so I might have overlooked something. Thanks in advance for answers!

Edit: Apparently I got a lot of things wrong. Thanks for all the helpful answers! I guess the correct question to ask would have been "How to return more than 1 value from a function". Sorry for this unintended duplicate question. Maybe this question still helps people who ran into the same misconception (thinking that you can return more than 1 value from a function and thus searching for the wrong question on the internet).

Sandman
  • 17
  • 6
  • You must be confused on how functions work in c, you can only return one thing from a function. This case you return a only. And the other two %d are filled with random memory spaces. Im surprised this compiled – Tyler Oct 26 '17 at 15:36

3 Answers3

1

You cannot return more than one value. One option is to pass pointers to those variables to a function instead:

void Variables(int *a, int *b, int *c)
{ 
    printf("input variables");
    scanf("%d", a);
    scanf("%d", b);
    scanf("%d", c);
    printf("%d:%d:%d\n", *a, *b, *c);
}

int main(void) 
{
    int a, b, c;
    Variables(&a, &b, &c)
    printf("\n%d:%d:%d\n", a, b, c);
    return 0;
}

Notice there are additional & and *.

Another options is to do this using structures:

struct variables {
    int a, b, c;
};

struct variables Variables(void)
{
    struct variables var;
    scanf("%d", &var.a);
    scanf("%d", &var.b);
    scanf("%d", &var.c);
    return var;
}

int main(void) 
{
    struct variables var = Variables();
    printf("\n%d:%d:%d\n", var.a, var.b, var.c);
    return 0;
}

You have to understand comma , operator. This why your code didn't work as you expected. You can read about it here:

0

A clean and simple option is to call Function 3 times with only one parameter at a time . like this:

  #include <stdio.h>

  int Variables () 
  { 
  printf("input variables");
  scanf("%d", &a);
  return a;
  }

  int main (void) 
  { 
  printf ("\n%d:%d:%d\n", Var(),Var(),Var());
  return 0;
  }
krpra
  • 466
  • 1
  • 4
  • 19
0

You cannot return multiple values in C. The comma operator you used is quite useless here. Your function simply returns the last one.

You might split your function call in two steps and use pointers or return a struct, like this:

#include <stdio.h>

void Variables (int *a, int *b, int *c) /* Example of a function wich stores    
variables with user input */
{ 
    printf("input variables");
    scanf("%d", a);
    scanf("%d", b);
    scanf("%d", c);
    printf("%d:%d:%d\n", *a, *b, *c); // Dunno why you need to print twice...
}

int main (void) 
{
int a, b, c;
Variables(&a, &b, &c);
printf ("\n%d:%d:%d\n", a, b, c );
return 0;
}

How about?

j4x
  • 3,595
  • 3
  • 33
  • 64