Presentation is loading. Please wait.

Presentation is loading. Please wait.

Function Part II: Some ‘advanced’ concepts on functions.

Similar presentations


Presentation on theme: "Function Part II: Some ‘advanced’ concepts on functions."— Presentation transcript:

1 Function Part II: Some ‘advanced’ concepts on functions

2 COMP104 Slide 2 Declaration vs. definition * A function is declared by writing down its interface (prototype) n A declaration can appear many times (even locally) n Compiler does not generate the codes  It only checks types and number of arguments * A function is defined by writing down its header and body n A definition is done only once n Compiler generates the codes * Before a name (identifier) can be used, it has to be declared: tell the compiler the type: int a; double b; * Most declarations are also definitions: allocation of memory.

3 COMP104 Slide 3 * A function must be declared before it can be used * All functions (like main()) ‘see’ each other n Don’t ‘nest’ functions! (you cannot unlike Pascal) n All functions are at the same level, global!

4 COMP104 Slide 4 Function Prototype * The function prototype declares the interface, or input and output parameters of the function, leaving the implementation for the function definition. (It therefore separates the concept from the implementation!) * This leads to ‘separate compilation’ as well!

5 COMP104 Slide 5 #include using namespace std; // Define a function to take absolute value of an integer int absolute(int fx) { int abs; if (fx >= 0) abs=fx; else abs= -fx; return (abs); } int main(){ int x, y, diff; cout << "Enter two integers (separated by a blank): "; cin >> x >> y; diff = x-y; diff = absolute(diff); cout << "The absolute difference between " << x << " and " << y << " is: " << diff << endl; return 0; } #include using namespace std; int absolute (int); // function prototype for absolute() int main(){ int x, y, diff; cout << "Enter two integers (separated by a blank): "; cin >> x >> y; diff = x-y; diff = absolute(diff); cout << "The absolute difference between " << x << " and " << y << " is: " << diff << endl; return 0; } // Define a function to take absolute value of an integer int absolute(int fx) { int abs; if (fx >= 0) abs=fx; else abs= -fx; return (abs); } Equivalent!

6 COMP104 Slide 6 Prototype vs signature * The signature of a function is the list of formal parameters without the name * The name and the signature (without return type) * The prototype is the full ‘ID’ of a function (with the return type)

7 COMP104 Slide 7 Function overloading (an OOP concept) * Functions may have the SAME name, but different signatures! * The ‘conflict’ is AUTOMATICALLY resolved by the difference of the signature! n Exact matches between parameter types n No exact match: pre-defined rules (type conversion by first widening, then narrowing) We already do it with the operator or function ‘+’!

8 COMP104 Slide 8 Good examples int max(int x, int y) { return (x>y) ? x:y;} int max(int x, int y, int z) { return max(max(x,y),z);} double max(double a, double b) { return (a>b) ? a:b;} // return type is not part of the signature // as long as the signature is different, // it’s valid ‘over-loading’ void swap(int& a, int& b) {int temp=a; a=b; b=temp;} void swap(float& a, float& b) {float temp=a; a=b; b=temp;} void swap(double& a, double& b) {double temp=a; a=b; b=temp;}

9 COMP104 Slide 9 Bad examples // bad design! Compiler does not know which to call int absolute(int a) {return (a<0) ? –a:a;} int absolute(int& a) {return(a=(a<0)?-a:a);} // wrong! It’s not a valid ‘over-loading’ // as the return type is not part of the signature! // We have two functions having the same signatures, no difference void swap(int& a, int& b) {int temp=a;a=b;b=temp;} int swap(int& a, int& b) {int temp=a;a=b;b=temp;return a;} int one(int x, double y) { return x;} double one(int x, double y) {return y;}

10 COMP104 Slide 10 Good design, but bad usage! int test(int a, double b); int test(double a, int b); test(3.2,4.6) is ambiguous! test(3,4) is ambiguous Compilation error! test(3,4.6)  the first test(3.0,4)  the second test(‘a’,4.6)  the first, ‘a’  int

11 COMP104 Slide 11 Default function argument * We can define ‘default’ parameters for more ‘smartness’ * Calling function can have fewer actual parameters (but we can not have fewer formal parameters

12 COMP104 Slide 12 Example int increment(int x, int step = 1) { return (x+step); } Int main() { cout << increment(10) << endl; cout << increment(10,5) << endl; return 0; }

13 COMP104 Slide 13 * specified only once, either in declaration or definition, but usually in declaration * at the end of formal parameter list * not function overloading The default parameters

14 COMP104 Slide 14 Example int increment(int, int=1); // or // int increment(int x, int step=1); int main() { cout << increment(10) << endl; cout << increment(10,5) << endl; return 0; } int increment(int x, int step) { return (x+step); }

15 COMP104 Slide 15 void swap(int& a, int& b) {int temp=a;a=b;b=temp;} // sort_order is 1 for ascending, otherwise descending void sort(int& x, int& y, int order=1) { if (order == 1) { if (x>y) swap(x,y); } else { if(x<y) swap(x,y); } int main() { int a = 24, b=8; sort(a,b); cout << a << b << endl; sort(a,b,2); cout … return 0; }


Download ppt "Function Part II: Some ‘advanced’ concepts on functions."

Similar presentations


Ads by Google