Functions Jordi Cortadella Department of Computer Science
Maximum of two numbers #include using namespace std; // This program reads two numbers and // prints the maximum value of both int main() { int x, y; cin >> x >> y; int m; if (x > y) m = x; else m = y; cout << "The maximum value is " << m << endl; } Introduction to Programming© Dept. CS, UPC2
Defining a function // Returns the max value of a and b int max(int a, int b) { int m; if (a > b) m = a; else m = b; return m; } maxmax int a int b int Introduction to Programming© Dept. CS, UPC3
Defining a function // Returns the max value of a and b int max(int a, int b) { int m; if (a > b) m = a; else m = b; return m; } We can call the function in many ways: s = max(x, y); z = max(n – 1, 3x); Introduction to Programming© Dept. CS, UPC4
Defining a function // Returns the max value of a and b int max(int a, int b) { if (a > b) return a; else return b; } int max(int a, int b) { if (a > b) return a; return b; } Introduction to Programming© Dept. CS, UPC5
Maximum of two numbers (III) // Returns the max value of a and b int max(int a, int b) { if (a > b) return a; else return b; } // This program reads two numbers and // prints the maximum value of both int main() { int x, y; cin >> x >> y; cout << "The maximum value is " << max(x, y) << endl; } Introduction to Programming© Dept. CS, UPC6
Maximum of three numbers // Returns the max value of a and b int max(int a, int b) { if (a > b) return a; else return b; } // This program reads three numbers // and prints their maximum value int main() { int x, y, z; cin >> x >> y >> z; cout << "The maximum value is " << max(x, max(y, z)) << endl; } xmaxmax maxmax x y z Introduction to Programming© Dept. CS, UPC7
Maximum of three numbers // Returns the max value of a and b int max(int a, int b) { if (a > b) return a; else return b; } // This program reads three numbers // and prints their maximum value int main() { int x, y, z; cin >> x >> y >> z; cout << "The maximum value is " << max(x, max(y, z)) << endl; } max(max(x, y), z) xmaxmax maxmax x y z Introduction to Programming© Dept. CS, UPC8
// Returns |x| int absolute(int x) { if (x >= 0) return x; return -x; } Absolute valueMathC++ == != > < >= <= a = b; Assignment if (a == b) … Condition Introduction to Programming© Dept. CS, UPC9
Calculate x y Algorithm: repeated multiplication x x x x y times // Pre: y 0 // Returns x y int power(int x, int y); Introduction to Programming© Dept. CS, UPC10
Calculate x y Algorithm: repeated multiplication x x x x y times yxi p=x i Introduction to Programming© Dept. CS, UPC11
// Pre: y 0 // Returns x y int power(int x, int y) { int p = 1; int i = 0; // Repeat y times while (i < y) { p = px; i = i + 1; // p = x i } return p; } Calculate x yyxi p=x i Introduction to Programming© Dept. CS, UPC12
Factorial Introduction to Programming© Dept. CS, UPC13
Calculate n!i f = i! // Pre: n 0 // Returns n! int factorial(int n); Introduction to Programming© Dept. CS, UPC14
Factorial // Pre: n 0 // Returns n! int factorial(int n) { int f = 1; int i = 0; while (i < n) { i = i + 1; f = fi; // f = i! } return f; } i f = i! Introduction to Programming© Dept. CS, UPC15
Summary Functions are used to group sequences of statements. Why should we use functions? – Re-use of code, modularity and readability When designing a loop, try to find a property that characterizes all iterations, e.g., p=x i (power) f=i! (factorial) Use the property to define the loop components (condition, initialization, body of the loop). Introduction to Programming© Dept. CS, UPC16