Passing variables in Standard C

In summary: If Standard C doesn't support the notion of references, here's the way with pointers.void function(int xx,int *yy){ xx=7; *yy=8;}HA! I was thinking the same thing! Thanks for the clarification!
  • #1
spursfan2110
21
0
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!
 
Physics news on Phys.org
  • #2
spursfan2110 said:
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
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;
}
 
  • #4
Awesome, thanks!
 
  • #5
Borek said:
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
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
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
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
Hurkyl: Good points. I could have retained the // comment delimiter, because it became standard in ISO C 1999. Thanks for mentioning that.
 
  • #10
Mark44 said:
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 to Passing variables in Standard C

1. What is the difference between passing variables by value and by reference in Standard C?

When passing variables by value, a copy of the variable's value is created and passed to the function. This means that any changes made to the variable within the function will not affect the original variable outside of the function. On the other hand, when passing variables by reference, the memory address of the variable is passed to the function. This allows the function to directly access and modify the original variable, making any changes permanent.

2. How do I pass an array as a parameter in Standard C?

To pass an array as a parameter in Standard C, you can declare the function with a pointer parameter and pass the array's name as the argument. This is because arrays in C are automatically converted to pointers when passed as function arguments. Alternatively, you can also specify the size of the array in the function parameter to prevent any unintended access to memory outside of the array's bounds.

3. Can I pass multiple variables of different data types to a function in Standard C?

Yes, you can pass multiple variables of different data types to a function in Standard C by specifying the data types of each parameter in the function declaration. You can then pass the corresponding arguments of the same data types when calling the function.

4. How can I return a variable from a function in Standard C?

To return a variable from a function in Standard C, you can use the return statement followed by the value you want to return. This value must be of the same data type as the function's return type. You can then assign the return value of the function to a variable when calling the function.

5. Is it possible to pass variables from one function to another in Standard C?

Yes, it is possible to pass variables from one function to another in Standard C by using either the pass-by-value or pass-by-reference method. You can pass the variable as an argument in the function call and use it in the receiving function. Additionally, you can also declare the variable as a global variable, making it accessible to all functions in the program.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
790
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
716
  • Engineering and Comp Sci Homework Help
Replies
1
Views
926
  • Engineering and Comp Sci Homework Help
Replies
3
Views
929
  • Engineering and Comp Sci Homework Help
Replies
23
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
Back
Top