computer science 1620 functions. given a number n, the factorial of n, written n!, is computed as...
Post on 19-Dec-2015
216 views
TRANSCRIPT
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) x … x 1
3! = 3 x 2 x 1 = 6
5! = 5 x 4 x 3 x 2 x 1 = 120
0! = 1
Write a program that takes a number n from the user, and calculates the factorial of that number.
#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.
#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.
#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.
#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.
#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.
#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.
#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.
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
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.
#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.
#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.
#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.
#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
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
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
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
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
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
#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.
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.
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;}
Imagine a "factorial machine"one input, for a number none output, for the computed number n!
1205
factorial
Imagine a "factorial machine"one input, for a number none output, for the computed number n!
10
factorial
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)?
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;
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;}
#include <iostream> using namespace std;
void whoAmI() {cout << ”Robert" << endl;cout << "Lethbridge" << endl;
}
int main() {
whoAmI(); whoAmI(); return 0;}
RobertLethbridgeRobertLethbridge
Output:
Start Here
#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
#include <iostream> using namespace std;
void whoAmI() {cout << "Robert" << endl;cout << "Lethbridge" << endl;
}
int main() {
whoAmI(); whoAmI(); return 0;}
RobertLethbridgeRobertLethbridge
Output:
Perform output
#include <iostream> using namespace std;
void whoAmI() {cout << "Robert" << endl;cout << "Lethbridge" << endl;
}
int main() {
whoAmI(); whoAmI(); return 0;}
RobertLethbridgeRobertLethbridge
Output:
Perform output
#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
#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
#include <iostream> using namespace std;
void whoAmI() {cout << "Robert" << endl;cout << "Lethbridge" << endl;
}
int main() {
whoAmI(); whoAmI(); return 0;}
RobertLethbridgeRobertLethbridge
Output:
Perform output
#include <iostream> using namespace std;
void whoAmI() {cout << "Robert" << endl;cout << "Lethbridge" << endl;
}
int main() {
whoAmI(); whoAmI(); return 0;}
RobertLethbridgeRobertLethbridge
Output:
Perform output
#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
#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
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.
#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.
#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.
#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.
#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.
#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.
#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.
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)?
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
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; }
#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
#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
#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
#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.
#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.
#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
#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
#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.
#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.
#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.
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.
#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.
#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.
#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.
#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.
#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.
#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.
#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.
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)?
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?
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; }
#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
#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
#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
#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.
#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).
#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.
#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.
#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
#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.
#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).
#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:
#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!
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.
#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.
#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.
#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.
#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.
#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.
#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.
#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.
#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.
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)?
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.
#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.
#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.
#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.
#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.
#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.
#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.
#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.
#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.
#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.
#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.
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
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;}
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;}
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;}
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;}
Where have we seen functions?
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;}
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
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;
}
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;
}
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;
}
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
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);}
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);}
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);}
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);}
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
}
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);}
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);}
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);}
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);}
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;
}
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
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
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);}
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.
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
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);
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.
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)
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);}
A good (and common) format for your .cc files to follow:
preprocessor directives
function prototypes
int main() {
}
function definitions
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.
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.
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.
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.
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.
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.
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.
Functions - Examples
Write a function called power that returns a real value b raised to a positive power e.
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.
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.
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.
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.