So I havent really been working on anything it's just a question I had. I was thinking that when you write functions, if you have a few arguments in the argument list, then use that function a ton, then find out later you need to add another argument to the functions argument list, you have to go through and add it to each invokation of that function, which is a pain. Now yes, a properly designed program may mitigate or even eliminate this issue, but lets face it, shit happens.
So I was wondering, is there a way to have one spot for a declaration of an argument list, that a function can use in any instance without it having to update each one?
As an example, lets say i have my function definition stored somewhere:
(Not sure if this would work its just an example to illustrate what im talking about)
ArgumentList = (int x, int y, int z);
void Function(ArgumentList)
This way, if i want to add an argument to the functions argument list, all I have to do is add it in one spot and any instance of that function will have it.
Two straightforward ways:
(1) Pass a struct (or class): then you pass a single, compound object with whatever you want as data members.
(2) Or make your function a function member of a struct or class containing your parameters.
If you add a new argument to a function, then presumably you need to pass different values in different calls to the function. I struggle to understand how you could automatically determine which calls should pass which values.
If the parameter is always the same then it shouldn't be a parameter. If you're initializing values then put them in a class and use a constructor.
But if you really need to add a parameter then pull on your big boy/girl undies and search the code for every call. Make the appropriate changes to each call.
Sure this is grunt work, but it doesn't really take that long. Also, you'll quickly get a reputation as a hero because you can do stuff that others "can't." Sometimes developers say that something is too hard to do when it's really just grunt work that they aren't willing to do. They'll make every excuse in the book for why it's too hard, too error prone, can't be done, will mess up the rest of the system etc. etc. etc. That's when you, Captain Coder, should step in, knuckle down and just do the work. Your boss will think you're amazing.
its not the cleanest, but if you had a very large program you can just use overloading. This works where you need a new parameter for new code, but the old stuff was fine without it, which happens sometimes.
1 2 3 4 5 6 7 8 9 10
void foo (thing a, int b)
{
stuff;
}
void foo (thing a, int b, oops iforgot)
{
foo(a, b);
use(iforgot);
}
you can also use a default value for new parameters to an extent to solve this:
1 2 3 4 5
void foo (thing a, int b, oops iforgot = derp)
{
stuff;
}
foo(x,y); //ok
foo(x,y,z); //also ok
if the old code NEEDS to send the new parameter, you have to use the other ideas.
if you have time, do it right though. unnecessary wrappers to patch stuff in that you missed is going to make a mess to read. Its not impossible to follow, but it will leave the reader doing a head-scratch as to WHY.
Whoa lots of responses. I was following the documentation on here for the struct and thats why i had the objects declared before the semicolon in the struct.
I was thinking since the objects were declared in the struct I could just pass the name of the struct in the function argument list and just use it that way, that way I could just use any declared object without having to write out all the objects in the function argument list. I see the problem with that now so I reverted it back to this:
@jonnin thats a good solution, i'll keep that one in mind as well. I know that doing the grunt work is pretty much unavoidable but for me the less i have to keep track of in my head the easier it is to just focus on creating.