3

I'm trying to pass variables from one function to another.

Like for example:

FuncA: takes in 3 inputs from the user and I want to use those 3 inputs in FuncB.

How would I do that? Would I just return the 3 values from FuncA and just pass it in as parameter of Func B?

Would I do something like this? **Without using pointers.

int FuncA(void);
int FuncB(int A, int B, int C, int D, int E);

int main(void)
{
    FuncA(void);
    FuncB(A,B,C);
}

int FuncA(void)
{
    printf("Enter 3 number:");
    scanf("%d %d %d" &A, &B, &C);
    return A, B, C;
}

int FuncB(int A, int B, int C)
{
    .............
}
Jim Balter
  • 16,163
  • 3
  • 43
  • 66
Thao Nguyen
  • 901
  • 7
  • 22
  • 42
  • 3
    `return A,B,C;` will only return `C` value. Left hand operand of comma expressions has no effect. – Mahesh Feb 27 '11 at 02:35

5 Answers5

7

Firstly, you can only return one value per function. This will probably make you ask, "how is it possible to get the values for A, B, and C from FuncA?"

How much do you know about pointers? The solution will be difficult to understand if you do not have a firm grip of what pointers are and how they work.

The solution is to pass 3 pointers (one for A, B, and C) so that FuncA can assign a value to them. This doesn't use the return keyword. It's assigning values at a specific location in memory which is, A, B, and C.

int FuncA(int* A, int* B, int* C)
{
    printf("Enter 3 number:");
    scanf("%d %d %d", A, B, C);
}

Now that A, B, and C contain the user input, we can pass those values to FuncB. You final code should look like this:

int FuncA(int* A, int* B, int *C);
int FuncB(int A, int B, int C);

int main(void)
{
    int A;
    int B;
    int C;

    FuncA(&A, &B, &C);
    FuncB(A, B, C);
}

int FuncA(int* A, int* B, int* C)
{
    printf("Enter 3 number:");
    scanf("%d %d %d", A, B, C);
}

int FuncB(int A, int B, int C)
{
    // ...
}
Marlon
  • 19,924
  • 12
  • 70
  • 101
5

One approach:

typedef struct {
  int a;
  int b;
  int c;
} ABC;

ABC funcA(void);
{
    ABC abc;
    printf("Enter 3 numbers: ");
    fflush(stdout);
    scanf("%d %d %d", &abc.a, &abc.b, &abc.c);
    return abc;
}

void funcB1(ABC abc)
{
    ...
}

void funcB2(int a, int b, int c)
{
    ...
}

int main(void)
{
    funcB1(funcA());  // one alternative

    ABC result = funcA();  // another alternative
    funcB2(result.a, result.b, result.c);
    ...
}
Jim Balter
  • 16,163
  • 3
  • 43
  • 66
4

I would setup your system like this:

void FuncA(int *A, int *B, int *C);
int FuncB(int A, int B, int C);

int main(void)
{
  // Declare your variables here
  int A, B, C;
  // Pass the addresses of the above variables to FuncA
  FuncA(&A, &B, &C);
  // Pass the values to FuncB
  FuncB(A, B, C);
}

void FuncA(int *A, int *B, int *C)
{ 
  printf("Enter 3 numbers: ");
  fflush(stdout);
  scanf("%d %d %d", A, B, C);
}

int FuncB(int A, int B, int C)
{
    //...
}
Quuxplusone
  • 23,928
  • 8
  • 94
  • 159
0

FuncA is returning an int. Assuming you want to call FuncB with A,B,C parametersa and return to the caller of FuncA whatever FuncB returns, you want something like this.

int FuncA(void)
{ 
printf("Enter 3 number:");
scanf("%d %d %d" &A, &B, &C);
return FuncB(A, B, C);
}
Bala R
  • 107,317
  • 23
  • 199
  • 210
-3

Declare A, B and C as global variables:

int A, B, C;
int FuncA(void);
int FuncB(int A, int B, int C);
....

and access them from any function, whether parameters or not. Or declare them static globals to limit the possible damage of global scoping.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
  • 2
    Global variables are considered bad, http://stackoverflow.com/questions/484635/are-global-variables-bad. – hlovdal Feb 27 '11 at 03:30
  • Question was closed, as whether global variables are good or bad is merely a matter of opinion and context. For instance their use may compile to less code on a system with 2k of memory or less. – hotpaw2 Dec 06 '18 at 18:58