-3

How would I pass a variable from one function to another in C? For example I want to pass the sumOfDice from function roll and use sumOfDice for the passLine function: Here is my code:

    int roll()
   {
    int dieRollOne = (random()%6) + 1; 
    int dieRollTwo = (random()%6) + 1;

    printf("On Dice One, you rolled a: %d\n", dieRollOne);
    printf("On Dice Two, you rolled a: %d\n", dieRollTwo);

    int sumOfDice = dieRollOne + dieRollTwo;
    printf("The sum of you Dice is: %d\n", sumOfDice);
    return sumOfDice
  }
int passLine(int sumOfDice)
{
       // other code
       printf("the sum of dice is: %d\n", sumOfDice);
}

I would have used global variable for sumOfDice, but we are not allowed to. Would I have to pass with asterisk, like: int *num;

klutt
  • 30,332
  • 17
  • 55
  • 95
na2193
  • 23
  • 1
  • 12
  • Is this a homework question? – Tersosauros Feb 16 '16 at 23:39
  • this is a homework project to create a game of craps. I am just stuck on how to pass variables because I'm still learning how to code in C from java – na2193 Feb 16 '16 at 23:42
  • You can only pass values between a function, and the function that called it. (In both directions) – user253751 Feb 16 '16 at 23:46
  • Your code (As posted) doesn't have a `main()` function (entry point). (As with Java), your program needs to have an entry point. As it is, we cannot see where you are calling **either**of these functions! – Tersosauros Feb 16 '16 at 23:46
  • I did not put a main function on this post because I thought it was irrelevant. I just wanted to know how to pass a function without using the main function. So do I call the passLine function from the main and pass in roll? – na2193 Feb 17 '16 at 00:05
  • In order to pass `sumOfDice` to `passLine`, you have to call `passLine` within the `roll` function passing `sumOfDice` as the parameter. There is *NO* reason the parameter name need be `sumOfDice` (e.g. `int passLine(int foo)` is fine). Then in `roll` , you simply call `passLine (sumOfDice);` which passes `sumOfDice` to `passLine`. – David C. Rankin Feb 17 '16 at 02:46

1 Answers1

1

Nasim, this is one of the most fundamental concepts in C. It should be well-explained in any C book/tutorial you use. That said, everybody needs to learn somewhere. In order to pass values to/from a function, you start with the declaration of a function.

A function in C may receive any number of parameters but may only return a single value (or no value at all). A function requires those parameters specified in its parameter list. A function declaration takes the form of:

type name (parameter list);

The type is the return type for the function (or void). The parameter list contains the type of variables that are passed to the function. While you will normally see a parameter list in a declaration that contains both the type and name, only the type is required in the declaration. A function definition provides the function code and the function return. The parameter list for a function definition will contain both the type and name of the parameters passed.

(note: you may see old K&R function definitions without any type relying on the fact that the default type is int. That type definition/parameter list is obsolete. see Function declaration: K&R vs ANSI)

Now that you have had a Cliff's-notes version of how to declare/define a function, a short example should illustrate passing/returning values to and from a function. This first example shows the function definitions that precede the main function. In this case no separate declaration is required:

#include <stdio.h>

int bar (int x) {
    return x + 5;
}

int foo (int a) {
    return bar(a);
}

int main (void) {

    int n = 5;
    printf ("\n n = %d, foo(%d) = %d\n\n", n, n, foo(n));

    return 0;
}

(note: function bar is placed before function foo because function foo relies on bar. A function must always have at minimum a declaration before it is called.)

Another example showing the common use of providing function declarations before main with the function definitions below would be:

#include <stdio.h>

int foo (int);
int bar (int);

int main (void) {

    int n = 5;
    printf ("\n n = %d, foo(%d) = %d\n\n", n, n, foo(n));

    return 0;
}

int foo (int a) {
    return bar(a);
}

int bar (int x) {
    return x + 5;
}

(note: even though the function foo is defined before bar here, there is no problem. Why? Because bar is declared (at the top) before foo is called. also note: the declaration are shown with the type only, just to emphasize a point, you will normally see int foo (int a); and int bar (int x); as the declarations.)

Use/Output

The output of both is:

$ ./bin/func_pass_param

 n = 5, foo(5) = 10

I hope this has cleared up some of the basics for you. If not, you can ask further, but you are far better served finding a good C book or tutorial and learning the language (at least the basics) before you attempt to compile and run a program -- it will take you far less time in the long run.

Community
  • 1
  • 1
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85