Thread: A little help, if you would.

  1. #1
    transmitt
    Guest

    A little help, if you would.

    Suppose I have a program that is broken down into several functions. We will call these functions main(), a(), b(), c(), and
    d().

    These functions all call each other in order. Without returning a
    value through each of these functions, how could I get data
    stored in d() to a()? A pointer? Something else?

    I am asking because I have been writing a program lately, and I have been trying to use return to get all of the values to the first function. Makes me feel like I'm going around the world to get to my neighbors house.

    Thanks in advance.
    - Mitt.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Hmm...
    Code:
    #include <stdio.h>
    
    int sumGlobal;
    
    void addGlobal (int a, int b);
    int addReturn (int a, int b);
    void addReferenced (int a, int b, int * c); 
    
    int main (void)
    {
     int a, b;
     int sumReturn, sumReferenced;
    
     printf ("Two numbers please.\n");
     scanf ("%d%d", &a, &b);
     
     addGlobal(a, b);
     sumReturn = addReturn(a, b);
     addReferenced (a, b, &sumReferenced);
     
     printf ("%d %d %d\n", sumGlobal, sumReturn, sumReferenced);
     return 0;
    }
    
    void addGlobal (int a, int b)
    {
     sumGlobal = a + b;
     return;
    }
    
    int addReturn (int a, int b)
    {
     int c = a + b;
     return c;
    }
    
    void addReferenced (int a, int b, int * c)
    {
     *c = a + b;
     return;
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531
    Use structure pointer or array for many values to return..
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  4. #4
    transmitt
    Guest
    Thanks everyone.

    - Mitt

Popular pages Recent additions subscribe to a feed