[SOLVED] Passing an entire group of variables as one argument to a function C++


I’m trying to simulate a system that requires multiple variables and parameters and requires multiple nested functions. I was wondering if there was a way to pass them as a group so I can just pass one or two arguments without having to itemize each parameter and variable within each function and trying to keep track of the location of them all in an array/vector. With R, one can use with() to evaluate a function using a list of named parameters (see here). Can you do this with a map in C++?


Write a struct with the parameters.

The multiple nested functions either need an override taking such a struct, or be rewritten to consume it as their argument.

Now you can pass parameters around in a bundle.

If the set of parameters is not uniform, you might be out of luck. You also might be able to solve it using inheritance and the like. It will depend a lot on details.

If there is a significant set of parameters that is uniform, making them a struct can reduce the number of parameters you pass around.

void do_math(int x, int y, int z, int w);


struct coords {
  int x,y,z,w;
void do_math(coords);

if we want to keep the original do_math around we can:

inline void do_math(coords c) { do_math(c.x, c.y, c.z, c.w); }

now we can have code that wants to call do_math a bunch of times with tweaks;

void do_math_over_and_over(coords c, int x_range) {
  for(int i = 0; i < x_range; ++i) {
    coords tmp = c;
    tmp.x += i;

Answered By – Yakk – Adam Nevraumont

Answer Checked By – Mildred Charles (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *