Your version can work well in some cases, but look at the downsides:
- You need to pass a large number of parameters to your
CG_step. This could get unwieldy in other cases. - How can the function store the changes it makes to the parameters? In-place assignments of arrays will work, but updating scalars will not work. You can play with multiple return values as you do with
residual(but you don’t use it actually?). But this makes things more brittle and complicated.
What’s best depends on the particular problem, but in general I think wrapping many related values in a struct is good software engineering practice.
And tangentially related is this quote from Linus Torvalds (see discussion here):
Bad programmers worry about the code. Good programmers worry about data structures and their relationships.
It’s often a good idea to prioritize the choice of data structure even if doing so you lose some mathematical purity.