So today i was learning about c++ lambda functions. and i have some doubts.↵
↵
Q.1) See this↵
↵
<spoiler summary="why the output is 2?">↵
↵
~~~~~↵
function<int(int,int)> sum = [&](int a, int b)↵
{↵
++a;↵
return a+b;↵
};↵
int a = 2, b = 3;↵
int s = sum(a, b);↵
cout << a << endl;// why it's 2 i have passed 'a' with refrence. how to pass it with refrence.↵
~~~~~↵
↵
↵
</spoiler>↵
↵
Q.2) another question↵
↵
<spoiler summary="SpoilerThis is giving me runtime error why?">↵
↵
~~~~~↵
function<int(int)>sum = [&](int a)get2;↵
{↵
// some code↵
cout << get(2); // how can i use the get function?↵
return something;↵
}cout << get2(3);↵
↵
function<int(int)> get2 = [&](int va)↵
{↵
int b = sum(3);↵
return va;↵
};↵
↵
↵
~~~~~↵
↵
↵
</spoiler>↵
↵
tell me
↵
Q.1) See this↵
↵
<spoiler summary="why the output is 2?">↵
↵
~~~~~↵
function<int(int,int)> sum = [&](int a, int b)↵
{↵
++a;↵
return a+b;↵
};↵
int a = 2, b = 3;↵
int s = sum(a, b);↵
cout << a << endl;// why it's 2 i have passed 'a' with refrence. how to pass it with refrence.↵
~~~~~↵
↵
↵
</spoiler>↵
↵
Q.2) another question↵
↵
<spoiler summary="
↵
~~~~~↵
function<int(int)>
cout << get(2); // how can i use the get function?↵
return something;↵
}
↵
{↵
};↵
↵
↵
~~~~~↵
↵
↵
</spoiler>↵
↵
tell me