"Pat" <pkelecy@[EMAIL PROTECTED]
> wrote in message
news:eZSdnW3iyMwcnenVnZ2dnUVZ_uCdnZ2d@[EMAIL PROTECTED]
> Is is possible to have variables that are defined in a function
accessible
> in another function?
>
> I'm working on a program that makes calls to several (user defined)
> functions. I would like to access some of the variables defined in the
> first function for use in the second one, but I'm not sure how to do
that.
> Everything I've read indicates the variables must be defined outside the
> function to be more globally accessible. But that would not be very
> convenient. So I hope there's a work around.
void Foo( int Val )
{
std::cout << Val << "\n";
}
void Bar()
{
int X = 20;
Foo( X );
}
Foo is using the variable X from Bar which is being passed by value (being
copied). This is the normal way to pass information between functions, as
parameters. This will work in C or C++ (even though my use of std::cout
will only work in C++).
Now, there are a lot of other ways to do it also, but it depends on what,
exactly, you are trying to do.


|