Dear students, observe output of the program below.
If you still not understand it, feel free to contact me.
//Program menunjukkan fungsi call by reference vs call by value
#include<iostream.h>
void ref(int&, int&);//call by reference
void val(int,int);// call by value
void main()
{
int x=5,y=7;
int q=6, r= 12;
cout<<"Value of x and y in main function"<<endl;
cout<<"Value of x="<<x<<endl<<"Value of y="<<y<<endl<<endl;
ref(x,y);//memanggil fungsi ref
cout<<"Value of x and y in main function after ref function called"<<endl;
//Lihat perubahan pada nilai x dan y
cout<<"Value of x="<<x<<endl<<"Value of y="<<y<<endl<<endl;
cout<<"Value of q and r in main function"<<endl;
cout<<"Value of q="<<q<<endl<<"Value of r="<<r<<endl<<endl;
val(q,r);//memanggil fungsi val
cout<<"Value of q and r in main function after val function called"<<endl;
//Lihat adakah nilai pada q dan r berubah?
cout<<"Value of q="<<q<<endl<<"Value of r="<<r<<endl<<endl;
}
//fungsi yang menggunakan konsep call by reference
void ref(int &a, int &c)
{
a=8;
c=0;
cout<<"value of x and y in the val function"<<endl;
cout<<"Value of x="<<a<<endl<<"Value of y="<<c<<endl<<endl;
}
//fungsi menunjukkan konsep call by value
void val(int d, int e)
{
d=d*10;
e=e*2;
cout<<"value of q and r in the val function"<<endl;
cout<<"Value of q="<<d<<endl<<"Value of r="<<e<<endl<<endl;
}
Tiada ulasan:
Catat Ulasan