193030

Lambda expressions

Jul 23rd, 2021 (edited)
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     // [ captures ] ( params ) { body }
  7.     //
  8.     [](){cout << "Shortest decleration " << endl;}();
  9.     // [](int a, int b){ cout << "Parameters sum: " << a+b << endl;} (10,20);
  10.     int sum =[](int a, int b){return a+b;} (10,20);
  11.     cout << "Parameters sum: " << sum << endl;
  12.    
  13.     int a = 5;
  14.     auto function = [a]() {cout << a << endl; }; // Passing A by value, a value won't change the expression
  15.     function();
  16.     a = 10;
  17.     function();
  18.    
  19.     auto function2 = [&a]() {cout << a << endl;}; // Passing A by reference, change will occur
  20.     function2();
  21.     a = 20;
  22.     function2();
  23.  
  24. }
Add Comment
Please, Sign In to add comment