When pass a variable to a function, why the function only gets a copy/duplicate of the variable?
int n=1;
void foo(int i)
{
i++;
}
As
It is to make sure that the function doesn't change the original value.
Because C pass arguments by value.
From Kernighan & Richtie 2nd edition : (1.8 Call by value) "In C all function arguments are passed by "value""
In order to change the value of a parameter you need to pass by reference using the '&' symbol.
The reason this is done is so that you can choose whether or not you want changes to stick with your variable. If you pass by value you can be sure that it will not change and cause an error in your program.