2. FUNTION IN C++
• A Function is a group of statements that together perform a
specific task. Every C++ program has at least one function,
which is main().
• Why use function ?
• Function are used to divide a large code into a module.
• Due to this we can easily debug and maintain the code.
3. COMPONENTS OF FUNCTION
• A function usually has three components.
• They are:
• Function prototype/declaration
• Function definition
• Function call
4. 1-Function Prototype/Declaration
• Function declaration informs the compiler about the function’s name, type
and number of argument it receives and type of value it returns.
• Syntax for function declaration
• For example,
// function name = display, receives a character as argument and returns
nothing
void display(char);
// function name=sum, reveives two integers as argument and returns and
integer
int sum(int,int);
return_type function_name(parameter);
5. 2-FUNCTION DEFINITION
Defining of function is nothing but give body of function that
means write logic inside function body.
return_type function_name(parameter)
{
//function body;
}
6. 3-FUNCTION CALL
• Function call statement calls the function by
matching its name and arguments. A function call
can be made by using function name and
providing the required parameters.
• Syntax for function call
function_name();
or
variable=function_name(argument);
7. EXAMPLE OF FUNCTION
void sum(); // declaring a function
a=10,b=20,c;
void sum() // defining function
{
c=a+b;
cout<<“sum:”<<c;
}
int main()
{
sum(); // calling function
}
Sum: 30
8. TYPE OF FUNCTION
There are different type of function in C++
Language:-
• Library function or pre-define function
• User defined function.
• Overloading function.
• Inline function.
• Friend function.
• Static function.
• Constructor function.
• Destructor function.
• Virtual function.
9. LIBRARY FUNCTION
• Library function are those which are predefined in
C++ compiler.
• The implementation part of pre-defined functions is
available in library files that are .lib/.obj files. .lib or
.obj files are contained pre-compiled code.
• printf(), scanf(), clrscr(), pow() etc. are pre-defined
functions.
10. USER DEFINED FUNTION
• These functions are created by programmer according to
their requirement.
• For example:-suppose you want to create a function for add
two number then you create a function with name sum()
this type of function is called user defined function.
11. OVERLOADING FUNCTION
• Whenever same method name is exiting multiple times in
the same class with different number of parameter or
different order of parameters or different types of
parameters is known as function overloading.
“Doing work in different way is called overloading”.
Different way to overload the method:-
1. By Changing number of arguments or parameters
2. By Changing the data type
14. INLINE FUNCTION IN C++
• An inline function is a combination of macro
& function. At the time of declaration or
definition, function name is preceded by word
inline.
• Function calls involve execution-time
overhead.
• Inline functions to help reduce function call
overhead, especially for small functions.
15. Example of Inline function in C++
inline void show()
{
cout<<“Hello World”;
}
int main()
{
show(); // Call it like a normal function
}
Inline
function
Hello World
16. FRIEND FUNCTION IN C++
• A Friend function is a non-member function of the class that has
been granted access to all private members of the class.
• We simply declare the function within the class by a prefixing its
declaration with keyword friend.
• Function definition must not use keyword friend.
• Definition of friend function is specified outside the class body
and is not treated as a part of the class.
• The major difference b/w member function and friend function is
that the member function is accessed through the object while
friend function requires object to be passed as parameter.
17. FRIEND FUNCTION IN C++
Syntax:
class ABC
{
……………
public:
friend void xyz(object of class);
};
18. STATIC FUNTION IN C++
• We can define class members static using static keyword.
• When we declare a member of a class as static no matter
how many objects of the class are created.
• There is only one copy of the static member.
• A static member is shared by all objects of the class.
• All static data is initialized to zero when the first object is
created, if no other initialization is present.
• Static member function called by :: inplace of .
Operator.
20. VIRTUAL FUNTION IN C++
• A virtual function a member function which is declared
within base class and is re-defined (Overriden) by derived
class.
• They are mainly used to achieve Runtime polymorphism
• Functions are declared with a virtual keyword in base
class.
• The resolving of function call is done at Run-time.
21. EXAMPLE OF VIRTUAL FUNTION
Class A
{ public:
virtual void show(){
cout<<“Hello base classn”;}
};class B : public A {
public:
void show() {
cout<<“Hello derive class”;
}
};