Changing the value from "deep" function

Hello,
I got a program and there is a function which has a function inside and that inside function has a function etc etc. and in one of really deep functions I realize I really need to change one of the values which "lives" in the first function. How can I do this? With reference I would have to change input for all functions. By value it would be even worst hell. By pointer? I feel it could be that, but I got no real experience with pointers. I would be thankful for help.
Changing value with a pointer isn't really any different than changing with a reference. If you could show code with the exact problem we may be of more help.
Pseudocode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void FunctionA()
{
   int x = 2;
   FunctionB( x );
   void FunctionB( int x )
   {
      void FunctionC()
      {
         void FunctionD()
         {
            //Here I want to change int x;
            //Actually the x in FunctionB;
         }
      }
   }
}

More convencional way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void FunctionA();
void FunctionB( int x );
void FunctionC();
void FunctionD();
main()
{
   FunctionA();
}
void FunctionA()
{
   int x = 2;
   FunctionB( x );
}
void FunctionB( int x)
{
   FunctionC();
}
void FunctionC()
{
   FunctionD();
}
void FunctionD()
{
   //Change the 'x' in FunctionB
}
Last edited on
You can't create a function inside of a function. You may call a function inside another though. http://www.cplusplus.com/doc/tutorial/functions/

*edit you just edited your post and the only way to modify variable x from function B would be to pass it as reference to c then reference again to d.

Something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void FunctionA();
void FunctionB( int x );
void FunctionC();
void FunctionD();
int main()
{
   FunctionA();
}
void FunctionA()
{
   int x = 2;
   FunctionB( x );
}
void FunctionB( int x)
{
   FunctionC(x);
}
void FunctionC(int &x)
{
   FunctionD(x);
}
void FunctionD(int &x)
{
   //Change the 'x' in FunctionB
   x = some new value
}
Last edited on
Seriously I cant skip functionC? In my real code it would be like 3-4 funcitions to pass throu >.< :(
Last edited on
You could call function d directly from function b if you wanted. So it would be like
1
2
3
4
5
void functionB(int x)
{
    functionC();
    functionD(x);
}


It's hard to say without seeing the actual code since I don't know what exactly you are doing.
Im making a console rpg game to practise my C++ skills. I got to call FunctionD in FunctionC :(
I'm no expert on design, and don't mean to offend, but it's probably a bad design if you need to go through a series of functions like that that keep going deeper and deeper while changing superficial variables in the "deep" function. What is it you're trying to do exactly that is making you want/need to structure the program like that?

Edit: I mean, it's not necessarily bad design, in a game or something you might want to pass a "deltaTime" variable into multiple layers of your update function. It might be preferable in your case to have the variable that's being changed be a property of a class so you don't have to explicitly pass it in to every related function.
Last edited on
Reasonable idea. But If I declared an object in FunctionB can I use its methods in FunctionD?
Ganado was talking about something like:

1
2
3
4
5
6
7
8
9
class someClass
{
    private:
        int x;
    public:
        someFunction();
        someOtherFunction();
        //they can both access x
};
Yeah, I think I get this. In my game battle is a function which is made from a really many other smaller functions and what you suggest is to make a class battle instead of fuction. Yeah propobly its the best way from design view. Thanks for your advice guys :)
Topic archived. No new replies allowed.