• Support PF! Buy your school textbooks, materials and every day products Here!

Passing variables in Standard C

  • #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!
 

Answers and Replies

  • #2
33,521
5,199
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!
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.
 
  • #3
Borek
Mentor
28,412
2,815
Code:
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
33,521
5,199
Code:
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;
}
Borek, does Standard C use references (as in C++)? I haven't kept up with what's current these days in Standard C.
 
  • #6
33,521
5,199
If Standard C doesn't support the notion of references, here's the way with pointers.
Code:
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;
}
 
  • #7
nvn
Science Advisor
Homework Helper
2,128
32
In standard C, I thought it should be as follows. Please correct me if I am remembering incorrectly.

Code:
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:
  • #8
Hurkyl
Staff Emeritus
Science Advisor
Gold Member
14,916
19
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.
 
  • #9
nvn
Science Advisor
Homework Helper
2,128
32
Hurkyl: Good points. I could have retained the // comment delimiter, because it became standard in ISO C 1999. Thanks for mentioning that.
 
  • #10
Borek
Mentor
28,412
2,815
Borek, does Standard C use references (as in C++)? I haven't kept up with what's current these days in Standard C.
You are probably right that it doesn't :blushing:
 

Related Threads on Passing variables in Standard C

  • Last Post
Replies
2
Views
11K
  • Last Post
Replies
2
Views
1K
  • Last Post
Replies
0
Views
853
Replies
4
Views
15K
Replies
2
Views
2K
  • Last Post
Replies
1
Views
2K
Replies
14
Views
2K
Replies
2
Views
1K
Replies
1
Views
3K
  • Last Post
Replies
3
Views
5K
Top