Passing variables in Standard C

  • Thread starter Thread starter spursfan2110
  • Start date Start date
  • Tags Tags
    Standard Variables
Click For Summary

Discussion Overview

The discussion revolves around how to pass variables to functions in Standard C, particularly focusing on the differences between passing by value and passing by reference. Participants explore methods to modify variable values within functions and retain those changes after returning to the calling function.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant notes that when calling a function, arguments are passed by value, meaning that the original variables retain their values unless pointers are used.
  • Another participant explains that to modify a variable's value within a function, a pointer to the variable must be passed, allowing the function to dereference the pointer and change the value.
  • A code example is provided showing how to pass a variable by value and another by reference using pointers.
  • Some participants discuss the syntax of function calls and comments in C, with one noting that single line comments ("//") are part of modern C standards.
  • There is a question about whether Standard C supports references like C++, with participants indicating uncertainty about the current standards.

Areas of Agreement / Disagreement

Participants generally agree on the mechanics of passing variables by value and using pointers for modification, but there is uncertainty regarding the use of references in Standard C and some disagreement about the syntax and features of the language.

Contextual Notes

Some participants express uncertainty about the current state of Standard C, particularly regarding references and syntax, indicating a potential gap in knowledge about recent updates to the language.

spursfan2110
Messages
19
Reaction score
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
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.
 
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!
 
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.
 
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;
}
 
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:
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.
 
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:
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K