Functions Review void fun() { cout << "A"; } void fin() { cout << "B"; fun(); cout << "C"; fun(); } int main() { fin(); fun(); What is the output? ___________________ } In the previous code, how many function calls (ignore cout!) _____ how many function definitions? ______ Order of definitions - Call must follow the definition! void fun() { } void fin() { fun(); OK? ____ fan(); OK? ____ } void fan() { } Prototype = first line of a function (return type, name, param's) int fun(int x, double y) { if (y > 0) return x; else return -x; } Prototype is: ________________________________________ Alternative approach to function definition order: void fun(); // start with prototype of each function void fin(); void fan(); void fun() { // then give function bodies in any order } void fin() { fun(); OK? _____ fan(); OK? _____ } void fan() { } More simply, skip prototypes and put main at the bottom. In a call, parameters must match number, type, order. (except conversions between int double char bool) void fun(int i, double d, bool b, char c) { cout << i << " " << d << " " << b << " " << c << endl; } int main() { fun(true, 45, 'H', 72.13); // works! } Not everything converts! fun(int arr[MAX], double x, double y); int main() { int n; double f1, f2; int nums[MAX]; fun(f1,nums,f2); OK? _____ fun(nums,f1,f2); OK? _____ fun(f1,f2); OK? _____ } Parameters - reference vs. value value - just a COPY of caller's data reference - sharing the caller's ORIGINAL data void fun(int a) { a is: value or reference a = 2 * a; } int main() { int m = 100; fun(m); cout << m; output? ____________ } ---------------------------------------------------------- void fun(int a) { a is: value or reference a = 2 * a; } int main() { int m[MAX]; m[0] = 100; fun(m[0]); cout << m[0]; output? _____________ } --------------------------------------------------------- Most parameters are value (int double char bool) BUT Arrays are always reference parameters. (In C/C++, array parameters are NOT copied.) void fun(int a[MAX]) { a[0] = 2 * a[0]; } int main() { int m[MAX]; m[0] = 100; fun(m); output? ______________ cout << m[0]; } -------------------------------------------------------- Sending arrays (or array elements) as parameters void fun(int m) { .... int main() { int arr[MAX]; fun(arr); good call? _____ fun(arr[0]); good call? _____ ----------------------------------- void fun(int m[MAX]) { .... int main() { int arr[MAX]; fun(arr); good call? ______ fun(arr[0]); good call? ______ --------------------------------------------- Declarations in the call or in the definition. void fun(int a, int b) { .... } int main() { int x = 5; int y = 6; fun(int x,int y); Problems? ______________________________ }