Functions Part 2
Video
Key Takeways
Video Addendum
Assignment
- For each code sample (0 to 2):
- Walk through the memory table for at least 2 different inputs.
- Write down what you think each sample does (in complete sentences).
- Execute the code in a repl to validate your assumption. What was wrong / right about your step 2?
- Do you see room for improvement in this code? If so, create a new repl with all of your improvements.
- For each code sample (3 to 4):
- Describe what you think is wrong with the code (This means write it down in complete sentences!).
- Confirm your hypothesis by building a memory table.
- Double check your work by trying to run the code in a new Repl for each example.
- In that same repl, try and make the relevant fixes until you’re satisfied.
- For your updated calculator from lesson 05, are there any places you can use functions with inputs?
- If so, create a new repl that does the exact same task but uses functions. Write down why you created the functions you did.
- If not, describe why you aren’t able to use functions.
Code Samples
Sample 0
#include <iostream>
int Function2(int a) {
a = a + 1;
return a * a;
}
int Function1() {
int n;
std::cin >> n;
return n;
}
int main() {
int a = Function1();
int b = Function2(a);
std::cout << "A square with size bigger than " << a << std::endl;
std::cout << "Has an area of size at least " << b << std::endl;
return 0;
}
Sample 1
#include <iostream>
int GetUserInput() {
std::cout << "Enter a number: ";
int n;
std::cin >> n;
return n;
}
int FunctionP(int a, int b) {
return a * b;
}
int main() {
int c = GetUserInput();
int d = GetUserInput();
int a = FunctionP(c, d);
std::cout << "Area " << c << "x" << d << " = " << a << std::endl;
return 0;
}
Sample 2
#include <iostream>
int GetUserInput() {
std::cout << "Enter a number: ";
int n;
std::cin >> n;
return n;
}
char FunctionC(int c) {
if (c == 0) {
return 'a';
}
if (c == 1) {
return 'e';
}
if (c == 2) {
return 'i';
}
if (c == 3) {
return 'o';
}
if (c == 4) {
return 'u';
}
return ' ';
}
int FunctionD(int a) {
if (a == 0) {
std::cout << "th";
}
if (a == 1) {
std::cout << "st";
}
if (a == 2) {
std::cout << "th";
}
if (a == 3) {
std::cout << "rd";
}
if (a == 4) {
std::cout << "th";
}
return 0;
}
bool FunctionZ(int z) {
if (z < 0) {
return true;
}
if (z >= 5) {
return true;
}
return false;
}
int main() {
int a = GetUserInput();
bool r = FunctionZ(a);
if (r) {
std::cout << "Sorry bad number. Try again." << std::endl;
return 1;
}
char l = FunctionC(a);
std::cout << "The " << a;
a = FunctionD(a);
std::cout << " vowel is " << l << "." << std::endl;
return 0;
}
Sample 3
#include <iostream>
int functionz() {
std::cout << "Enter a number: ";
int n;
std::cin >> n;
return n;
}
int function_a(int a, int b) {
if (a > b) {
return b;
}
return a;
}
int main() {
int z = functionZ();
int b = functionZ()
int c = function_a(b);
std::cout << "The bigger number between : " << z << " " << b << " is " << c << "." << std::endl;
return 0;
}
Sample 4
#include <iostream>
float A(float p) {
return pi * 4.0 / 3.0 * p * p * p;
}
int main() {
float A = GetUserInput();
float b = A(A);
std::cout << "Volume of sphere with radius " << A << " is " << b << "." << std::endl;
return 0;
}
End of Assignment Checklist
- I finished all the assignments.
- I shared my assignments with others.
- I provided feedback for assignments of at least 2 others.
- I addressed the feedback from others and thanked them for the review.