#1
  1. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12

    returning variable*s* from functions?


    if a function that's called from your main function needs to use, modify and return numerous int variables (like i and j counter/tracker types of variables along with a few others, for example), is there any other way to get these back to the main function and back into their respective variables, other than making them global, or semi-global (external variables declared static) or is making them global/semi-global the best way?

    this is a problem that has cropped up for me in various little exercise bits of code i'm doing, so i can only imagine the problem getting worse, and i'm wondering how this sort of thing is dealt with generally? it's advised that you pass variables as much as possible via function parameters and return them via return, but the parts of my code that lend themselves well to function-ising often need to make use of and return more than one variable. arrays don't need returning (which has always seemed a bit odd to me - the difference between single and array variables), but ints do need returning and it seems that only one can be returned.

    this is mainly with variables that already exist in the main loop like as i mentioned earlier i & j type counters, in mind.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    if you want to modify variables inside a function, pass them by reference or pointer:
    Code:
    void add(int a, int b, int &result) {
      result=a+b;
    }
    or:
    void add(int a, int b, int *result) {
      *result=a+b;
    }
  4. #3
  5. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    ok thanks. is that c or c++? if it's c++, sorry i meant c. if it is in c i can't get it to work right now but i haven't tried particularly hard yet
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    If you'd rather just return modified versions of a certain variable (instead of directly modifying the variable itself), simply bundle the modified variables together into a struct and return that struct. The caller can pick the pieces out of the struct that it wants.
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.
  8. #5
  9. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    M. HIrsch's second example is the C variant. That is:

    Code:
    void add(int a, int b, int *result) {
      *result = a+b;
    }
    Since you're new to pointers, maybe you'll find this example helpful:

    Code:
    void swap (int *a, int *b) {
       int tmp;
       tmp = *a;
       *a = *b;
       *b = tmp;
    }
    
    int main() {
       int i, j;
    
       i = 12;
       j = 15;
       printf("i = %d, j = %d\n", i, j);
       swap (&i, &j);
       printf("i = %d, j = %d\n", i, j);
    }
    Good luck :)
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  10. #6
  11. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    right so the answer is pointers basically. as you mentioned, i haven't got that far yet. i had thought that i'd pretty well much covered variables in the book i'm learning from, and therefore missed how this should be done. but i haven't missed it. i just haven't got to it yet. ok, thanks. :)
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    so the "big fun" (pointers) still lies ahead of you... :D

    rule #1:
    never give up.
    rule #2:
    if you still want to, go to #1
  14. #8
  15. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    rule #1:
    never give up.
    rule #2:
    if you still want to, go to #1
    Ack! You should know better than to use goto! :eek:
    Code:
    while (!given_up) {
        try_harder();
    }
    :D
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    no, i meant:

    while (given_up || !given_up) {
    try_harder();
    }

    ;)
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    37
    Rep Power
    12
    also to add to this post it is always more effiecent to pass by refrence rather then value. This is also helpful information cause if you need to pass an array you can only pass it by refrence. also when passing an array you must include the array size. okay that is my two cents on this topic
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    13
    Balance,

    If you haven't studied pointers yet and still find a need to modify many variables in a function, there's a good chance that your program logic has problems.

    There's a couple of good rules to stick by here. First, a function should only try to do one thing. Second, a function should be kept short. These two rules play nicely into each other.

    It sounds like you are a student at University, so I'm going to suggest looking your professor or your TA up during office hours, bringing a recent homework assignment with you, and ask about better ways to structure your code. I make my living programming and I still talk with my boss and my colleagues about how to structure my programs.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/

IMN logo majestic logo threadwatch logo seochat tools logo