Change a Variable from inside a C Function

Variables are meant to be changed. But changing a variable from inside a function is a tricky one in C-like programming. Let’s consider this code snippet.

#include <stdio.h>

void change_value(int v) {

  printf("Value of input variable in function change_value(): %d.\n", v);
  v = v + 100;
  printf("Value of input variable in function change_value() is changed to: %d.\n", v);
}

int main(){
  int x = 5;
  
  change_value(x);
  printf("Value of x after calling change_value(): %d.\n", x);
}

Here we started with a variable, x, of 5 and tried to change that from inside the function change_value() by adding 100 with it.

The expectation is that the varible x will be changed to 105 after the change_value() function. But here is the output.

Value of input variable in function change_value(): 5.
Value of input variable in function change_value() is changed to: 105.
Value of x after calling change_value(): 5.

From this output, we can see that even though the variable is changed to 105 inside the function, the value of x remains unchanged (5) after coming out of the function. That’s why when we printed x in main() after the function call, it printed 5.

So this is not the right way to change a vaiable from inside a function. In the next sections, we’ll explore the possible ways to achieve that.

Using Global Variable

The easiest way to change a variable from inside a function is to make the variable global. The global variables can be accessed or changed from any function. Let’s consider this code snippet.

#include <stdio.h>

int x = 0;

void change_value() {

  printf("Value of input variable in function change_value(): %d.\n", x);
  x = x + 100;
  printf("Value of input variable in function change_value() is changed to: %d.\n", x);
}

int main(){
  x = 5;
  
  change_value();
  printf("Value of x after calling change_value(): %d.\n", x);
}

We don’t need to pass the variable as a parameter to the change_value() function as global variable can be accessed directly from any function. Here we’ll get the expected output.

Value of input variable in function change_value(): 5.
Value of input variable in function change_value() is changed to: 105.
Value of x after calling change_value(): 105.

Global variable change from anywhere gets reflected everywhere. That’s why the change, we did in the change_variable() function, can be seen in the main() function.

Even though it look quite simple, it is not a recommended way. In general, global variable usage is not a good practice unless it is absolutely required. A practical program would be completely unmanageable if we make all variables global.

Returning the Changed Value

As changing a variable inside a function does not get reflected outside, we can return the changed value from the function. Then we can use that returned value outside the function.

int change_value(int v) {

  printf("Value of input variable in function change_value(): %d.\n", v);
  v = v + 100;
  printf("Value of input variable in function change_value() is changed to: %d.\n", v);
  return v;
}

int main(){
  int x = 5;
  
  x = change_value(x);
  printf("Value of x after calling change_value(): %d.\n", x);
}

We’ll get the expected output here.

Value of input variable in function change_value(): 5.
Value of input variable in function change_value() is changed to: 105.
Value of x after calling change_value(): 105.

Here the function returned the changed value. Problem with this approach is that if you want to change multiple variables in a single function call, you can not do that. Because C function can return only one variable at a time. There are tricks to return multiple values but things will be complicated.

Using Call by Reference

This probably is the most elegant solution. Instead of passing the value of a variable, we can pass the reference or pointer of the variable as input parameter of the function. Using that pointer we’d be able to change the variable. This mechanism is also know as ‘Call by Reference’. Here is the code.

#include <stdio.h>

void change_value(int* v) {

  printf("Value of input variable in function change_value(): %d.\n", *v);
  *v = *v + 100;
  printf("Value of input variable in function change_value() is changed to: %d.\n", *v);
}

int main(){
  int x = 5;
  
  change_value(&x);
  printf("Value of x after calling change_value(): %d.\n", x);
}

And here is the expected output.

Value of input variable in function change_value(): 5.
Value of input variable in function change_value() is changed to: 105.
Value of x after calling change_value(): 105.

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

Leave a Reply

Your email address will not be published. Required fields are marked *

1
6
1
5
7
1