educative.io

Educative

Lambda Functions

Hello,
Queries about this example:
int addFunc(int a, int b){ return a + b; }

int main(){

auto addObj = [](int a, int b){ return a + b; }; 

}
Queries:

  1. Specific reason to use data type as auto?
  2. What does “[]” signifies here?

Hi @Mak, Thanks for reaching out to us.
1. If the parameter type is generic, you can use the auto keyword as the type specifier. This keyword tells the compiler to create the function call operator as a template. Each instance of auto in a parameter list is equivalent to a distinct type parameter.

2. [] is called the lambda introducer which denotes the start of the lambda expression.

Hope this helps.
Happy Learning :slight_smile: