FUNCTIONS IN C++

Need of Function

  • To simplify a program 
  • Reduce the length of program
  • Time of execution is reduce
  • Less memory required


Function Block

  1.  Function Declaration
  2.  Function Definition
  3. Function Body

Function Structure
                        datatype  functionname();        //function  declaration
                        datatype functionname()             //function defination
                             {
                                  ..........                                          //function body
                                  ..........
                              }


1.Default Argument Function
In this function argument value is provided during declaration

Let's take example
CODE:
#include<iostream>
using namespace std;
inline int swap(int &a,int &b)
{
    int temp;
    temp = a;
    a = b;
    b = temp;
}
int main()
{
    cout << "Write a program that illustrates the use of default arguments.\n\n\n";
    int x,y;
    cout << "Enter the value of x:";
    cin >> x;
    cout << "Enter the value of y:";
    cin >> y;
    swap (x,y);
    cout << "x=" << x << endl;
    cout << "y=" << y << endl;
}
OUTPUT:

0 Comments