0

Firstly, I want to create a function inside main. Secondly, I want to pass this function as an argument to another function. Is it possible please?

For the first point, I know that it is possible using lambda functions or structures, as explained in the following link: Can we have functions inside functions in C++?

However, I did not manage to pass this function as an argument to another function.

More precisely, I want something like:

int main()
{
    void add(int x, int y){cout<<x+y<<endl;}
    int a = 1;
    int b = 2;
    apply_operator(add,a,b);
}

where apply_operator is defined in another file by:

void apply_operator(void (*operator)(int,int),int x,int y)
{
    operator(x,y);
}
ZZZ
  • 21
  • 4

5 Answers5

1

Using lambdas and refraining from using the operator keyword we get:

#include <iostream>

void apply_operator(void (*op)(int, int), int x, int y)
{
    op(x, y);
}

int main()
{
    auto add = [](int x, int y) { std::cout << (x + y) << std::endl; };
    int a = 1;
    int b = 2;
    apply_operator(add, a, b);
}

Which prints 3 as expected.

Nathan Pierson
  • 5,461
  • 1
  • 12
  • 30
  • Thank you very much, it worked very well. I tried the same thing with "apply_operator(&add, a, b);" instead and it did not work. – ZZZ Jun 16 '21 at 17:01
  • That's because a lambda is already convertible to a function pointer. Taking its address gives you something different. – Nathan Pierson Jun 16 '21 at 17:03
1

You can certainly use a lambda.

int main()
{
    auto add = [](int x, int y){cout<<x+y<<endl;};
    int a = 1;
    int b = 2;
    apply_operator(add,a,b);
}

See it work!

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • Thank you very much, it worked very well. I tried the same thing with "apply_operator(&add, a, b);" instead and it did not work. – ZZZ Jun 16 '21 at 17:02
  • @ZZZ that is because a captureless lambda will implicitly convert to a function pointer. Your address-of-a-lambda will not. – Drew Dormann Jun 16 '21 at 17:07
0

You may be interested in using a lambda in this case

int main()
{
    auto add = [](int x, int y){ cout<<x+y<<endl; };
    int a = 1;
    int b = 2;
    apply_operator(add,a,b);
}

As long as the lambda does not capture it can decay into a function pointer.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

You could use function pointers. I have pasted the code below. I have declared a the pointer "f" to a fucntion which takes in two integers and returs nothing in return and assigned the address of the fucntion add to it.

#include <iostream>

using namespace std;

void add(int x, int y)
{
    cout<<x+y<<endl;
}

void apply_operator(void (*f)(int, int),int a, int b)
{
    f(a, b);
}
int main()
{
    void (*f)(int, int);
    f = &add;
    int a = 1;
    int b = 2;
    apply_operator(f,a,b);
}
Salvankar
  • 91
  • 6
-1
# include<iostream>

# include<conio.h>
using namespace std;
int fun(int a, int b)
{
    if(a>b)
    {
    cout<<"1st number is larger";
    }
    else
    {   
    cout<<"2nd number is larger";
    }
}       

int main()
{
    int a,b;
    cout<<"enter the two numbers";
    cin>>a>>b;
    fun(a,b);
    getch();

   return 0;

    getch();

}
pushplata
  • 1
  • 1