Week 4 EECS 183 MAXIM ALEKSA maximal.io
Agenda Functions Scope Conditions Boolean Expressions Lab 2 Project 2 Q&A
Lectures 15% 36% 19% 8:30am 10:00am with Bill Arthur 10:00am 11:30am with Mary Lou Dorf 1:00pm 2:30pm with Héctor García-Ramírez 4pm 5:30pm with Bill Arthur 30%
Enrolled
Favorite apps
maximal.io/eecs183 slides, notes, source code, links, et cetera
Functions
Functions side effects 0 or more inputs 0 or 1 output Black box
Functions with return value int score0 = 100; int score1 = 80; int bestscore = max(score0, score1); cout << "The best score is " << bestscore << endl;
Moose or Meese? cout << pluralize("moose", "meese", n);
Singular or plural pluralstring singularstring number singularstring if number is 1 pluralstring otherwise pluralize
Singular or plural 4 "gallons" "gallons" "gallon" pluralize
Functions with side effects int main() { } printheader(); void printheader() { } cout << "Shopping List for Cupcakes" << endl;
Functions with side effects int main() { sayhello("hannah"); sayhello("michael"); } void sayhello(string name) { } cout << "Hello, " << name << "!" << endl;
Why functions? Organization Simplification Reusability
Nested expressions double result = pow(sqrt(4), ceil(abs(-2) + 0.75));
Libraries iostream string cmath algorithm
Defining a function int add(int a, int b) { int sum = a + b; return sum; }
Defining a function function header / signature int add(int a, int b) { int sum = a + b; return sum; }
Defining a function header / signature int add(int a, int b) { body int sum = a + b; return sum; }
Defining a function return type int add(int a, int b) { int sum = a + b; return sum; }
Defining a function name return type int add(int a, int b) { int sum = a + b; return sum; }
Defining a function name parameter list return type int add(int a, int b) { int sum = a + b; return sum; }
Defining a function header / signature int add(int a, int b) { body int sum = a + b; return sum; }
Defining a function int add(int a, int b) { int sum = a + b; return statement } return sum;
Declaring a function name parameter list return type int add(int a, int b);
Using a function #include <iostream> using namespace std; int add(int a, int b); int main() { int sum = add(1, 2); cout << sum << endl; } int add(int a, int b) { int sum = a + b; return sum; }
Using a function #include <iostream> using namespace std; prototype / declaration function call int add(int a, int b); int main() { int sum = add(1, 2); cout << sum << endl; } implementation / definition int add(int a, int b) { int sum = a + b; return sum; }
RME /** * Requires: Nothing. * Modifies: Nothing. * Effects: Returns sum of a and b. */ int add(int a, int b); int add(int a, int b) { int result = a + b; return result; }
RME /** * Requires: n >= 0. * Modifies: Nothing. * Effects: Returns square root of n. */ double sqrt(double n); double sqrt(double n) { double result =??? return result; }
RME /** * Requires: Nothing. * Modifies: cout. * Effects: Says hello to someone with name name. */ void sayhello(string name); void sayhello(string name) { } cout << "Hello " << name << "!\n";
Scope main sqrt x: 5 n: 5
Scope increment-0.cpp increment-1.cpp increment-2.cpp
Scope Local variables (defined inside functions) are only known within functions wherein they were defined. Local variables are passed by value to other functions. That means that other functions receive a copy of the value, not the variable itself. The variable in the original function is unchanged unless overwritten.
scope.cpp #include <iostream> using namespace std; // prototypes void foo(); void bar(); // global variable int x = 5; int main() { cout << x << endl; int x = 7; cout << x << endl; foo(); cout << x << endl; bar(); } void foo() { x = 10; int x = 42; cout << x << endl; } void bar() { cout << x << endl; }
Swap 2 a b 5
Swap int a = 183; int b = 281; int temp = a; a = b; b = temp; cout << "a is " << a << endl; cout << "b is " << b << endl;
Swap void swap(int a, int b) { int temp = a; a = b; b = temp; }
Deadlines Today 27 Assignment 2 Monday 30 Lab 2 Friday 3 Project 2
Comparison Operators < <= x > >= y ==!=
Conditions if (condition) { } // do this
Conditions if (condition) { // do this } else { // do that }
Conditions if (course == 183) { cout << "EECS 183 is Elementary Programming Concepts" << endl; } else { cout << "I'm not familiar with that class!" << endl; }
Conditions if (condition) { // do this } else if (condition) { // do that } else { // do this other thing }
Conditions if (course == 183) { cout << "EECS 183 is Elementary Programming Concepts" << endl; } else if (course == 203) { cout << "EECS 203 is Discrete Mathematics" << endl; } else { cout << "I'm not familiar with that class!" << endl; }
Conditions if (x < y) { cout << "x is less than y" << endl; } else if (x > y) { cout << "x is greater than y" << endl; } else { cout << "x is equal to y" << endl; }
Conditions conditions-0.cpp conditions-1.cpp
True or false? if is a function. if (condition) { } // do this
True or false? Every if statement needs an else statement.
Conditions if (condition) { // do something } else { // do nothing }
Conditions if (condition) { // do something } else { // do nothing }
True or false? Every else statement must go with an if statement.
True or false? Order of if / else if / else statements matters.
Logical Operators &&!
Boolean Expressions if (condition && condition) { // do this if both conditions are true } if (condition condition) { // do this either condition is true } if (!condition) { // do this if condition is false }
Conditions if (condition) { // do nothing } else { // do something }
Conditions if (condition) { // do nothing } else { // do something }
Conditions if (!condition) { } // do something
Boolean Expressions Write a boolean expression in C++ that means a < b < c in a math class. Hint: this boolean expressions checks if a is equal to b: a == b
Operator Precedence Level Operator 2 ++ -- () []. 3 ++ --! + - & 5 * / % 6 + - 7 << >> 8 < > <= >= 9 ==!= 13 && 14 15 = = = %= += =
conditions-2.cpp Ask the user for an integer between 1 and 9. Say if that number is small (1, 2, 3), medium (4, 5, 6), large (7, 8, 9) or invalid (not between 1 and 9). Give me an integer between 1 and 9: 7 You typed a large number. Give me an integer between 1 and 9: 183 You typed an invalid number.
triangles-0.cpp Write a program that asks the user for (three) side lengths of a triangle and then prints if the triangle is valid or invalid. valid What is the length of side a? 3 What is the length of side b? 2 What is the length of side c? 10 Invalid invalid
triangles-1.cpp Write a program that asks the user for (three) side lengths of a triangle and then prints the type of the triangle (equilateral, isosceles, scalene, invalid). equilateral isosceles scalene invalid
Style
eecs183.org/docs/style
Indentation Indent each level of code by four spaces (usually can press Tab) int main() { int n; cin >> n; if (n > 0) { cout << "Thanks for the positive int!" << endl; } }
Silicon Valley http://www.hbo.com/silicon-valley
Lab 2 eecs183.org/labs/2 Exam question Implement getstudentfullname Implement main Enter student's name: Amy Enter the number of credits for Amy: 32 Amy is a sophomore Testing Implement test_isgregoriandate Implement test_isleapyear Submit lab2.cpp
Project 2 eecs183.org/projects/2 Implement Birthdays Implement functions Test functions Implement main Read date from user and print day of week Submit birthdays.cpp ******************************* Birthday Calculator ******************************* Menu Options ------------ 1) Determine day of birth 2) Print the next 10 leap years 3) Determine birthdays for the next 10 years 4) Finished Choice --> 1 Enter your date of birth format: month / day / year --> 1 / 27 / 2017 You were born on a: Friday Have a great birthday!!! Menu Options ------------ 1) Determine day of birth 2) Print the next 10 leap years 3) Determine birthdays for the next 10 years 4) Finished Choice --> 4 **************************************************** Thanks for using the Birthday Calculator ****************************************************