wtfunctional/examples/00_dafunc.cpp

19 lines
382 B
C++
Raw Normal View History

#include <iostream>
#include <cassert>
using namespace std;
int f(int x) { return ++x;}
int g(int& x) { return ++x;}
int main() {
int x = 2;
/* f is "functional", as it has no side-effects */
f(x);
assert(x==2);
/* g changes a variable of an outer scope,
* i.e. it has a side effect
* Thus, g is not functional!
*/
g(x);
assert(x!=2);
}