Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)

Similar presentations


Presentation on theme: "Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)"— Presentation transcript:

1 Computer Science 1620 Functions

2 Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2) x … x 1 3! = 3 x 2 x 1 = 6 5! = 5 x 4 x 3 x 2 x 1= 120 0! = 1

3 Write a program that takes a number n from the user, and calculates the factorial of that number.

4 #include using namespace std; int main() { int n; cout << "Please enter a number:"; cin >> n; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } cout << fixed << setprecision(0); cout << n << "! = " << factn << endl; return 0; } Write a program that takes a number from the user, and calculates the factorial of that number.

5 #include using namespace std; int main() { int n; cout << "Please enter a number:"; cin >> n; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } cout << fixed << setprecision(0); cout << n << "! = " << factn << endl; return 0; } Write a program that takes a number from the user, and calculates the factorial of that number.

6 #include using namespace std; int main() { int n; cout << "Please enter a number:"; cin >> n; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } cout << fixed << setprecision(0); cout << n << "! = " << factn << endl; return 0; } Write a program that takes a number from the user, and calculates the factorial of that number. We'll use a double to store the factorial, since factorials are typically very big numbers.

7 #include using namespace std; int main() { int n; cout << "Please enter a number:"; cin >> n; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } cout << fixed << setprecision(0); cout << n << "! = " << factn << endl; return 0; } Write a program that takes a number from the user, and calculates the factorial of that number.

8 #include using namespace std; int main() { int n; cout << "Please enter a number:"; cin >> n; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } cout << fixed << setprecision(0); cout << n << "! = " << factn << endl; return 0; } Write a program that takes a number from the user, and calculates the factorial of that number.

9 #include using namespace std; int main() { int n; cout << "Please enter a number:"; cin >> n; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } cout << fixed << setprecision(0); cout << n << "! = " << factn << endl; return 0; } Write a program that takes a number from the user, and calculates the factorial of that number.

10 #include using namespace std; int main() { int n; cout << "Please enter a number:"; cin >> n; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } cout << fixed << setprecision(0); cout << n << "! = " << factn << endl; return 0; } Write a program that takes a number from the user, and calculates the factorial of that number.

11

12 Example: Given a set of n objects, the number of combinations of those objects taken r at a time can be calculated as: ie. Given a class of 112 students, the number of possible groups of 5 students is:

13 Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers, and displays the results.

14 #include using namespace std; int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= (n-r); i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers, and displays the results.

15 #include using namespace std; int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= (n-r); i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers, and displays the results.

16 #include using namespace std; int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= (n-r); i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers, and displays the results.

17 #include using namespace std; int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= (n-r); i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers, and displays the results. Here's where we need to calculate C n r

18 Problem Breakdown: double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double result = factn / (factr * factnr); Calculate n! Calculate r! Calculate (n-r)! Combine results into C n r

19 Problem Breakdown: double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double result = factn / (factr * factnr); Calculate n! Calculate r! Calculate (n-r)! Combine results into C n r

20 Problem Breakdown: double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double result = factn / (factr * factnr); Calculate n! Calculate r! Calculate (n-r)! Combine results into C n r

21 Problem Breakdown: double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); Calculate n! Calculate r! Calculate (n-r)! Combine results into C n r

22 Problem Breakdown: double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); Calculate n! Calculate r! Calculate (n-r)! Combine results into C n r

23 #include using namespace std; int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers.

24 Does this solve our problem? Yes Any problems with previous code? duplication!!! the calculation for factorial is the same, but the code to calculate is repeated 3 times the only change is the stopping condition value, and the variable that we set.

25 Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers. double = 1.0; for (int i = 1; i ; i++) { factn *= i; } #include using namespace std; int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; }

26 Imagine a "factorial machine" one input, for a number n one output, for the computed number n! 120 5 factorial

27 Imagine a "factorial machine" one input, for a number n one output, for the computed number n! 1 0 factorial

28 Imagine a "factorial machine" one input, for a number n one output, for the computed number n! x result double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } 120 5 3 questions where does this code go? how do we set the input (x)? how do we get the output (result)?

29 Function a subprogram a piece of code that can be re-used called from other places in the program example: code to write out my name and location cout << ”Robert" << endl; cout << "Lethbridge" << endl;

