#include #include using namespace std; //Whenever this function is "called", //the code inbetween the curly brackets //is excecuted. void sayHello() { cout << "Hi 1370!" << endl; cout << "Is it fridya yet?" << endl; } //Can pass more than one parameter void greetPersonToClass(string name, string course) { cout << "Hello!" << endl; cout << "Welcome to " << course << " " << name << endl; } void greetPerson(string name) { cout << "Hi " << name << ", welcome to the class" << endl; } //What parameters? void annoyingKid(string phrase, int numTimes) { for (int i = 0; i < numTimes; i = i + 1) { cout << "annoying kid says: " << phrase << endl; } } double squared(double x) { double answer; answer = x * x; return answer; } //Return sum of num1 and num2 double add(double num1, double num2) { return num1 + num2; } //Print values from start up to end void printRange(int start, int end) { for (int i=start; i<=end; i++) { cout << i << ", " << endl; } cout << endl; } double takeHomePay(int dollars, double tax) { //How much tax is taken double taxDollars = dollars * tax; //subtract tax amount from dollars, return result double takehome = dollars - taxDollars; return takehome; } bool isGreater(double x, double y) { if (x > y) { return true; } else { return false; } } //compute and return x^n (x raised to nth power) //Run time: how many steps does this take with respect to n //Run time: O(n) double power(double x, int n) { double product = 1; //loop n times (however you want) for (int i = 0; i < n; i++) { product = product * x; } //Ok, we've computed answer in variable product return product; } //Pass by reference example void incrementByOne(int &number) { number = number + 1; } int main() { //Most basic of functions sayHello(); //Passing a parameter to a function greetPerson("Lisa"); greetPerson("Roger"); greetPerson("Isabel"); //Can pass multiple parameters greetPersonToClass("Lisa", "1370"); greetPersonToClass("Simon", "1101"); //Challenge #1: annoyingKid("Are we there yet?", 5); //print "are we there yet" 5 times annoyingKid("Any games on your phone?", 17); //Value returning functions int result; result = squared(5); cout << result << endl; cout << squared(8) << endl; //64 cout << 5 + squared(3) << endl; //14 squared(11); //What will happen? (you will see nothing) //Challenge #2: cout << add(3.14, 8.2) << endl; // 11.16 //Challenge #3: printRange(5, 12); //5 6 7 8 9 10 11 12 printRange(2, 5); //2 3 4 5 //Challenge #4: //Write function that takes as in put your money earned //before taxes, as well as the tax rate, //and returns the takehome pay amount. cout << "I get to take home " << takeHomePay(500, .10) << " dollars." << endl; //Challenge #5: if (isGreater(3.14, 8)) { cout << "Yep, 3.14 is bigger than 8" << endl; } else { cout << "Nope, 3.14 is NOT bigger than 8" << endl; } //Challenge #6: Ultimate challenge: cout << power(3, 4) << endl; //81 cout << power(5, 3) << endl; //125 //Challenge #7: Pass by reference! int x = 0; incrementByOne(x); cout << x << endl; // 1 incrementByOne(x); incrementByOne(x); cout << x << endl; // 3 return 0; }