Presentation is loading. Please wait.

Presentation is loading. Please wait.

More ‘concepts’ on Function

Similar presentations


Presentation on theme: "More ‘concepts’ on Function"— Presentation transcript:

1 More ‘concepts’ on Function

2 Declaration vs. definition
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. A function is declared by writing down its interface (prototype) A declaration can appear many times (even locally) 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 A definition is done only once Compiler generates the codes

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

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

5 Equivalent! #include <iostream> 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 <iostream> 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 Prototype vs signature
Prototype = return type + name + signature The signature of a function is the list of formal parameters, without the name and the return type The prototype is the full ‘ID’ of a function (with the return type)

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! Exact matches between parameter types No exact match: pre-defined rules (type conversion by first widening, then narrowing) We already do it with the operator or function ‘+’!

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 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 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 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 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 The default parameters
specified only once, either in declaration or definition, but usually in declaration at the end of formal parameter list not function overloading

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 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 "More ‘concepts’ on Function"

Similar presentations


Ads by Google