Call by value means passing the value directly to a function. The called function uses the value in a local variable; any changes to it DO NOT affect the source variable. In call by value method, the called function creates its own copies of original values sent to it. Any changes, that are made, occur on the function’s copy of values and are not reflected back to the calling function.
2. • Call by value means passing the value directly to a
function. The called function uses the value in a
local variable; any changes to it DO NOT affect the
source variable.
• In call by value method, the called function creates
its own copies of original values sent to it. Any
changes, that are made, occur on the function’s
copy of values and are not reflected back to the
calling function.
3. • Simple Example:-
• #include <iostream>
• #include <conio.h>
• void swap(int x, int y)
• {
• int temp;
• temp = x;
• x = y;
• y = temp;
• cout<<"nn Swap Valuenn";
• cout<<"A="<<x<<endl; cout<<"B="<<y<<endl;
• }
• int main()
• {
• int a = 7, b = 4;
• swap(a, b);
• cout<<"n******fastest********nn";
• cout<<"Original values are a="<<a<<endl;
• cout<<"Original values are b="<<b<<endl;
• getch();
• }
•
4. • Using Class Example:-
• #include <iostream>
• #include <conio.h>
• class fastest {
• public:
• void show() {
• cout<<"Welcome to Fastlearning.in n";
• }
• };
• void print(fastest c) {
• c.show();
• }
• int main(void) {
• fastest b;
• print(b);
• getch();
• }
5. • Using friend function Example:-
• #include <iostream.h>
• #include <conio.h>
• class test;
• class fast {
• int a,b;
• public:
• void get1();
• friend void sum(fast,test);
• };
• class test {
• int c,d;
• public:
• void get2();
• friend void sum(fast,test);
• };
• void fast::get1() {
• cout<<"Enter value for a: ";
• cin>>a;
• cout<<"Enter value for b: ";
• cin>>b;
• }
6. • void test::get2() {
• cout<<"Enter value for c: ";
• cin>>c; cout<<"Enter value for d: ";
• cin>>d;
• }
• void sum(fast a1,test a2) { cout<<"A+C="<<a1.a+a2.c<<endl;
cout<<"B+D="<<a1.b+a2.d<<endl;
• }
• main() {
• fast a;
• test b;
• a.get1();
• b.get2();
• sum(a,b);
• getch();
• }
8. • The call by reference method of passing
arguments to a function copies the
address of an argument into the formal
parameter. Inside the function, the
address is used to access the actual
argument used in the call. This means
that changes made to the parameter
affect the passed argument.
9. • Simple Program:-
• #include <iostream.h>
• #include <conio.h>
• void swap( int &a, int &b ) {
• int tmp;
• tmp = a;
• a = b;
• b = tmp;
• return;
• }
10. • int main( )
{
• int x ,y;
• cout<<"Enter Value of A="; cin>>x;
• cout<<endl; cout<<"Enter Value of B="; cin>>y; cout<<endl;
swap( x, y );
• cout << "x: " << x << endl << "y: " << y << endl;
• getch();
• }