Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions Jordi Cortadella Department of Computer Science.

Similar presentations


Presentation on theme: "Functions Jordi Cortadella Department of Computer Science."— Presentation transcript:

1 Functions Jordi Cortadella Department of Computer Science

2 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

3 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

4 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, 3x); Introduction to Programming© Dept. CS, UPC4

5 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

6 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

7 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

8 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

9 // 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

10 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

11 Calculate x y Algorithm: repeated multiplication x  x  x    x y times yxi p=x i 4301 4313 4329 43327 43481 Introduction to Programming© Dept. CS, UPC11

12 // 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 = px; i = i + 1; // p = x i } return p; } Calculate x yyxi p=x i 4301 4313 4329 43327 43481 Introduction to Programming© Dept. CS, UPC12

13 Factorial Introduction to Programming© Dept. CS, UPC13

14 Calculate n!i f = i! 01 11 22 36 424 5120 // Pre: n  0 // Returns n! int factorial(int n); Introduction to Programming© Dept. CS, UPC14

15 Factorial // Pre: n  0 // Returns n! int factorial(int n) { int f = 1; int i = 0; while (i < n) { i = i + 1; f = fi; // f = i! } return f; } i f = i! 01 11 22 36 424 5120 Introduction to Programming© Dept. CS, UPC15

16 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


Download ppt "Functions Jordi Cortadella Department of Computer Science."

Similar presentations


Ads by Google