//---------------------------------------------------------------------- // // functions.cpp // // This program gives some example functions. // // Written by: Simon Parsons // // Last modified: 4th October #include using namespace std; // sumUp() // // A simple function that takes two integers as its arguments and // returns an integer. int sumUp (int a, int b){ int c; c = a + b; return c; } // divvyUp() // // This function takes two integers as its arguments and returns a // double double divvyUp (int a, int b){ return (double) a / b; } // isEven() // // This function has a bit more to it. Oh, and it returns a bool. bool isEven (int a){ if ((a % 2) == 0){ return true; } else { return false; } } // printValues() // // And now we have a function that doesn't return a value void printValues(int a, double b){ cout << "We have an integer " << a << " and a double " << b << endl; } // Now we start main() from which we call the functions int main() { // Declare variables int x; int y; int z; // Initialize variables x = 0; y = 0; // Now enter values cout << "Please enter a value for x: "; cin >> x; cout << "and now a value for y: "; cin >> y; cout << "Thanks!" << endl << endl; // Functions return values, so we can use them like this: z = sumUp(x, y); cout << z << endl; // and like this: cout << divvyUp(x, y) << endl << endl; // We can also use them in more complex constructions: if(isEven(x)){ printValues(z, divvyUp(x, y)); } else { cout << divvyUp(y, x) << endl; } return 0; } // end of main()