1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Passing variables in Standard C

  1. Mar 10, 2010 #1
    Hey, so I am teaching myself the C language to get a head start for a class I have to take later and am a little bit confused about something. I understand that if you call a function from within your int main, the variables you pass are actually copies of the originals and when the program returns to intmain, they will retain the last values they before the outside function was called, provided what you passed was not a pointer to an array, correct? So is there a way to edit the values of variables inside of external functions and have them retain that edited value when they return to int main, other than returning a value and saving that?

    I hope this was clear, thanks for the help!
     
  2. jcsd
  3. Mar 10, 2010 #2

    Mark44

    Staff: Mentor

    Yes, when you call a function, the arguments are passed by value, which means that the values of the variables or expressions are what are passed to your function. Another parameter passing mechanism is passing by reference, in which a passed variable can have its value changed.

    Unlike some other programming languages, C is strictly call by value, but it is possible for a function to modify its passed parameters. The way you do this is to pass a pointer to the variable. In this case, the function has the address of the variable, and can deference the pointer to actually change what is pointed to. Hope that helps.

    BTW, call it the main function, but not int main or intmain. The int indicates that this function returns an int value.
     
  4. Mar 10, 2010 #3

    Borek

    User Avatar

    Staff: Mentor

    Code (Text):

    void function(int xx,int &yy)
    {
      xx = 7;
      yy = 8;
    }

    int main()
    {
      int x,y;

      x = 3;
      y = 4;
      function(x,y);
    // now x is 3 and y is 8

      return 0;
    }
     
  5. Mar 10, 2010 #4
    Awesome, thanks!
     
  6. Mar 10, 2010 #5

    Mark44

    Staff: Mentor

    Borek, does Standard C use references (as in C++)? I haven't kept up with what's current these days in Standard C.
     
  7. Mar 10, 2010 #6

    Mark44

    Staff: Mentor

    If Standard C doesn't support the notion of references, here's the way with pointers.
    Code (Text):

    void function(int xx,int *yy)
    {
      xx = 7;
      *yy = 8;
    }

    int main()
    {
      int x,y;

      x = 3;
      y = 4;

      // Pass x by value and y by reference.
      // I.e., pass a copy of x, but pass the address of y.
      function(x, &y);
      // now x is 3 and y is 8

      return 0;
    }
     
     
  8. Mar 10, 2010 #7

    nvn

    User Avatar
    Science Advisor
    Homework Helper

    In standard C, I thought it should be as follows. Please correct me if I am remembering incorrectly.

    Code (Text):
    void function(int xx,int *yy)
    {
       xx=7;
       *yy=8;
    }

    int main()
    {
       int x,y;
       x=3;
       y=4;
       function(x,&y);
       /* now x is 3 and y is 8. */
       return(0);
    }
    EDIT: And now I see Mark44 and I posted at about the same time.
     
    Last edited: Mar 10, 2010
  9. Mar 10, 2010 #8

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    I believe the single line comments ("//") are in modern standard C.

    AFAIK, return has never had function call like syntax -- its usage has always been return <expression>. Of course, "(0)" is a perfectly valid expression.
     
  10. Mar 10, 2010 #9

    nvn

    User Avatar
    Science Advisor
    Homework Helper

    Hurkyl: Good points. I could have retained the // comment delimiter, because it became standard in ISO C 1999. Thanks for mentioning that.
     
  11. Mar 11, 2010 #10

    Borek

    User Avatar

    Staff: Mentor

    You are probably right that it doesn't :blushing:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Passing variables in Standard C
Loading...