computer science 1620 functions. given a number n, the factorial of n, written n!, is computed as...

149
Computer Science 1620 Functions

Post on 19-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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)

Computer Science 1620

Functions

Page 2: 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)

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

Page 3: 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)

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

Page 4: 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)

#include <iostream>#include <iomanip> 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.

Page 5: 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)

#include <iostream>#include <iomanip> 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.

Page 6: 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)

#include <iostream>#include <iomanip> 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 storethe factorial, since factorials are typically very big numbers.

Page 7: 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)

#include <iostream>#include <iomanip> 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.

Page 8: 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)

#include <iostream>#include <iomanip> 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.

Page 9: 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)

#include <iostream>#include <iomanip> 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.

Page 10: 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)

#include <iostream>#include <iomanip> 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.

Page 11: 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)
Page 12: 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)

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:

)!(!

!

rnr

nC

r

n nr −==⎟⎟

⎞⎜⎜⎝

134153712)!5112(!5

!112

5

112 1125 ≈

−==⎟⎟

⎞⎜⎜⎝

⎛C

Page 13: 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)

Write a program that takes two integers n and r from the user, and calculates Cn

r for those two numbers, and displays the results.

Page 14: 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)

#include <iostream>#include <iomanip> 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 Cn

r for those two numbers, and displays the results.

Page 15: 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)

#include <iostream>#include <iomanip> 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 Cn

r for those two numbers, and displays the results.

Page 16: 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)

#include <iostream>#include <iomanip> 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 Cn

r for those two numbers, and displays the results.

Page 17: 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)

#include <iostream>#include <iomanip> 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 Cn

r for those two numbers, and displays the results.

Here's where we needto calculate Cn

r

Page 18: 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)

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 Cn

r

Page 19: 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)

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 Cn

r

Page 20: 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)

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 Cn

r

Page 21: 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)

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 Cn

r

Page 22: 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)

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 Cn

r

Page 23: 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)

#include <iostream>#include <iomanip> 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 Cn

r for those two numbers.

Page 24: 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)

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.

Page 25: 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)

Write a program that takes two integers n and r from the user, and calculates Cn

r for those two numbers.

double <result> = 1.0; for (int i = 1; i <= <input>; i++) { factn *= i;}

#include <iostream>#include <iomanip> 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;}

Page 26: 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)

Imagine a "factorial machine"one input, for a number none output, for the computed number n!

1205

factorial

Page 27: 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)

Imagine a "factorial machine"one input, for a number none output, for the computed number n!

10

factorial

Page 28: 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)

Imagine a "factorial machine"one input, for a number none 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)?

Page 29: 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)

Functiona subprograma piece of code that can be re-usedcalled from other places in the programexample: code to write out my name and

location

cout << ”Robert" << endl;cout << "Lethbridge" << endl;

Page 30: 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)

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 <iostream> using namespace std;

void whoAmI() {cout << ”Robert" << endl;cout << "Lethbridge" << endl;

}

int main() {

whoAmI(); whoAmI(); return 0;}

Page 31: 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)

#include <iostream> using namespace std;

void whoAmI() {cout << ”Robert" << endl;cout << "Lethbridge" << endl;

}

int main() {

whoAmI(); whoAmI(); return 0;}

RobertLethbridgeRobertLethbridge

Output:

Start Here

Page 32: 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)

#include <iostream> using namespace std;

void whoAmI() {cout << ”Robert" << endl;cout << "Lethbridge" << endl;

}

int main() {

whoAmI(); whoAmI(); return 0;}

RobertLethbridgeRobertLethbridge

Output:

At this point, we move tothe first line in the function whoAmI

Page 33: 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)

#include <iostream> using namespace std;

void whoAmI() {cout << "Robert" << endl;cout << "Lethbridge" << endl;

}

int main() {

whoAmI(); whoAmI(); return 0;}

RobertLethbridgeRobertLethbridge

Output:

Perform output

Page 34: 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)

#include <iostream> using namespace std;

void whoAmI() {cout << "Robert" << endl;cout << "Lethbridge" << endl;

}

int main() {

whoAmI(); whoAmI(); return 0;}

RobertLethbridgeRobertLethbridge

Output:

Perform output

Page 35: 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)

#include <iostream> using namespace std;

void whoAmI() {cout << "Robert" << endl;cout << "Lethbridge" << endl;

}

int main() {

whoAmI(); whoAmI(); return 0;}

RobertLethbridgeRobertLethbridge

Output:

Function ended, start again right after function call

Page 36: 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)

#include <iostream> using namespace std;

void whoAmI() {cout << "Robert" << endl;cout << "Lethbridge" << endl;

}

int main() {

whoAmI(); whoAmI(); return 0;}

