Home > Net >  Functions with similar parameter but have different outputs
Functions with similar parameter but have different outputs

Time:11-11

I have to write multiple functions that have similar parameters. Is there a way to make it shorter and readable? Here is an example code

#include <iostream>
#include <vector>

void test1(int index){
    std::cout << "Chosen Answer : " << index << std::endl;
    std::cout << "ABCD ";
}

void test2(int index){
    std::cout << "Chosen Answer : " << index << std::endl;
    std::cout << "JKLM ";
}

void test3(int index){
    std::cout << "Chosen Answer : " << index << std::endl;
    std::cout << "PQRS ";
}

int main() {
    std::vector<int> input;
    for(int i=0; i<5; i  ){
        input.push_back(i);
    }
    
    test1(input[1]);
    test2(input[2]);
    test3(input[3]);
    
    return 0;
    
}

CodePudding user response:

In your example, you can create a gểnal function that has another parameter that specifies what function do you want to use:

void test(int index, int mode)
{
    switch(mode)
    {
        case 1:
            test1();
            break;
        case 2:
            test2();
            break;
        case 3:
            test3();
            break;
        default:
            std::cout << "Invalid mode ";
            break;
    }
}
// code ...

int main()
{
    test(input, 1) // call test1()
    test(input, 2) // call test2()
    test(input, 3) // call test3()
}

CodePudding user response:

Expanding on a comment, basically it is always the same procedure: find out what is same and what is different between them. What is same you put into another function, what is different is parameters to that function.

What is same:

std::cout << "Chosen Answer : " << index << std::endl;
std::cout <<

What is different

              "some string";

Put what is same in a function and what is different is parameters to that function:

 void test(int index, const std::string& text) {         
     std::cout << "Chosen Answer : " << index << std::endl;
     std::cout << text;
 }
 void test1(int index) { test(index,"ABCD"); }
 void test2(int index) { test(index,"JKLM"); }
 void test3(int index) { test(index,"PQRS"); }

This was refactoring from the functions point of view. If we considered main, then perhaps we wouldn't need test1,test2 and test3 in the first place:

int main() {
    std::vector<int> input;
    for(int i=0; i<5; i  ){
        input.push_back(i);
    }
    
    std::vector<std::string> texts{"ABCD","JKLM","PQRS"};
    for (int i=0; i<3;   i) {
        test(input[i 1],texts[i]);
    }
}

yes because inside each of the functions there are different math logic that has to be done depends on the given index and each functions is printing the result of math logic with different paragraph.

Thats not what your code example illustrates, though the procedure is the same: Find the common part, find the parts that are different. You can pass callables to functions to factor out some maths. A simple way to do that is via lambdas, for example:

template <typename F>
void calc(F f,int a, int b,const std::string& op) {
    std::cout << a << op << b << "=" << f(a,b) << "\n";
}

int main() {
    calc( [](int a,int b) { return a b; },1,2," ");
    calc( [](int a,int b) { return a-b; },1,2,"-");
}
  

CodePudding user response:

You can declare methods even more shorter.

#define DECLARE_TEST_METHOD(name, output_value) void test##name(int index) { \
                                                std::cout << "Chosen Answer : " << index << std::endl; \
                                                std::cout << output_value; \
                                                }
DECLARE_TEST_METHOD(1, "ABCD ");
DECLARE_TEST_METHOD(2, "JKLM ");
DECLARE_TEST_METHOD(3, "PQRS ");
  •  Tags:  
  • c
  • Related