How much can you make coding

how to reuse code using generics and open source software development
Dr.ShaneMatts Profile Pic
Dr.ShaneMatts,United States,Teacher
Published Date:23-07-2017
Your Website URL(Optional)
Comment
6.096 Lecture 3: Functions How to reuse code Geza Kovacs include iostream using namespace std; int main() int threeExpFour = 1; for (int i = 0; i 4; i = i + 1) threeExpFour = threeExpFour 3; cout "34 is " threeExpFour endl; return 0; include iostream using namespace std; Copy-paste int main() coding int threeExpFour = 1; for (int i = 0; i 4; i = i + 1) threeExpFour = threeExpFour 3; cout "34 is " threeExpFour endl; int sixExpFive = 1; for (int i = 0; i 5; i = i + 1) sixExpFive = sixExpFive 6; cout "65 is " sixExpFive endl; return 0; include iostream using namespace std; Copy-paste int main() coding int threeExpFour = 1; for (int i = 0; i 4; i = i + 1) (bad) threeExpFour = threeExpFour 3; cout "34 is " threeExpFour endl; int sixExpFive = 1; for (int i = 0; i 5; i = i + 1) sixExpFive = sixExpFive 6; cout "65 is " sixExpFive endl; int twelveExpTen = 1; for (int i = 0; i 10; i = i + 1) twelveExpTen = twelveExpTen 12; cout "1210 is " twelveExpTen endl; return 0; include iostream With a using namespace std; function // some code which raises an arbitrary integer // to an arbitrary power int main() int threeExpFour = raiseToPower(3, 4); cout "34 is " threeExpFour endl; return 0; include iostream With a using namespace std; function // some code which raises an arbitrary integer // to an arbitrary power int main() int threeExpFour = raiseToPower(3, 4); cout "34 is " threeExpFour endl; int sixExpFive = raiseToPower(6, 5); cout "65 is " sixExpFive endl; return 0; include iostream With a using namespace std; function // some code which raises an arbitrary integer // to an arbitrary power int main() int threeExpFour = raiseToPower(3, 4); cout "34 is " threeExpFour endl; int sixExpFive = raiseToPower(6, 5); cout "65 is " sixExpFive endl; int twelveExpTen = raiseToPower(12, 10); cout "1210 is " twelveExpTen endl; return 0; Why define your own functions? • Readability: sqrt(5) is clearer than copy-pasting in an algorithm to compute the square root • Maintainability: To change the algorithm, just change the function (vs changing it everywhere you ever used it) • Code reuse: Lets other people use algorithms you’ve implemented Function Declaration Syntax Function name int raiseToPower(int base, int exponent) int result = 1; for (int i = 0; i exponent; i = i + 1) result = result base; return result; Function Declaration Syntax Return type int raiseToPower(int base, int exponent) int result = 1; for (int i = 0; i exponent; i = i + 1) result = result base; return result; Function Declaration Syntax Argument 1 int raiseToPower(int base, int exponent) int result = 1; for (int i = 0; i exponent; i = i + 1) result = result base; return result; • Argument order matters: – raiseToPower(2,3) is 23=8 – raiseToPower(3,2) is 32=9 Function Declaration Syntax Argument 2 int raiseToPower(int base, int exponent) int result = 1; for (int i = 0; i exponent; i = i + 1) result = result base; return result; • Argument order matters: – raiseToPower(2,3) is 23=8 – raiseToPower(3,2) is 32=9 Function Declaration Syntax int raiseToPower(int base, int exponent) signature int result = 1; for (int i = 0; i exponent; i = i + 1) result = result base; return result; Function Declaration Syntax int raiseToPower(int base, int exponent) int result = 1; for (int i = 0; i exponent; i = i + 1) result = result base; body return result; Function Declaration Syntax int raiseToPower(int base, int exponent) int result = 1; for (int i = 0; i exponent; i = i + 1) result = result base; return result; Return statement include iostream using namespace std; int raiseToPower(int base, int exponent) Function int result = 1; declaration for (int i = 0; i exponent; i = i + 1) result = result base; return result; Function invocation int main() int threeExpFour = raiseToPower(3, 4); cout "34 is " threeExpFour endl; return 0; Returning a value • Up to one value may be returned; it must be the same type as the return type. int foo() char foo() return "hello"; // error return "hello"; // ok Returning a value • Up to one value may be returned; it must be the same type as the return type. • If no values are returned, give the function a void return type void printNumber(int num) cout "number is " num endl; int main() printNumber(4); // number is 4 return 0; Returning a value • Up to one value may be returned; it must be the same type as the return type. • If no values are returned, give the function a void return type – Note that you cannot declare a variable of type void int main() void x; // ERROR return 0; Returning a value • Return statements don’t necessarily need to be at the end. • Function returns as soon as a return statement is executed. void printNumberIfEven(int num) if (num % 2 == 1) cout "odd number" endl; return; cout "even number; number is " num endl; int main() int x = 4; printNumberIfEven(x); // even number; number is 3 int y = 5; printNumberIfEven(y); // odd number