Thursday 4 September 2014

FUNCTIONS

FUNCTIONS

Functions are the self-defining blocks of code and they are used for repeated task or code.Every function on its execution return a value. Functions play an important role in c++ program development. Functions to give structured programming. Another advantage of using functions is that
it is reduced the size of a program by calling and using them at different places in the program.
 
 
How to declare a function
 
returntype functionname (no. of arguments or parameters)
{
 
     statement;
 
}
 
Syntax of Function
 
void show();                       // Function prototype
 
void main()
{
      ........
 
     show();                         // calling
       ........
 
}
 
void show()                       //Function definition
{
    ...............
    ...............
    ...............
 
}
 
INLINE FUNCTION
A Function declared with the help of inline keyword is known as Inline Function. Whenever a function is called lot of time wasted get wasted in the passing of the code to the function definition. so to reduce this problem with declared a function inline. By which the function call automatically get replaced by the definition this whole concept is based on inline declaration.
 
 
It works perfectly on simple function function but it has two disadvantages.
  1. It never works on the function which contains loops.
  2. It might not work on those function which has return type. 

FRIEND FUNCTION
 
A function is declared with the help of keyword friend is known as friend function .It acts as a bridge between two classes. If two classes wants to use same function it must be friend of both. Friend function can also access private data of the class.
 
Features of Friend function
  1. Friend function cannot be declared virtual.
  2. It is also defined outside the class.
  3. It takes class objects as arguments.
  4. It is never inside the class.
  5. We don't object to call this function.
VIRTUAL FUNCTION
A Function declared with the help of keyword virtual is termed as virtual function. It can be accessed only by the object of the class in which it has been declared.
 
PURE VIRTUAL FUNCTION
When a Virtual function is assigned 0 it becomes pure virtual function and the class which contains Pure Virtual function is termed as Abstract class. We cannot create object of the abstract class and abstract class always acts as the base class.

No comments:

Post a Comment