Presentation is loading. Please wait.

Presentation is loading. Please wait.

LECTURE 11 TYPES OF USER DEFINE FUNCTIONS ITC-414.

Similar presentations


Presentation on theme: "LECTURE 11 TYPES OF USER DEFINE FUNCTIONS ITC-414."— Presentation transcript:

1 LECTURE 11 TYPES OF USER DEFINE FUNCTIONS ITC-414

2 TYPES OF USER DEFINE FUNCTIONS Three types of user define functions 1. Overloaded Functions 2. Inline Functions 3. Recursive Functions Lecture 11: Preparaed By Lecturer Shumaila Sheikh www.itcafe.741.com

3 Overloaded Functions In C++ two different functions can have the same name if their parameter types or number are different. That means that you can give the same name to more than one function if they have either a different number of parameters or different types in their parameters. These functions are called overloaded functions. Lecture 11: Preparaed By Lecturer Shumaila Sheikh www.itcafe.741.com

4 #include int operate (int a, int b) { return (a*b); } float operate (float a, float b) { return (a/b); } void main () { int x=5,y=2; float n=5.0,m=2.0; cout << operate (x,y); cout << "\n"; cout << operate (n,m); cout << "\n"; getch(); } In this case we have defined two functions with the same name, operate, but one of them accepts two parameters of type int and the other one accepts them of type float. The compiler knows which one to call in each case by examining the types passed as arguments when the function is called. If it is called with two int as its arguments it calls to the function that has two int parameters in its prototype and if it is called with two floats it will call to the one which has two float parameters in its prototype. So the behavior of a call to operate depends on the type of the arguments passed because the function has been overloaded. Lecture 11: Preparaed By Lecturer Shumaila Sheikh www.itcafe.741.com

5 void greater(int,int,int); void greater(float,float,float); void main() { int a,b,c; float x,y,z; cout<<"Enter first value "; cin>>a; cout<<"Enter second value "; cin>>b; cout<<"Enter third value "; cin>>c; greater(a,b,c); cout<<endl<<endl; cout<<"Enter first real value "; cin>>x; cout<<"Enter second real value "; cin>>y; cout<<"Enter third real value "; cin>>z; greater(x,y,z); getch(); } void greater(int a1, int b1, int c1) { if(a1>b1 && a1>c1) cout<<a1<<" is greater than "<<b1<<" and "<<c1; else if(b1>a1 && b1>c1) cout<<b1<<" is greater than "<<a1<<" and "<<c1; else cout<<c1<<" is greater than "<<a1<<" and "<<b1; } void greater(float x1, float y1, float z1) { if(x1>y1 && x1>z1) cout<<x1<<" is greater than "<<y1<<" and "<<z1; else if(y1>x1 && y1>z1) cout<<y1<<" is greater than "<<x1<<" and "<<z1; else cout<<z1<<" is greater than "<<x1<<" and "<<y1; } Lecture 11: Preparaed By Lecturer Shumaila Sheikh www.itcafe.741.com

6 Inline Functions Inline functions are functions where the call is made to inline functions. The actual code then gets placed in the calling program. This does not change the behavior of a function itself, but is used to suggest to the compiler that the code generated by the function body is inserted at each point the function is called, instead of being inserted only once and perform a regular call to it. Reason for the need of Inline Function: Normally, a function call transfers the control from the calling program to the function and after the execution of the program returns the control back to the calling program after the function call. These concepts of function saved program space and memory space are used because the function is stored only in one place and is only executed when it is called. This concept of function execution may be time consuming but it is valid and good for larger programs. If the function is short, the programmer may wish to place the code of the function in the calling program in order for it to be executed. This type of function is best handled by the inline function. In this situation, the programmer may be wondering “why not write the short code repeatedly inside the program wherever needed instead of going for inline function?” Lecture 11: Preparaed By Lecturer Shumaila Sheikh www.itcafe.741.com

7 What happens when an inline function is written? The inline function takes the format as a normal function but when it is compiled it is compiled as inline code. The function is placed separately as inline function, thus adding readability to the source program. When the program is compiled, the code present in function body is replaced in the place of function call. General Format of inline Function: The general format of inline function is as follows: inline datatype function_name(arguments) ; Lecture 11: Preparaed By Lecturer Shumaila Sheikh www.itcafe.741.com

8 Example of inline function #include inline int greater(int,int); void main() { int a,b; clrscr(); cout<<"Enter first value "; cin>>a; cout<<"Enter second value "; cin>>b; cout<<greater(a,b)<<" is greater"; getch(); } int greater(int x, int y) { if(x<y) return y; else return x; } Lecture 11: Preparaed By Lecturer Shumaila Sheikh www.itcafe.741.com

9 Recursive Function A function that called itself is called recursive function. recursion is when a function calls itself. That is, in the course of the function definition there is a call to that very same function. At first this may seem like a never ending loop. So to end the function calling, we must give a certain condition in function. If that condition is true, the function returns or exit else it continuously call itself. void myFunction( int counter) { if(counter == 0) return; else { cout <<counter<<endl; myFunction(--counter); return; } } Lecture 11: Preparaed By Lecturer Shumaila Sheikh www.itcafe.741.com

10 Example of recursive function #include long factorial (long a) { if (a > 1) return (a * factorial (a-1)); else return (1); } int main () { long number; cout << "Please type a number: "; cin >> number; cout << number << "! = " << factorial (number); return 0; } Lecture 11: Preparaed By Lecturer Shumaila Sheikh www.itcafe.741.com


Download ppt "LECTURE 11 TYPES OF USER DEFINE FUNCTIONS ITC-414."

Similar presentations


Ads by Google