Talk About Network

Google





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

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

by Pat <pkelecy@[EMAIL PROTECTED] > Jul 9, 2008 at 09:52 AM

Thanks for all the replies.  I don't think I was very clear in my post 
so let me illustrate with an example.  The code I'm developing, written 
in C++, is a dll that is used by another application to create geometry 
objects (solid models).  As such, there are many operations involved, 
and to keep it organized I would like to put each operation in its own 
function.  So the general structure looks something like this:

******************************************
{headers}

{function prototypes}

{structure definitions}

// MAIN FUNCTION FOR CREATING OBJECT
long CreatePrimative(struct FunctionLib* functionLib, void* 
callbackData, double* paramValues)
{
   // CREATE 2D OUTLINE OF OBJECT
   long profile = CreatProfile(functionLib, callbackData, paramValues);

   // EXTRUDE PROFILE INTO 3D
   long object = CreateObject(functionLib, callbackData, paramValues, 
profile);

   return object;
}

// USER FUNCTIONS
long CreateProfile(struct FunctionLib* functionLib, void* callbackData, 
double* paramValues)
{
    double r=paramValue[0];
    double theta=paramValue[1];
    double pi = 3.1415926;

    double x1 = r*cos(theta + pi/2);
    double y1 = r*sin(theta + pi/2);
    .
    {compute "profile", return as a pointer}
    .
    return profile;
}


long CreateObject(struct FunctionLib* functionLib, void* callbackData, 
double* paramValues, profile)
{
    double r=paramValue[0];
    double theta=paramValue[1];
    double pi = 3.1415926;

    double x1 = r*cos(theta + pi/2);
    double y1 = r*sin(theta + pi/2);

    double z1 = sqrt(x1*x1 + y1*y1);
    .
    {compute "object", return as a pointer}
    .
    return object;
}

*************************************************************************

Now in this example, the variable "z1" is only used in the second 
function (CreateObject).  But to calculate it, I need to compute x1 and 
y1, quantities that were also calculated in the first function 
(CreateProfile).  This involves repeating some variable assignments as 
well (r, theta, pi).  So to avoid all this repetition, I would like to 
somehow  make x1 and y1, when computed in the first function, available 
for use in second.  My first thought was to make x1 and y1 global, but 
I'm not sure how to do that in C++.  Can it be done in this context? 
Perhaps a better way is to put everything that needs to pass out into a 
structure.  Any opinions?

My actual code is much more complicated, with more functions and many 
more variable definitions and calculations.  So being able to share 
information between these is pretty im****tant.

Thanks for any advice with this.  -Pat
 




 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
localhost-V2008-12-19 Fri Jan 9 5:50:53 PST 2009.