#include #include using namespace std; //basic "void" function. //void means this doesn't give you anything, or return anything //This function just does something void someFunction() { cout << "Welcom to this cool function" << endl; for (int i = 1; i <= 10; i++) { cout << i << ", "; } cout << "Done!" << endl; } //This void function takes one integer n as input void printUpTo(int n) { for (int i = 1; i <= n; i++) { cout << i << ", "; } cout << "Whoo! done!" << endl; } //This void function takes in 2 parameters //This function prints numbers from //value 'start' to value 'end' void printFrom(int start, int end) { /* for (int i = start; i <= end; i++) { cout << i << ", "; } cout << endl; */ int i = start; while (i <= end) { cout << i << ", "; i++; } cout << endl; } //Parameter list my be arbitrily diverse, //including strings, ints, or whatever. void annoyingKid(string sentence, int numTimes) { for (int i = 0; i < numTimes; i++) { cout << sentence << endl; } } //this function returns an 'int' int add(int a, int b) { int total = a + b; return total; } //Compute the sum of integers from a to b. int sumTotal(int a, int b) { int total = 0; for (int i = a; i <= b; i++) { total = total + i; } return total; } //Type 'bool' is short for boolean. //boolean variables have value either 'true' or 'false'. bool greater(double x, double y) { if (x > y) return true; else return false; } int main() { //void function, no parameters someFunction(); someFunction(); someFunction(); //void function, but with a parameter printUpTo(5); printUpTo(10); printUpTo(20); //another void function, but with 2 parameters printFrom(5, 15); //You may pass variables as well int x = 3; int y = 7; printFrom(x, y); //May pass multiple parameters of different types string phrase = "Are we there yet?"; int numReps = 5; annoyingKid(phrase, numReps); annoyingKid("Mom lets us do that!", 3); //Now a function that returns a value cout << add(5, 3) << endl; // 8 int num1 = 13; int num2 = 20; cout << add(num1, num2) << endl; //33 int sum = add(14, 35); cout << sum << endl; // 49 //Challenge: cout << sumTotal(10, 35) << endl; //10+11+12+...+35 cout << sumTotal(5, 12) << endl; cout << sumTotal(72, 93) << endl; //Implement a function 'greater' that returns true if //first parameter is great than second, false if not. double number1, number2; cout << "enter 2 numbers" << endl; cin >> number1; cin >> number2; if (greater(number1, number2)) { cout << number1 << " is greater " << endl; } else { cout << number1 << " is not greater" << endl; } //Next up... read about pass by reference! return 0; }