LECTURE 11 TYPES OF USER DEFINE FUNCTIONS ITC-414.

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
C++ for Engineers and Scientists Third Edition
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
D.L. Patel Institute of management & Technology (M.C.A. College),”Vidyanagri” Name: Bhatt Nishant D. Subject: OOP (object oriented programming language)
C Programming Lecture 8-2 : Function (advanced). Recursive Function (recursion) A function that calls itself (in its definition) Classic example : factorial.
C++ Function 1. Function allow to structure programs in segment of code to perform individual tasks. In C++, a function is a group of statements that.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
GE 211 Function in C++ Dr. Ahmed Telba Math Library Functions Defining a Function: The general form of a C++ function definition is as follows:
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
CPS120: Introduction to Computer Science Functions.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
CPS120: Introduction to Computer Science Lecture 14 Functions.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
Chapter 6.  Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
1 Remember: Examination is a chance not ability. By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Function Overloading and References
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Chapter 8 Functions in Depth. Chapter 8 A programmer-defined function is a block of statements, or a subprogram, that is written to perform a specific.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
1 CS 101 Fall 2001 Lecture 3. 2 Boolean expressions The expressions that are allowed to be in the parentheses following an if can be of the form x>y,
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Functions BICSE-6A Mr. Naeem Khalid Lecturer, Dept. of Computing.
Exception Handling How to handle the runtime errors.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
C++ CODES PART#04 1 COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT.
User Defined Functions
User-Written Functions
Functions and an Introduction to Recursion
School of EECS, Peking University
CSC113: Computer Programming (Theory = 03, Lab = 01)
CS1061 C Prgramming Lecture 11: Functions
CSC113: Computer Programming (Theory = 03, Lab = 01)
User-Defined Functions
Chapter 7: User-Defined Functions II
Chapter 6: User-Defined Functions I
Functions and an Introduction to Recursion
CS 101 First Exam Review.
Functions Imran Rashid CTO at ManiWeber Technologies.
CS1201: Programming Language 2
Functions Chapter No. 5.
Programming Fundamental-1
Presentation transcript:

LECTURE 11 TYPES OF USER DEFINE FUNCTIONS ITC-414

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

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

#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

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

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

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

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

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

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