30 Function written outside of main every function has a: return type name parameter list you call a function by typing its name, followed by args #include using namespace std; void whoAmI() { cout << ”Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; }

31 #include using namespace std; void whoAmI() { cout << ”Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; } Robert Lethbridge Robert Lethbridge Output: Start Here

32 #include using namespace std; void whoAmI() { cout << ”Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; } Robert Lethbridge Robert Lethbridge Output: At this point, we move to the first line in the function whoAmI

33 #include using namespace std; void whoAmI() { cout << "Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; } Robert Lethbridge Robert Lethbridge Output: Perform output

34 #include using namespace std; void whoAmI() { cout << "Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; } Robert Lethbridge Robert Lethbridge Output: Perform output

35 #include using namespace std; void whoAmI() { cout << "Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; } Robert Lethbridge Robert Lethbridge Output: Function ended, start again right after function call

36 #include using namespace std; void whoAmI() { cout << "Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; } Robert Lethbridge Robert Lethbridge Output: At this point, we move to the first line in the function whoAmI

37 #include using namespace std; void whoAmI() { cout << "Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; } Robert Lethbridge Robert Lethbridge Output: Perform output

38 #include using namespace std; void whoAmI() { cout << "Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; } Robert Lethbridge Robert Lethbridge Output: Perform output

39 #include using namespace std; void whoAmI() { cout << "Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; } Robert Lethbridge Robert Lethbridge Output: Function ended, start again right after function call

40 #include using namespace std; void whoAmI() { cout << "Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; } Robert Lethbridge Robert Lethbridge... Output: At this point, program terminates

41 Example: Write a program with a function called factorial that computes the factorial of 5 and writes the value to output. Call that function from the main function.

42 #include using namespace std; void factorial() { double result = 1.0; for (int i = 1; i <= 5; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of 5 and writes the value to output. Call that function from the main function.

43 #include using namespace std; void factorial() { double result = 1.0; for (int i = 1; i <= 5; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of 5 and writes the value to output. Call that function from the main function.

44 #include using namespace std; void factorial() { double result = 1.0; for (int i = 1; i <= 5; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of 5 and writes the value to output. Call that function from the main function.

45 #include using namespace std; void factorial() { double result = 1.0; for (int i = 1; i <= 5; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of 5 and writes the value to output. Call that function from the main function.

46 #include using namespace std; void factorial() { double result = 1.0; for (int i = 1; i <= 5; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of 5 and writes the value to output. Call that function from the main function.

47 #include using namespace std; void factorial() { double result = 1.0; for (int i = 1; i <= 5; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of 5 and writes the value to output. Call that function from the main function.

48 Imagine a "factorial machine" one input, for a number n one output, for the computed number n! x result double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } 120 5 3 questions where does this code go? how do we set the input (x)? how do we get the output (result)?

49 Function Input sending data to the function accomplished through a list of parameters a list of variables contained in parameter list we set these variables in the call using arguments a list of expressions

50 Function Inputs declared as parameters function can have 0 or more parameters specify the type and the variable name Function arguments the value of each argument is assigned to corresponding parameter variable void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "(2+1) x (2+1) = "; square(2+1); return 0; }

51 #include using namespace std; void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "\n(2+1) * (2+1) = "; square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Start Here

52 #include using namespace std; void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "\n(2+1) * (2+1) = "; square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Perform Output

53 #include using namespace std; void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "\n(2+1) * (2+1) = "; square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: At this point, we move to the first line in the function square The parameter x gets the value of the expression in the call, which is 2

54 #include using namespace std; void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "\n(2+1) * (2+1) = "; square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Perform Output. Note that x has value 2.

55 #include using namespace std; void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "\n(2+1) * (2+1) = "; square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Function ended, start right after function call.

56 #include using namespace std; void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "\n(2+1) * (2+1) = "; square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Perform Output

57 #include using namespace std; void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "\n(2+1) * (2+1) = "; square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: At this point, we move to the first line in the function square The parameter x gets the value of the expression in the call, which is 2+1 = 3

58 #include using namespace std; void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "\n(2+1) * (2+1) = "; square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Perform Output. Note that x has value 3.

59 #include using namespace std; void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "\n(2+1) * (2+1) = "; square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Function ended, start right after function call.

60 #include using namespace std; void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "\n(2+1) * (2+1) = "; square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Program terminates.

61 Example: Write a program with a function called factorial that computes the factorial of x (x is an inputted number) and writes the value to output. Call that function from the main function with the value 5.

62 #include using namespace std; void factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and writes the value to output. Call that function from the main function with the value 5.

63 #include using namespace std; void factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and writes the value to output. Call that function from the main function with the value 5.

64 #include using namespace std; void factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and writes the value to output. Call that function from the main function with the value 5.

65 #include using namespace std; void factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and writes the value to output. Call that function from the main function with the value 5.

66 #include using namespace std; void factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and writes the value to output. Call that function from the main function with the value 5.

67 #include using namespace std; void factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and writes the value to output. Call that function from the main function with the value 5.

68 #include using namespace std; void factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and writes the value to output. Call that function from the main function with the value 5.

69 Imagine a "factorial machine" one input, for a number n one output, for the computed number n! x result double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } 120 5 3 questions where does this code go? how do we set the input (x)? how do we get the output (result)?

70 Function Output receiving data from a function accomplished through a return statement recall that an expression in C++ has a value 2; // the value of this expression is 2 2 + 3; // the value of this expression is 5 2 > 3; // the value of this expression is false (0) i = 2; // the value of this expression is 2 a function call is an expression with a value square(2); square(2+1); what is the value of these expressions?

71 Function Output given by the return statement Function call the value of the function call is the value that is returned the type of the value returned by a function call is given by its return type int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; }

72 #include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Start here

73 #include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Perform Output

74 #include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: At this point, we move to the first line in the function square The parameter x gets the value of the expression in the call, which is 2

75 #include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Do calculation. Note that x has value 2.

76 #include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Returns value stored in result The value of the expression square(2) is the return value (4).

77 #include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Perform output.

78 #include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Perform output.

79 #include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: At this point, we move to the first line in the function square The parameter x gets the value of the expression in the call, which is (2+1) = 3

80 #include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Do calculation. Note that x has value 3.

81 #include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Returns value stored in result The value of the expression square(3) is the return value (9).

82 #include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Perform Output:

83 #include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Terminate Program!

84 Example: Write a program with a function called factorial that computes the factorial of x (x is an inputted number) and returns that value. Call that function from the main function with the value 5.

85 #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } return result; } int main() { cout << fixed << setprecision(0); cout << "The factorial of 5 is "; cout << factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and returns that value. Call that function from the main function with the value 5.

86 #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } return result; } int main() { cout << fixed << setprecision(0); cout << "The factorial of 5 is "; cout << factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and returns that value. Call that function from the main function with the value 5.

87 #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } return result; } int main() { cout << fixed << setprecision(0); cout << "The factorial of 5 is "; cout << factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and returns that value. Call that function from the main function with the value 5.

88 #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } return result; } int main() { cout << fixed << setprecision(0); cout << "The factorial of 5 is "; cout << factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and returns that value. Call that function from the main function with the value 5.

89 #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } return result; } int main() { cout << fixed << setprecision(0); cout << "The factorial of 5 is "; cout << factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and returns that value. Call that function from the main function with the value 5.

90 #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } return result; } int main() { cout << fixed << setprecision(0); cout << "The factorial of 5 is "; cout << factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and returns that value. Call that function from the main function with the value 5.

91 #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } return result; } int main() { cout << fixed << setprecision(0); cout << "The factorial of 5 is "; cout << factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and returns that value. Call that function from the main function with the value 5.

92 #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } return result; } int main() { cout << fixed << setprecision(0); cout << "The factorial of 5 is "; cout << factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and returns that value. Call that function from the main function with the value 5.

93 Imagine a "factorial machine" one input, for a number n one output, for the computed number n! x result double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } cout << result; 120 5 3 questions where does this code go? how do we set the input (x)? how do we get the output (result)?

94 Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers, and displays the results.

95 #include using namespace std; int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers. Rewrite this program using the factorial function.

96 #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers.

97 #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers.

98 #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = factorial(n); double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers.

99 #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = factorial(n); double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers.

100 #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = factorial(n); double factr = factorial(r); double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers.

101 #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = factorial(n); double factr = factorial(r); double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers.

102 #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = factorial(n); double factr = factorial(r); double factnr = factorial(n – r); double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers.

103 #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double result = factorial(n) / (factorial(r) * factorial(n – r)); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers.

