C++ Passing Argument To Function

  • Home
  • C++
  • C++ Passing Argument To Function

Arguments can be passed in 3 ways:

Call By Value


It is Passing arguments by value.

Program to show call by value.




#include<iostream.h>
#include<conio.h>
void show(int a, int b)
{
   cout<<"\nValue of x ="<<a;
   cout<<"\nValue of y ="<<b; 
}
void main()
{
     clrscr();
     int x,y;
     cout<<"Enter two value = ";
     cin>>x>>y;
     show(x,y);
     getch();
}


Output


Enter two value = 10
7
Value of x=10
Vaue of y = 7

Call By Reference


It is Passing arguments by reference.

Program to show call by reference.




#include<iostream.h>
#include<conio.h>
void show(int &a, int &b)
{
   cout<<"\nValue of x ="<<a;
   cout<<"\nValue of y ="<<b; 
}
void main()
{
     clrscr();
     int x,y;
     cout<<"Enter two value = ";
     cin>>x>>y;
     show(x,y);
     getch();
}


Output


Enter two value = 10
7
Value of x=10
Vaue of y = 7

Call By Pointer


It is Passing arguments by Pointer.

Program to show call by Pointer.




#include<iostream.h>
#include<conio.h>
void show(int *a, int *b)
{
   cout<<"\nValue of x ="<<*a;
   cout<<"\nValue of y ="<<*b; 
}
void main()
{
     clrscr();
     int x,y;
     cout<<"Enter two value = ";
     cin>>x>>y;
     show(&x,&y);
     getch();
}


Output


Enter two value = 10
7
Value of x=10
Vaue of y = 7

Back To Top