RobertLethbridgeRobertLethbridge

Output:

At this point, we move tothe first line in the function whoAmI

Page 37: 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)

#include <iostream> using namespace std;

void whoAmI() {cout << "Robert" << endl;cout << "Lethbridge" << endl;

}

int main() {

whoAmI(); whoAmI(); return 0;}

RobertLethbridgeRobertLethbridge

Output:

Perform output

Page 38: 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)

#include <iostream> using namespace std;

void whoAmI() {cout << "Robert" << endl;cout << "Lethbridge" << endl;

}

int main() {

whoAmI(); whoAmI(); return 0;}

RobertLethbridgeRobertLethbridge

Output:

Perform output

Page 39: 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)

#include <iostream> using namespace std;

void whoAmI() {cout << "Robert" << endl;cout << "Lethbridge" << endl;

}

int main() {

whoAmI(); whoAmI(); return 0;}

RobertLethbridgeRobertLethbridge

Output:

Function ended, start again right after function call

Page 40: 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)

#include <iostream> using namespace std;

void whoAmI() {cout << "Robert" << endl;cout << "Lethbridge" << endl;

}

int main() {

whoAmI(); whoAmI(); return 0;}

RobertLethbridgeRobertLethbridge ...

Output:

At this point, program terminates

Page 41: 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)

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.

Page 42: 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)

#include <iostream>#include <iomanip> 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.

Page 43: 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)

#include <iostream>#include <iomanip> 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.

Page 44: 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)

#include <iostream>#include <iomanip> 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.

Page 45: 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)

#include <iostream>#include <iomanip> 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.

Page 46: 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)

#include <iostream>#include <iomanip> 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.

Page 47: 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)

#include <iostream>#include <iomanip> 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.

Page 48: 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)

Imagine a "factorial machine"one input, for a number none 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)?

Page 49: 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)

Function Inputsending data to the functionaccomplished through a list of parameters

a list of variablescontained in parameter list

we set these variables in the call using arguments

a list of expressions

Page 50: 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)

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; }

Page 51: 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)

#include <iostream> 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

Page 52: 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)

#include <iostream> 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

Page 53: 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)

#include <iostream> 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 tothe first line in the function squareThe parameter x gets the value of the expression in the call, which is2

Page 54: 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)

#include <iostream> 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 hasvalue 2.

Page 55: 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)

#include <iostream> 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, startright after function call.

Page 56: 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)

#include <iostream> 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

Page 57: 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)

#include <iostream> 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 tothe first line in the function squareThe parameter x gets the value of the expression in the call, which is2+1 = 3

Page 58: 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)

#include <iostream> 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 hasvalue 3.

Page 59: 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)

#include <iostream> 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, startright after function call.

Page 60: 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)

#include <iostream> 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.

Page 61: 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)

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.

Page 62: 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)

#include <iostream>#include <iomanip> 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.

Page 63: 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)

#include <iostream>#include <iomanip> 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.

Page 64: 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)

#include <iostream>#include <iomanip> 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.

Page 65: 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)

#include <iostream>#include <iomanip> 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.

Page 66: 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)

#include <iostream>#include <iomanip> 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.

Page 67: 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)

#include <iostream>#include <iomanip> 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.

Page 68: 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)

#include <iostream>#include <iomanip> 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.

Page 69: 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)

Imagine a "factorial machine"one input, for a number none 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)?

Page 70: 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)

Function Output receiving data from a functionaccomplished through a return statement recall that an expression in C++ has a value

2; // the value of this expression is 22 + 3; // the value of this expression is 52 > 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 valuesquare(2);square(2+1);

what is the value ofthese expressions?

Page 71: 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)

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; }

Page 72: 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)

#include <iostream> 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

Page 73: 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)

#include <iostream> 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

Page 74: 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)

#include <iostream> 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 tothe first line in the function squareThe parameter x gets the value of the expression in the call, which is2

Page 75: 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)

#include <iostream> 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 hasvalue 2.

Page 76: 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)

#include <iostream> 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 valuestored in result

The value of the expressionsquare(2) is the return value (4).

Page 77: 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)

#include <iostream> 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.

Page 78: 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)

#include <iostream> 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.

Page 79: 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)

#include <iostream> 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 tothe first line in the function squareThe parameter x gets the value of the expression in the call, which is(2+1) = 3

Page 80: 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)

#include <iostream> 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 hasvalue 3.

Page 81: 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)

#include <iostream> 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 valuestored in result

The value of the expressionsquare(3) is the return value (9).

Page 82: 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)

#include <iostream> 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:

Page 83: 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)

#include <iostream> 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!

Page 84: 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)

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.

Page 85: 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)

