tags:

views:

138

answers:

2

I need to combine these two functions. and need help in so:

int Triangle(GzRender *render, int numParts, GzToken *nameList,GzPointer *valueList)
{    
    if (on_screen) 
        {      
          return Position(render, pos);
        }
}



int Position(GzRender *render, GzCoord vertexList[3])
   {
     GzCoord *pv[3];
     int i,j;
     pv[0] = &vertexList[0];
 pv[1] = &vertexList[1];
 pv[2] = &vertexList[2];          

    //more function code in here
   }

Can anyone help me with this.

Regards

+3  A: 

Normally, separating out functions is a better, more common practice (and one of the main tasks during refactoring). That being said, you can "combine" these simply by doing:

int Triangle(GzRender *render, int numParts, GzToken *nameList,GzPointer *valueList)
{    
    if (on_screen) 
    {      
      //function code in here, working on "pos" instead of vertexList

      // return value
    }
    // return some other value here?
}
Reed Copsey
I have made a few more changes in my second function position(). When i did what you told me to, i get an error " 'vertexList' : undeclared identifier "
Zeeshan Rang
That's why I mentioned that. You'll need to replace "vertexList" with "pos" everywhere in there, since you were passing pos before.
Reed Copsey
Thank you. it works.
Zeeshan Rang
+1  A: 

The first poster (Reed Copsey) is correct about the fact that it's generally better to keep the functions separate.

Have you considered using the inline directive?

http://www.codersource.net/cpp_tutorial_inline_functions.html

Technically it's only a 'compiler hint' but you could try it. What it does is tell the compiler that you would like to include the body of the method denoted as inline in any other methods that call it. It's better from a maintenance standpoint, and should achieve what you are aiming for without the headaches of 'cut-and-paste' coding.

int Triangle(GzRender *render, int numParts, GzToken *nameList,GzPointer *valueList)
{    
    if (on_screen) 
        {      
          return Position(render, pos);
        }
}



inline int Position(GzRender *render, GzCoord vertexList[3])
{
      //function code in here
}
Andrew Rollings
I have made a few more changes in my second function position(). When i did what you told me to, i get an error " 'vertexList' : undeclared identifier "
Zeeshan Rang