// ============================================ // Functions (p. 5) #include using namespace std; int add (int, int); int main () { int c = add(10, 20); cout << c << endl; return 0; } int add (int num1, int num2) { return num1 + num2; } // ============================================ // Functions (p. 11) int add (int, int); void test (int); int main () { int c = add(10, 20); cout << c << endl; return 0; } int add (int num1, int num2) { test (num1); return num1 + num2; } void test (int toPrint) { cout << toPrint << endl; } // ============================================ // Functions (p. 12) int add (int num1, int num2) { return num1 + num2; } int main () { // fine! int c = add(10, 20); cout << c << endl; return 0; } // ============================================ // Functions (p. 12) void a() { // error! b(); } void b() { ; } int main () { a(); b(); return 0; } // ============================================ // Functions (p. 13) void a() { // error! b(); } void b() { a(); } int main () { a(); b(); return 0; } // ============================================ // Functions (p. 13) void a(); void b(); int main () { a(); b(); return 0; } void a() { // fine! b(); } void b() { a(); } // ============================================ // Functions (p. 14) int add (int num1, int num2) { return num1 + num2; } int main () { double q1 = 10.5; double q2 = 20.7; double c = add(q1, q2); // ! cout << c << endl; return 0; } // ============================================ // Functions (p. 15) int add (int, int); int main () { const int C = 5; double d = 1.6; cout << add(10, 20) << endl; cout << add(C, d) << endl; // ! cout << add(10 * C, 20) << endl; return 0; } int add (int num1, int num2) { return num1 + num2; } // ============================================ // Functions (p. 17) int test (int); int main() { cout << test(-1); return 0; } int test (int a) { if (a > 0) return 5; } // ============================================ // Functions (p. 18) int factorial (int n) { int ans = 1; for (int a = 1; a <= n; a++) ans *= a; // ans = ans * a; return ans; } // ============================================ // Functions (p. 18) void factorial (int n) { int ans = 1; for (int a = 1; a <= n; a++) ans *= a; // ans = ans * a; cout << ans; } // ============================================ // Functions (p. 22) int main() { int i = 50; // it will be hidden for(int i = 0; i < 20; i++) { cout << i << " "; // print 0 1 2 ¡K 19 } cout << i << endl; // 50 return 0; } // ============================================ // Functions (p. 23) #include using namespace std; int i = 5; int main() { for(; i < 20; i++) cout << i << " "; // ? cout << endl; int i = 2; cout << i << endl; // ? cout << ::i << endl; // ? return 0; } // ============================================ // Functions (p. 26) int test(); int main() { for (int a = 0; a < 10; a++) cout << test() << " "; return 0; // 1, 1, ..., 1 } int test() { int a = 0; a++; return a; } // ============================================ // Functions (p. 26) int test(); int main() { for (int a = 0; a < 10; a++) cout << test() << " "; return 0; // 1, 2, ..., 10 } int test() { static int a = 0; a++; return a; } // ============================================ // Functions (p. 29) void swap (int x, int y); int main() { int a = 10, b = 20; cout << a << " " << b << endl; swap(a, b); cout << a << " " << b << endl; } void swap (int x, int y) { int temp = x; x = y; y = temp; } // ============================================ // Functions (p. 32) void shiftArray (int [], int); int main() { int num[5] = {1, 2, 3, 4, 5}; shiftArray(num, 5); for (int i = 0; i < 5; i++) cout << num[i] << " "; return 0; } void shiftArray (int a[], int len) { int temp = a[0]; for (int i = 0; i < len - 1; i++) a[i] = a[i + 1]; a[len - 1] = temp; } // ============================================ // Functions (p. 33) void printArray (int [], int); int main() { int num[5] = {1, 2, 3, 4, 5}; printArray(num, 5); return 0; } void printArray (int a[], int len) { for (int i = 0; i < len; i++) cout << a[i] << " "; cout << endl; } // ============================================ // Functions (p. 34) void printArray (int [5], int); int main() { int num[5] = {1, 2, 3, 4, 5}; printArray(num, 5); return 0; } void printArray (int a[5], int len) { for (int i = 0; i < len; i++) cout << a[i] << " "; cout << endl; } // ============================================ // Functions (p. 35) void printArray (int [][2], int); int main() { int num[5][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; printArray(num, 5); return 0; } void printArray (int a[][2], int len) { for (int i = 0; i < len; i++) { for (int j = 0; j < 2; j++) cout << a[i][j] << " "; cout << endl; } } // ============================================ // Functions (p. 38) void printArray (const int [5], int); int main() { int num[5] = {1, 2, 3, 4, 5}; printArray(num, 5); return 0; } void printArray (const int a[5], int len) { for (int i = 0; i < len; i++) cout << a[i] << " "; cout << endl; }