fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int pureFunction(int number){
  5. return number + 1;
  6. }
  7.  
  8. int& notPureFunction(int& number){
  9. number = number + 1;
  10. return number;
  11. }
  12.  
  13. int main() {
  14. int number = 1;
  15. cout << "number: " <<number << endl;
  16. cout << "---------------------------" << endl;
  17.  
  18. cout << "pureFunction gives back: "<< pureFunction(number) << endl;
  19. cout << "number after pureFunction: " << number << endl;
  20. if( pureFunction(number) == pureFunction(number) ) {
  21. cout << "Two identical function call gives the same result. f(x) == f(x)" << endl;
  22. cout << "number after function equality test: " << number << endl;
  23. }
  24. cout << "---------------------------" << endl;
  25.  
  26.  
  27. cout << "notPureFunction gives back: "<< notPureFunction(number) << endl;
  28. cout << "number after notPureFunction: " << number << endl;
  29. if( notPureFunction(number) != notPureFunction(number) ) {
  30. cout << "Two identical function call does not give back the same result. f(x) != f(x)" << endl;
  31. cout << "number after function equality test: " << number << endl;
  32. }
  33. cout << "---------------------------" << endl;
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
number: 1
---------------------------
pureFunction gives back: 2
number after pureFunction: 1
Two identical function call gives the same result. f(x) == f(x)
number after function equality test: 1
---------------------------
notPureFunction gives back: 2
number after notPureFunction: 2
Two identical function call does not give back the same result. f(x) != f(x)
number after function equality test: 4
---------------------------