0

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?

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?

4
  • 3
    This depends on how you're going to pass these variables. If you pass pointers to them, then the function will be able to change the variables' values, otherwise, it won't. Commented Jan 3, 2017 at 16:17
  • 4
    How a function acts on a variable is independent if it is originally global or not. Posting examples of usage would help clarity your question. Commented Jan 3, 2017 at 16:18
  • @SouravGhosh *ptr = bar, trivially, unless the pointer is const Commented Jan 3, 2017 at 16:18
  • You could easily write a little test program to find out. Commented Jan 3, 2017 at 16:43

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

Comments

5

If you pass the address of variable as a parameter of function and if you do any change on it yes it will be changed. Also for global variables you don't have to pass them into function you can use them in any function and change because they are global.

Comments

2

If you name the arguments the same as the global variables, then the local variables created by the formal arguments will shadow the global variables, and inside the function they wll be closest in scope and used and changing the will not change the global variables.

For example, consider the following program:

int a = 1;
int b = 2;

void f(int a, int c);

int main(void)
{
    f(a, b);  // Pass the global variables
    return 0;
}

void f(int a, int c)
{
    a = 5;  // Changes the local variable a, not the global variable
    c = 6;  // Changes the local variable c
    b = 7;  // Changes the global variable
}

After the function f has been called, the value of the global variable a will still be 1 but the value of b has been changed to 7.

1 Comment

it's unclear to me whether the OP was talking about shadowing, or simply about passing the global variables as parameters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.