Hello all,
So here is my problem:

I have 2 functions that one uses the other's calculations and result:

int functionA(int a, int b) {
int x = a+b;
return x;
}
int functionB(int a, int b, int c) {
int xx = x(a,b);
int f = xx * c;
return f;
}

I have reached a point in a program I am trying to write, where I need functions that take too many parameters and I was thinking whether or not there is a better way to do it, like calling the function A or its result directly without having to insert it parameters as parameters to funciton B and so on...
Thank you all.

Recommended Answers

All 4 Replies

You could use global variables, that that is normally fround upon because it can lead to bugs and errors.

you sould nest the functions like this: functionB(a,b,FunctionA(a,b));

In addition to what Ancient Dragon points out, if you are to the point where the number of arguments is cumbersome you may want to look into a refactor - possibly using structs to pass around common data.

lambda is also another way out. consider looking into that.
I personally like lambda when it comes to what you are requesting. Clean and mmm simple. That is depends on your knowledge with cpp11.

Taking the code that you have and changing it a little bit you can make it look like this:

int functionA(int a, int b) 
{
    return a + b;
}

int functionB(int a, int b, int c) 
{
    return c * x(a, b);
}
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.