0
Upcoming SlideShare
×

Functions

414

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
414
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
59
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Functions"

1. 1. Functions FUNCTION OVERLOADING 2. PASSING DEFAULT ARGUMENTS 3. INLINE FUNCTION 1. Compiled By: Kamal Acharya
2. 2. Function Overloading  When the function with the same name perform the multiple task it is called as the function overloading.  Example: If the function add() can add not only the integers but also the floats and strings it is overloaded.  The functions differ in one of the following things    Number of parameters Data types of parameters Order of appearance Compiled By: Kamal Acharya
3. 3. Number Of Arguments #include<iostream.h> #include<conio.h> void print(char); void print(char, int); int main() { clrscr(); print('X'); print('Z',12); getch(); return 0; } Compiled By: Kamal Acharya void print(char a) { cout<<"In the Single argument function"<<endl; cout<<a<<endl; } void print(char a, int b) { int i; cout<<"In the double argument function"<<endl; for(i=0;i<b;i++) { cout<<a<<"t"; } }
4. 4. Data Types of Parameters #include<iostream.h> #include<conio.h> void add(int , int); void add(float, float); int main() { clrscr(); add(2,6); add(12.6,1.2); getch(); return 0; } Compiled By: Kamal Acharya void add(int a, int b) { cout<<"In the integer addition function"<<endl; cout<<a+b<<endl; } void add(float a, float b) { cout<<"In the float addition function"<<endl; cout<<a+b<<endl; }
5. 5. Oreder Of Appearance #include<iostream.h> #include<conio.h> void fun(int , char); void fun(char, int); int main() { clrscr(); fun(2,'A'); fun('B',1); getch(); return 0; } Compiled By: Kamal Acharya void fun(int a, char b) { cout<<"In the function with first argument integer and the second character"<<endl; cout<<"The first argument is"<< a<<".The second argument is "<< b<<endl; } void fun(char a, int b) { cout<<"In the function with first argument character and the second integer"<<endl; cout<<"The first argument is"<< a<<".The second argument is "<< b<<endl; }
6. 6. Passing Default Arguments • Allows programmer to define a default behavior  A value for a parameter can be implicitly passed  Reduces need for similar functions that differ only in the number of parameters accepted Compiled By: Kamal Acharya
7. 7.  Default parameters must appear after any mandatory parameters  Bad example void Trouble(int x = 5, double z, double y) { ... } Cannot come before mandatory parameters Compiled By: Kamal Acharya
8. 8. Consider void PrintChar(char c = '=', int n = 80) { for (int i = 0; i < n; ++i) cout << c; }  What happens in the following invocations? PrintChar('*', 20); PrintChar('-'); PrintChar(); Compiled By: Kamal Acharya
9. 9. Program to Illustrate Default Arguments #include<iostream.h> #include<conio.h> void print(int a= 10 , char b='B'); int main() { clrscr(); print(); print(2); print(1,'Z'); getch(); return 0; } Compiled By: Kamal Acharya void print(int a, char b) { int i; for(i=0;i<a;i++) { cout<<b<<"t"; } }
10. 10. Inline Function  Reduce function call overhead—especially for small functions.  Qualifier inline before a function’s return type in the function definition  “Advises” the compiler to generate a copy of the function’s code in place (when appropriate) to avoid a function call. Compiled By: Kamal Acharya
11. 11.  Multiple copies of the function code are inserted in the program (often making the program larger).  The compiler can ignore the inline qualifier and typically does so for all but the smallest functions. Compiled By: Kamal Acharya
12. 12. Syntax of Inline Function inline returnType functionName(arguments); Example: inline int add(int a, int b); Compiled By: Kamal Acharya
13. 13. Program to Illustrate inline Function #include<iostream.h> #include<conio.h> inline float mul(float x, float y) { return(x*y); } inline double div(double p, double q) { return(p/q); } Compiled By: Kamal Acharya int main() { clrscr(); float a=12.12; float b=2.2; cout<<mul(a,b)<<"n"; cout<<div(a,b)<<"n"; getch(); return 0; }
1. A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.