Here we’ll see how we can change a variable from inside a C function. Let’s consider the following 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 in main(), we have a variable x with value 5. We tried to change the value of x in function change_value() by adding 100 with it.
Here is the output of this program.
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 it is clear that even though the variable is changed to 105 inside the function, the value of x remains 5 after coming out of the function. When we printed x in main() after the function call, it prints 5.
Here are a few ways we can change a variable from inside a function.
Using Global Variable
We can make the variable x global. Here is the program.
#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); }
As variable x is now global, we don’t need to pass that as function parameter.
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.
From this output we can see the changed value even after the function call. If you change a global variable from any function, you’ll be able to see the changed value in any other functions.
Main problem with this approach is that we have to use global variable. In general global variable usage is not recommended unless absolutely required. Managing too many global variables is difficult.
Returning the Changed Value
Instead of using a global variable, we can return the changed value from the function. Here is the example code.
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); }
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.
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 using struct but things are complicated.
Using Call by Reference
This probably is the most elegant solution. Instead of passing the value of the 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.
- Read also: Call by Value vs Call by Reference in C.
The post Change a Variable from inside a C Function appeared first on QnA Plus.