Created
November 27, 2016 10:44
-
-
Save mazhar-ansari-ardeh/d10e50a850bde3262fae23c8e5d8c242 to your computer and use it in GitHub Desktop.
A small program that demonstrates the definition and usages of lambda expressions in C++.
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 <functional> | |
#include <iostream> | |
#include <stdio.h> | |
using namespace std; | |
extern function<int(int, function<int(int)>)> g; | |
auto f = [](int n, function<int(int)> h) | |
{ | |
if (n >= 5) return n; | |
cout << "h is some function " << h(4.0) << endl; | |
auto func = [h](int m) { return m*h(m); }; | |
return g(n + 1, func); | |
}; | |
function<int(int, function<int(int)>)> g = [](int n, function<int(int)> h) | |
{ | |
if (n >= 5) return n; | |
cout << "h is some function " << h(5.0) << endl; | |
auto func = [h](int m) { return m*h(m); }; | |
return f(n + 1, func); | |
}; | |
int main() | |
{ | |
auto initial_function = [](int m) { return m*m; }; | |
f(1, initial_function); | |
getchar(); | |
return 0; // Return value of the function is int so its better to use 0 rather than 0.0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment