# Passing variables in Standard C

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!

Related Engineering and Comp Sci Homework Help News on Phys.org
Mark44
Mentor
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.

Borek
Mentor
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;
}

Awesome, thanks!

Mark44
Mentor
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.

Mark44
Mentor
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;
}

nvn
Homework Helper
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:
Hurkyl
Staff Emeritus
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.

nvn