Pass value to other function

K

Kay

I have got a question. There are three different functions that contain
a variable. How can I carry these variable to other function ?
 
J

John Harrison

Kay said:
I have got a question. There are three different functions that contain
a variable. How can I carry these variable to other function ?

Sounds difficult. Variables don't exist when a function has exited.

So unless your first function calls your second function and then the second
function calls the third function what you are asking is impossible.

If you need variables to exist after a function has exited then probably
what you should be using is classes not functions.

As usual some code to illustrate the precise situation would help.

john
 
M

Mike Wahler

Kay said:
I have got a question. There are three different functions that contain
a variable. How can I carry these variable to other function ?

You can't. But you could pass their values as arguments
to another function (provided their types are compatible
with the declared types of its parameters).

int other(int i1, int i2, int i3)
{
return i1 + i2 + i3;
}

int f1(void)
{
int v1 = 42;
return v1;
}

int f2(void)
{
int v2 = 99;
return v2;
}

int f3(void)
{
int v3 = 69;
return v3;
}

int main()
{
return (f1() - f3() + f2() - f1()) / other(f2(), f3() ,f1());
}


If you tell us what you're really trying to do, perhaps
I could come up with a more useful example.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,947
Members
47,498
Latest member
yelene6679

Latest Threads

Top