Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have two functions that call each other C++

I have 2 functions like this that does obfuscation on if loop:

void funcA(string str)
{
    size_t f = str.find("if");
    if(f!=string::npos)
    {
        funcB(str);        //obfuscate if-loop
    }
}

void funcB(string str)
{
     //obfuscate if loop
     funcA(body_of_if_loop);     //to check if there is a nested if-loop
}

The problem with this would be that funcA would not be able to see funcB and vice versa if I put funcB before funcA.

Would appreciate any help or advice here.

like image 771
consprice Avatar asked Jan 30 '13 07:01

consprice


People also ask

Can two functions call each other?

The functions that call itself are direct recursive and when two functions call each other mutually, then those functions are called indirect recursive functions.

How do two functions communicate with each other?

Functions communicate among themselves with the help of arguments and return value.

Can we call main function from another function in C?

In 'C' you can even call the main() function, which is also known as the "called function" of one program in another program, which is called "calling function"; by including the header file into the calling function.

Can a function call itself C?

The C programming language supports recursion, i.e., a function to call itself.


2 Answers

What you want is forward declaration. In your case:

void funcB(string str);

void funcA(string str)
{
    size_t f = str.find("if");
    if(f!=string::npos)
    {
        funcB(str);        //obfuscate if-loop
    }
}

void funcB(string str)
{
     //obfuscate if loop
     funcA(body_of_if_loop);     //to check if there is a nested if-loop
}
like image 155
sheu Avatar answered Sep 25 '22 18:09

sheu


A forward declaration would work:

void funcB(string str); 

void funcA(string str)
{
    size_t f = str.find("if");
    if(f!=string::npos)
    {
        funcB(str);        //obfuscate if-loop
    }
}

void funcB(string str)
{
     //obfuscate if loop
     funcA(body_of_if_loop);     //to check if there is a nested if-loop
}
like image 30
billz Avatar answered Sep 22 '22 18:09

billz