Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C - C++ Learning > Re: Access vari...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 9 Topic 4196 of 4306
Post > Topic >>

Re: Access variables define in a function in another function?

by "Jim Langston" <tazmaster@[EMAIL PROTECTED] > Jul 8, 2008 at 09:04 PM

"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.
 




 9 Posts in Topic:
Access variables define in a function in another function?
Pat <pkelecy@[EMAIL PR  2008-07-08 20:04:32 
Re: Access variables define in a function in another function?
"Alf P. Steinbach&qu  2008-07-09 02:12:05 
Re: Access variables define in a function in another function?
Barry Schwarz <schwarz  2008-07-08 18:33:12 
Re: Access variables define in a function in another function?
"Jim Langston"   2008-07-08 21:04:52 
Re: Access variables define in a function in another function?
Pat <pkelecy@[EMAIL PR  2008-07-09 09:52:04 
Re: Access variables define in a function in another function?
"Jim Langston"   2008-07-09 11:42:36 
Re: Access variables define in a function in another function?
Bart van Ingen Schenau &l  2008-07-10 17:42:23 
Re: Access variables define in a function in another function?
"osmium" <r1  2008-07-09 06:07:16 
Re: Access variables define in a function in another function?
Francis Glassborow <fr  2008-07-10 13:07:24 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Oct 15 12:56:08 CDT 2008.