#include <iostream>#include <iomanip> 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.

Page 86: 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)

#include <iostream>#include <iomanip> 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.

Page 87: 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)

#include <iostream>#include <iomanip> 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.

Page 88: 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)

#include <iostream>#include <iomanip> 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.

Page 89: 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)

#include <iostream>#include <iomanip> 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.

Page 90: 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)

#include <iostream>#include <iomanip> 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.

Page 91: 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)

#include <iostream>#include <iomanip> 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.

Page 92: 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)

#include <iostream>#include <iomanip> 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.

Page 93: 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)

Imagine a "factorial machine"one input, for a number none 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)?

Page 94: 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)

Write a program that takes two integers n and r from the user, and calculates Cn

r for those two numbers, and displays the results.

Page 95: 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)

#include <iostream>#include <iomanip> 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 Cn

r for those two numbers.

Rewrite this programusing the factorial function.

Page 96: 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)

#include <iostream>#include <iomanip> 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 Cn

r for those two numbers.

Page 97: 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)

#include <iostream>#include <iomanip> 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 Cn

r for those two numbers.

Page 98: 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)

#include <iostream>#include <iomanip> 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 Cn

r for those two numbers.

Page 99: 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)

#include <iostream>#include <iomanip> 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 Cn

r for those two numbers.

Page 100: 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)

#include <iostream>#include <iomanip> 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 Cn

r for those two numbers.

Page 101: 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)

#include <iostream>#include <iomanip> 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 Cn

r for those two numbers.

Page 102: 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)

#include <iostream>#include <iomanip> 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 Cn

r for those two numbers.

Page 103: 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)

#include <iostream>#include <iomanip> 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 Cn

r for those two numbers.

Page 104: 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)

#include <iostream>#include <iomanip> 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 Cn

r for those two numbers.

Page 105: 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)

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)

returntype

functionname

parameterlist

Page 106: 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)

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;}

Page 107: 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)

Functions as expressions the value of a function call is its return value return value can be used in other

expressionsint 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;}

Page 108: 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)

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;}

Page 109: 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)

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;}

Page 110: 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)

Where have we seen functions?

Page 111: 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)

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;}

Page 112: 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)

Functions can call other functionsExample: write a program that takes an

integer n, and outputs a table of all values Cn

r for all values 0 <= r <= n

Page 113: 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)
Page 114: 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)

Without functions:#include <iostream>#include <iomanip> 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;

}

Page 115: 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)

With function factorial:#include <iostream>#include <iomanip> 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;

}

Page 116: 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)

With function factorial:#include <iostream>#include <iomanip> 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;

}

Page 117: 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)

With factorial, looks much betterwhat 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

Page 118: 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)

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<int>(result);}

Page 119: 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)

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<int>(result);}

Page 120: 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)

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<int>(result);}

Page 121: 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)

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<int>(result);}

Page 122: 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)

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

}

Page 123: 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)

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<int>(result);}

Page 124: 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)

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<int>(result);}

Page 125: 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)

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<int>(result);}

Page 126: 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)

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<int>(result);}

Page 127: 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)

With function factorial and choose:#include <iostream>#include <iomanip> 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<int>(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;

}

Page 128: 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)

Function Placement

2 key ideas1. 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 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

Page 129: 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)

Main function code is more interesting to user than helper functions thus, it is typically placed near the top of

your file, before other functionshowever, if we place a function after the

main function, we can't call it from mainC++ must know the parameter types and return

type of a function before it can be called, for type checking

Page 130: 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)

With function factorial and choose:#include <iostream>#include <iomanip> 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<int>(result);}

Page 131: 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)

With function factorial and choose:#include <iostream>#include <iomanip> 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<int>(result);}

This will not compile.

Page 132: 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)

Function Prototypea 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

Page 133: 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)

Function Prototype function header, without bodyappended 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);

Page 134: 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)

With function factorial and choose:#include <iostream>#include <iomanip> 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<int>(result);}

This will compile, sincemain can check types.

Page 135: 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)

Function Prototypesince 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)

Page 136: 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)

With function factorial and choose:#include <iostream>#include <iomanip> 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<int>(result);}

Page 137: 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)

A good (and common) format for your .cc files to follow:

preprocessor directives

function prototypes

int main() {

}

function definitions

Page 138: 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)

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.

Page 139: 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)

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.

Page 140: 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)

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.

Page 141: 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)

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.

Page 142: 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)

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.

Page 143: 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)

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.

Page 144: 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)

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.

Page 145: 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)

Functions - Examples

Write a function called power that returns a real value b raised to a positive power e.

Page 146: 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)

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.

Page 147: 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)

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.

Page 148: 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)

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.

Page 149: 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)

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.