Call By Value |
Call By Reference |
In this, the function is called by passing value of variable as argument. |
In this, the function is called by passing address of variable as argument. |
Arguments passed by value can be variables, literals or expressions. |
Arguments passed by reference can only be variables. |
In this, copy of actual arguments is created and passed to function. |
In this, no copy of actual arguments is created. |
Actual and formal arguments are created in different memory locations. |
Actual and formal arguments are created in same memory locations. |
Any changes made to the formal arguments will have no effect on actual arguments. |
Any changes made to the formal arguments will have immediate effect on actual arguments. |
No pointers are used in this approach. |
The pointers are used in this approach. |
It is a slow way of calling functions. |
It is a fast way of calling functions. |
Program:
#include<iostream>
using namespace std;
int swap(int, int);
int main()
{
int a=4, b=10;
cout<<"Before Swaping:";
cout<<"\nValue of a="<<a<<" &
b="<<b;
swap(a,b);
return 0;
}
int swap(int x, int y)
{
int temp;
temp=x;
x=y;
y=temp;
cout<<"\nAfter Swaping";
cout<<"\nValue Of a="<<x<<" &
b="<<y;
}
|
Program:
#include<iostream>
using namespace std;
int swap(int *, int *);
int main()
{
int a=4, b=10;
cout<<"Before Swaping:";
cout<<"\nValue of a="<<a<<" &
b="<<b;
swap(&a,&b);
return 0;
}
int swap(int *x, int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
cout<<"\nAfter Swaping";
cout<<"\nValue Of a="<<*x<<" &
b="<<*y;
}
|
No comments:
Post a Comment