Passing Variables between functions

Discussion in 'C Programming' started by tigrfire, Nov 6, 2005.

  1. tigrfire

    tigrfire Guest

    I don't want to use global variables to do this, but I'm stumped on how
    to pass a variable between one function to another, so if someone could
    help out, that'd be appreciated. Here's an example of what I need to
    accomplish:

    (note: Don't worry about whether or not the functions work properly,
    but if you want to comment, I won't stop you.)

    void playGame ()
    {
    int sum; /* sum of rolled dice */
    int myPoint; /* point earned */
    int balance; /* user's current balance */
    ..
    ..
    ..
    }


    void getWager()
    {
    int wager;

    printf("Enter wager: ");
    scanf("%d", &wager);

    while (wager > balance || wager <= 0)
    {
    printf("Your wager must not exceed your current balance.\n");
    printf("Enter a new wager: ");
    scanf("%d", &wager);
    }
    }
    tigrfire, Nov 6, 2005
    #1
    1. Advertising

  2. tigrfire

    Malcolm Guest

    "tigrfire" <> wrote > void playGame ()
    > {
    > int sum; /* sum of rolled dice */
    > int myPoint; /* point earned */
    > int balance; /* user's current balance */
    > .
    > .
    > .
    > }
    >
    >
    > void getWager()
    > {
    > int wager;
    >
    > printf("Enter wager: ");
    > scanf("%d", &wager);
    >
    > while (wager > balance || wager <= 0)
    > {
    > printf("Your wager must not exceed your current balance.\n");
    > printf("Enter a new wager: ");
    > scanf("%d", &wager);
    > }
    > }
    >


    Two ways

    1)

    int getWager()
    {
    int wager;

    /* code to print message etc */
    scanf("%d", &wager);

    return wager.
    }

    in the caller

    theWager = getWager();



    2)

    /*
    get the wager, return 0 on success, -1 on fail
    */
    int getWager(int *wager)
    {
    int result;
    /* code to print message goes here */

    result = scanf("%d", wager);

    /* scanf tells you how many fields were read successfully*/
    if(result == 1)
    return 0;
    else
    return -1;
    }

    /* in the caller */

    int ok;
    int wager;

    ok = getWager(&wager);
    if(ok == -1)
    {
    /* code to handle error goes here. User didn't enter a valid wager */
    }


    The second way is better, but the first will work.
    Malcolm, Nov 6, 2005
    #2
    1. Advertising

  3. tigrfire

    Netocrat Guest

    On Sun, 06 Nov 2005 12:33:54 -0800, tigrfire wrote:

    > I don't want to use global variables to do this, but I'm stumped on how
    > to pass a variable between one function to another, so if someone could
    > help out, that'd be appreciated. Here's an example of what I need to
    > accomplish:
    >
    > (note: Don't worry about whether or not the functions work properly,
    > but if you want to comment, I won't stop you.)
    >
    > void playGame ()
    > {
    > int sum; /* sum of rolled dice */
    > int myPoint; /* point earned */
    > int balance; /* user's current balance */


    getWager(balance);

    You needn't call it with a variable named "balance" - any expression that
    has int type or can implicitly be safely converted to int is fine.

    This is pretty fundamental stuff and comp.lang.c isn't the best place to
    learn it. A text-book or online course would be better.

    > .
    > .
    > .
    > }
    > void getWager()


    void getWager(int balance)

    > {
    > int wager;
    >
    > printf("Enter wager: ");
    > scanf("%d", &wager);
    >
    > while (wager > balance || wager <= 0)
    > {
    > printf("Your wager must not exceed your current balance.\n");
    > printf("Enter a new wager: ");
    > scanf("%d", &wager);
    > }
    > }


    --
    http://members.dodo.com.au/~netocrat
    Netocrat, Nov 6, 2005
    #3
  4. tigrfire

    Guest

    tigrfire wrote:
    > I don't want to use global variables to do this, but I'm stumped on how
    > to pass a variable between one function to another, so if someone could
    > help out, that'd be appreciated. Here's an example of what I need to
    > accomplish:
    >
    > (note: Don't worry about whether or not the functions work properly,
    > but if you want to comment, I won't stop you.)
    >
    > void playGame ()
    > {
    > int sum; /* sum of rolled dice */
    > int myPoint; /* point earned */
    > int balance; /* user's current balance */
    > .
    > .
    > .
    > }
    >
    >
    > void getWager()
    > {
    > int wager;
    >
    > printf("Enter wager: ");
    > scanf("%d", &wager);
    >
    > while (wager > balance || wager <= 0)
    > {
    > printf("Your wager must not exceed your current balance.\n");
    > printf("Enter a new wager: ");
    > scanf("%d", &wager);
    > }
    > }



    This is one of the first things you should have been taught when
    learning C. Functions are usually taught in the third or fourth class.
    You are therefore obviously not a C programmer. This is BASIC stuff.

    A function is:

    return_type function_name (parameter_type parameter, ...) {
    function_body
    }


    Therefore passing a value into a function:

    getWager(balance);

    getting a value out of the function:

    wager = getWager();

    pass a value in and get a value out:

    wager = getWager(balance);

    Once you understand this, then you can start playing with pointers.
    , Nov 7, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ILLOGIC
    Replies:
    1
    Views:
    347
    Rob Williscroft
    Jun 1, 2004
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,570
    Victor Bazarov
    Jun 21, 2004
  3. Steven T. Hatton

    Passing member functions to C functions?

    Steven T. Hatton, Oct 4, 2004, in forum: C++
    Replies:
    7
    Views:
    1,301
    David Hilsee
    Oct 7, 2004
  4. Yodai

    passing variables through functions...

    Yodai, Jan 15, 2004, in forum: C Programming
    Replies:
    13
    Views:
    573
  5. Replies:
    3
    Views:
    362
    Eric Sosman
    Jul 20, 2007
Loading...

Share This Page