Search code examples
cvariableslocal

How to use values of local variable in another function in C


Suppose there are two function i.e., function1() and function2()

int function1()
{
  int F;
  printf("enter any value");
  scanf("%d",&F); 
  printf("Value is %d",F);
}

In function1() variable F is used which is local variable. How can I use the value of this local variable F in another function function2()?


Solution

  • Actually, you already did.

    passing the reference of F to scanf is how to use it. Writing your own function, you only need to decide if you are passing the reference or the value. If passing the value

    int function(int value) {
     ...
    }
    

    which would be called like

    function(F);
    

    if passing the reference

    int function(int* value) {
      ...
    }
    

    which would be called like

    function(&F);