Lambdas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main(){ | |
int multiplier = 25; | |
/*Takes an integer input by reference and multiplies it by 2.*/ | |
auto func1 = [](int& a){a *=2;}; | |
/*Takes an integer input by value, multiplies it by 2 and returns the result.*/ | |
auto func2 = [](int a)->int {a *=2; return a;}; | |
/*Takes an integer input by value, multiplies it by multiplier whose value is captured from the outer scope and returns the result.*/ | |
auto func3 = [multiplier](int a)->int{a *= multiplier; return a;}; | |
int temp = 5; | |
func1(temp); | |
cout << temp<<endl; | |
cout << func2(temp)<<endl; | |
cout << func3(temp)<<endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment