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()
?
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);
Function local variable lifetimes are local to that function. If you have to use a value held by a local variable inside a function to another function, you can
return
the value from function1()
.function2()
.NOTE: above answer assumes that both function1()
and function2()
are called from a parent function, maybe func()
.
Edit:
if you need to use multiple values of local variables in other functions, (without using global variables), most common practice is to
function1()
function1()
from the local variables.function2()
as argument.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With