104 #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << factorial(n) / (factorial(r) * factorial(n – r)) << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers.

105 The anatomy of a function: double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } function header (declaration) function body (definition) return type function name parameter list

106 Functions with no return value sometimes, we do not need a value back from our function we indicate this with the void return type return keyword in void function simply exits function void square(int x) { int result = x * x; cout << result; } void inverse(int x) { if (x == 0) return; double result = 1.0 / x; cout << result; }

107 Functions as expressions the value of a function call is its return value return value can be used in other expressions int square(int x) { int result = x * x; return result; } int main() { cout << square(2);// outputs 4 cout << square(2) + square(3);// outputs 13 cout << square(square(2));// outputs 16 return 0; }

108 Functions with multiple inputs functions can have as many parameters as you like first argument aligns with first parameter, second with second, etc … int sum(int x, int y) { return x + y; } int main() { cout << sum(2, 3);// outputs 5 return 0; }

109 More than one return statement a function may have more than one return statement function terminates as soon as return statement is reached int max(int x, int y) { if (x > y) return x; return y; } int main() { cout << max(2, 3) << endl; // outputs 3 cout << max(3, 2) << endl; // outputs 3 return 0; }

110 Where have we seen functions?

111 The main function: a function called by operating system can have parameters (more on this later) has a return value 0 – program terminated normally nonzero – program terminated abnormally int main() { return 0; }

112 Functions can call other functions Example: write a program that takes an integer n, and outputs a table of all values C n r for all values 0 <= r <= n

113

114 Without functions: #include using namespace std; int main() { int n; cout << "Please enter n:"; cin >> n; cout << " r n choose r" << endl; cout << "--- ----------" << endl; for (int r = 0; r <= n; r++) { double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << setw(3) << r << setw(15) << result << endl; } return 0; }

115 With function factorial: #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n; cout << "Please enter n:"; cin >> n; cout << " r n choose r" << endl; cout << "--- ----------" << endl; for (int r = 0; r <= n; r++) { double factn = factorial(n); double factr = factorial(r); double factnr = factorial(n-r); double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << setw(3) << r << setw(15) << result << endl; } return 0; }

116 With function factorial: #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n; cout << "Please enter n:"; cin >> n; cout << " r n choose r" << endl; cout << "--- ----------" << endl; for (int r = 0; r <= n; r++) { double result = factorial(n) / (factorial(r) * factorial(n-r)); cout << fixed << setprecision(0); cout << setw(3) << r << setw(15) << result << endl; } return 0; }

117 With factorial, looks much better what is a good candidate for a function in this code? calculating n choose r write a function called choose that takes two values n and r, and returns n choose r inputs: n (int), r(int) output: an integer

118 write a function called choose that takes two values n and r, and calculates and returns n choose r inputs: n (int), r(int) output: an integer int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); }

119 write a function called choose that takes two values n and r, and calculates and returns n choose r inputs: n (int), r(int) output: an integer int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); }

120 write a function called choose that takes two values n and r, and calculates and returns n choose r inputs: n (int), r(int) output: an integer int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); }

121 write a function called choose that takes two values n and r, and calculates and returns n choose r inputs: n (int), r(int) output: an integer int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); }

122 write a function called choose that takes two values n and r, and calculates and returns n choose r inputs: n (int), r(int) output: an integer int choose(int n, int r) { double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); double result }

123 write a function called choose that takes two values n and r, and calculates and returns n choose r inputs: n (int), r(int) output: an integer note: this assumes that we have the factorial function defined in our program!!! int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); }

124 write a function called choose that takes two values n and r, and calculates and returns n choose r inputs: n (int), r(int) output: an integer int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); }

125 write a function called choose that takes two values n and r, and calculates and returns n choose r inputs: n (int), r(int) output: an integer int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); }

126 write a function called choose that takes two values n and r, and calculates and returns n choose r inputs: n (int), r(int) output: an integer int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); }

127 With function factorial and choose: #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); } int main() { int n; cout << "Please enter n:"; cin >> n; cout << " r n choose r" << endl; cout << "--- ----------" << endl; for (int r = 0; r <= n; r++) { cout << setw(3) << r << setw(15) << choose(n, r) << endl; } return 0; }

