|
From: Pat on 8 Jul 2008 20:04 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. Thanks. -Pat
From: Alf P. Steinbach on 8 Jul 2008 20:12 * Pat: > 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. It seems that what you want to pass around is information, not variables. Please post a minimal, compilable example that illustrates the problem. Please also do tell which language you're using: C, or C++. Cheers, & hth., - Alf -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail?
From: Barry Schwarz on 8 Jul 2008 21:33 On Tue, 08 Jul 2008 20:04:32 -0400, Pat <pkelecy@_REMOVETHIS_gmail.com> wrote: >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. Does the called function need to update the variables or only extract their value. If the latter, then simply passing the variable (actually its value) as an argument in the calling statement seems sufficient. If the former, then pass the address of the variable to the function and dereference the address as needed. Are you trying to do something that makes these techniques unworkable? Remove del for email
From: Jim Langston on 9 Jul 2008 00:04 "Pat" <pkelecy@_REMOVETHIS_gmail.com> wrote in message news:eZSdnW3iyMwcnenVnZ2dnUVZ_uCdnZ2d(a)insightbb.com... > 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.
From: osmium on 9 Jul 2008 09:07 "Pat" writes: > 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. You might be thinking of nested functions which some languages, such as Pascal, have. Alas, C and C++ do not have that capability and I miss it. This certainly increases the typing load and visual clutter in some problems. One idea that might possible help in some situations is to contrive a structure or array and pass that amongst the functions. *You* must expend extra time to do the contriving, so it is not free, of course I think of the address space of C as "flat".
|
Next
|
Last
Pages: 1 2 Prev: Include decimal point in floating point values? Next: langurage is difficult |