Back to slide -- Keyboard shortcut: 'u'        next -- Keyboard shortcut: 'n'          lambda-expressions/forms.cc - The examples from above in context.Lecture 3 - slide 2 : 27
Program 1

// Forms from slide - in context.

#include <iostream>     
#include <string>

int g{4};   // global variable

int main () {
  int m{5}, n{6};

  auto f1 = [](int x)->int{return x + 1;};  // a function that returns x + 1. 
  auto f2 = [m](int x){return x + m;};      // can read m from main.
  auto f3 = [=](int x){return x + m + n;};  // can read all local variables in main.
  auto f4 = [&m](){++m;};                   // access to m by reference - can write.
  auto f5 = [&](){++m; n--;};               // access to all local variables in main by reference.
  auto f6 = [&]{++m;};                      // empty parameter list implicit.

  auto g1 = [](int x){return x + g;};       // g1 can access a global variable!

  std::cout << f1(1) << std::endl;     // 2
  std::cout << f2(1) << std::endl;     // 6
  std::cout << f3(1) << std::endl;     // 12

  f4();                                // m becomes 6
  f5();                                // m becomes 7, n becomes 5
  f6();                                // m becomes 8

  std::cout << g1(1) << std::endl;     // 5
  
  std::cout << "m: " << m << " n: " << n << std::endl;    // m: 8 n: 5

}