I have 30 functions like this

void func1(int *var1, int *var2)
{
    func2(var1, var2);
}

void func2(int *var1, int *var2)
{
    func3(var1, var2);
}

void func3(int *var1, int *var2)
{
    func4(var1, var2);
}

I do not need var3 until func15. Is there a way to fix this mistake without having to change all the function calls and headings? var3 needs to be remembered every time I get to func15. I only use var3 in func15 though so I really do not want to have to change all of my function calls and headings. It obviously can't be a local variable or it will be forgotten each time it is called.

void func15(int *var1, int *var2, int *var3)
{
    func16(var1, var2);
}

Recommended Answers

All 4 Replies

If all the functions are in the same scope, use a variable that is global to that scope, and only moduify the arguments for and the calls to func15. Most modern IDE's will make that relatively easy.

How do you make a variable global to a scope? I only know about global variables to an entire program where you put it at the top of the program then your entire program can see it.

If the functions are in a class you declare it as a member of that class. If it is meant only for internal use keep it private, if a user of the class will need it make it public.

It depends on when the correct value for var3 is known and by whom (i.e. which function). If it is known only by func14, there's no point in changing the code. If it is known before the cascade of functions is called you might make it global. Since you have not stated which language you're using let's assume its c. If you declare var3 outside all functions, it will be visible inside each one including func15.
See: https://www.codingunit.com/c-tutorial-functions-and-global-local-variables

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.