C++ Types User Defined

Types Of User Defined Function


Function without arguments and without return value


  • Function contains no argument and no return value
  • return type of function is void
  • There is simple passing of control from calling function to called function and vice versa but there is no passing of value between them.

Program to demonstrate function without argument and without return value.



#include<iostream.h>
#include<conio.h>
void sub()
{
     int a,b,c;
     cout<<"Enter two values = ";
     cin>>a>>b;
     c=a-b;
     cout<<"\nSub = "<<c; 
}
void main()
{
     clrscr();
     sub();
     getch();
}


Output


Enter two values = 10
7
Sub = 3

Function with arguments and without return value


  • Function contains with argument and no return value.
  • return type of function is void.
  • There is passing of values from calling function to user defined function.


Program to demonstrate use function with arguments and without return value.



#include<iostream.h>
#include<conio.h>
void sub(int a,int b)
{
    int c;
    c=a-b;
    cout<<"\n Sub = "<<c;
}
void main()
{
     clrscr();
     int x,y;
     cout<<"Enter two value = ";
     cin>>x>>y;
     sub(x,y);
     getch();
}


Output


Enter two values = 10
7
Sub = 3

Function with arguments and with return value


  • Function contains with argument and with return value.
  • return type of function is some data type like int, float, char, etc.
  • There is a two-way communication of values between calling function as well as called function.


Program to demonstrate use function with arguments and with return value.



#include<iostream.h>
#include<conio.h>
int sub(int a,int b)
{
    int c;
    c=a-b;
    return(c);
}
void main()
{
     clrscr();
     int x,y,z;
     cout<<"Enter two value = ";
     cin>>x>>y;
     z=sub(x,y);
     cout<<"\nSub = "<<z; 
     getch();
}


Output


Enter two values = 10
7
Sub = 3

Function without arguments and with return value


  • Function contains without argument and with return value.
  • return type of function is some data type like int, float, char, etc.
  • There is a one-way communication of values between called function and call function.


Program to demonstrate use function without arguments and with return value.



#include<iostream.h>
#include<conio.h>
int sub()
{
    int a,b,c;
    cout<<"Enter value of a =";
    cin>>a;
    cout<<"Enter value of b =";
    cin>>b;
    c=a-b;
    return(c);
}
void main()
{
     clrscr();
     z=sub();
     cout<<"\nSub = "<<z; 
     getch();
}


Output


Enter value of a = 10
Enter value of a = 7
Sub = 3

Back To Top