128 Function Placement 2 key ideas 1. The engine of a C++ program is the main method allocates tasks to other functions program functionality can usually be inferred from a well written main method, as opposed to a poorly written onewell written main methoda poorly written one ie. suppose someone asks: "What does your program do?" "program outputs n choose r for all 0 <= r <= n" 2. the user is typically interested in what function does, not how it does it ex. square root

129 Main function code is more interesting to user than helper functions thus, it is typically placed near the top of your file, before other functions however, if we place a function after the main function, we can't call it from main C++ must know the parameter types and return type of a function before it can be called, for type checking

130 With function factorial and choose: #include using namespace std; int main() { int n; cout << "Please enter n:"; cin >> n; cout << " r n choose r" << endl; cout << "--- ----------" << endl; for (int r = 0; r <= n; r++) { cout << setw(3) << r << setw(15) << choose(n, r) << endl; } return 0; } double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); }

131 With function factorial and choose: #include using namespace std; int main() { int n; cout << "Please enter n:"; cin >> n; cout << " r n choose r" << endl; cout << "--- ----------" << endl; for (int r = 0; r <= n; r++) { cout << setw(3) << r << setw(15) << choose(n, r) << endl; } return 0; } double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); } This will not compile.

132 Function Prototype a function declaration (header) without a definition (body) tells the compiler what the parameters and return type of the function will be, when it is defined allows C++ to do type checking prior to function definition

133 Function Prototype function header, without body appended with semicolon { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } function header (declaration) function body (definition) double factorial(int x);

134 With function factorial and choose: #include using namespace std; double factorial(int x); int choose(int n, int r); int main() { int n; cout << "Please enter n:"; cin >> n; cout << " r n choose r" << endl; cout << "--- ----------" << endl; for (int r = 0; r <= n; r++) { cout << setw(3) << r << setw(15) << choose(n, r) << endl; } return 0; } double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); } This will compile, since main can check types.

135 Function Prototype since we are only type checking, parameter names are of no use can also be written as (more common): double factorial(int x) double factorial(int)

136 With function factorial and choose: #include using namespace std; double factorial(int); int choose(int, int); int main() { int n; cout << "Please enter n:"; cin >> n; cout << " r n choose r" << endl; cout << "--- ----------" << endl; for (int r = 0; r <= n; r++) { cout << setw(3) << r << setw(15) << choose(n, r) << endl; } return 0; } double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); }

137 A good (and common) format for your.cc files to follow: preprocessor directives function prototypes int main() { } function definitions

138 Functions - Examples Write a function called odd that takes an integer number, and returns true if that number is odd, or false if the number is even.

139 bool odd(int x) { if (x % 2 == 1) { return true; } return false; } Write a function called odd that takes an integer number, and returns true if that number is odd, or false if the number is even.

140 bool odd(int x) { if (x % 2 == 1) { return true; } return false; } Write a function called odd that takes an integer number, and returns true if that number is odd, or false if the number is even.

141 bool odd(int x) { if (x % 2 == 1) { return true; } return false; } Write a function called odd that takes an integer number, and returns true if that number is odd, or false if the number is even.

142 bool odd(int x) { if (x % 2 == 1) { return true; } return false; } Write a function called odd that takes an integer number, and returns true if that number is odd, or false if the number is even.

143 bool odd(int x) { if (x % 2 == 1) { return true; } return false; } Write a function called odd that takes an integer number, and returns true if that number is odd, or false if the number is even.

144 bool odd(int x) { return (x % 2 == 1); } Write a function called odd that takes an integer number, and returns true if that number is odd, or false if the number is even.

145 Functions - Examples Write a function called power that returns a real value b raised to a positive power e.

146 double power(double b, int e) { double result = 1.0; for (int i = 1; i <= e; i++) result *= b; return result; } Write a function called power that returns a real value b raised to a positive power e.

147 double power(double b, int e) { double result = 1.0; for (int i = 1; i <= e; i++) result *= b; return result; } Write a function called power that returns a real value b raised to a positive power e.

148 double power(double b, int e) { double result = 1.0; for (int i = 1; i <= e; i++) result *= b; return result; } Write a function called power that returns a real value b raised to a positive power e.

149 double power(double b, int e) { double result = 1.0; for (int i = 1; i <= e; i++) result *= b; return result; } Write a function called power that returns a real value b raised to a positive power e.


Download ppt "Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)"

Similar presentations


Ads by Google