So If I use global variables as the arguments of a function, would the
function be able to change the value of the global variable?
No, you will be unable to change the global variable used as an argument of a function parameter. The function parameter gets a copy of the global variable. It itself (the parameter) is a local variable of the function. Any changes of the local variable do not influence on the original argument because the function deals with a copy of the value of the global variable.
And If i defined a global variable, and I used a function to alter the
value of that variable, would the new value of that variable stay
after the function returns?
If the function deals with the global variable directly or indirectly through a pointer to the global variable used as a function parameter then the value of the global variable can be changed by the function.
Consider the following demonstrative program
#include <stdio.h>
int x = 10;
void f(int x)
{
x = 20;
}
void g(int *px)
{
*px = 30;
}
void h()
{
x = 40;
}
int main(void)
{
printf("Before calling f( x ) - x = %d\n", x);
f(x);
printf("After calling f( x ) - x = %d\n\n", x);
printf("Before calling g( &x ) - x = %d\n", x);
g(&x);
printf("After calling g( &x ) - x = %d\n\n", x);
printf("Before calling h() - x = %d\n", x);
h();
printf("After calling h() - x = %d\n\n", x);
return 0;
}
Its output is
Before calling f( x ) - x = 10
After calling f( x ) - x = 10
Before calling g( &x ) - x = 10
After calling g( &x ) - x = 30
Before calling h() - x = 30
After calling h() - x = 40
One more interesting case
Consider the following code snippet
int x = 10;
void h( int value )
{
x += value;
}
//...
h( x++ );
Here there is a sequence point after evaluation of the function arguments. Thus inside the function the global variable will have the value 11 and as result you will get that after this statement
x += value;
x will be equal to 21.
*ptr = bar, trivially, unless the pointer isconst