Call by reference is an important technique in C++ programming era. This technique has various forms such as,
- Passing by value,
- Passing by Independent Reference,
- Passing by Return reference,
For all the above methods, we have a sample C++ program. Feel free to check it out below. All free source codes are there
Passing Value using Call by reference in C++:
C++ program on “Passing Value using Call by reference in C++“. It will perform a reference operation for the given variable and passing the values. This CPP program can be used for all kind of students for their lab exams and related tests. It may be useful for their exam purpose.
[SEE: IT Related Abroad Studies] & [Bike Back Wordings]
/* Title: Swapping in Call by reference Author: Karthikh Venkat © http://students3k.com This website provides lots of useful information for all students. */ //Header Files. #include<iostream.h> #include<conio.h> void neg(int *k); int main() { clrscr(); int a; a = 20; cout << x << "Negated is "; neg(&a); cout << x << "\n"; getch(); return 0; } void neg(int *k) { *k = -*k; }
[Read: Aptitude Practice Area -> Easy methods]
Independent Reference Program in Call by reference:
// Header files #include<iostream.h> #include<conio.h> int main() { clrscr(); int x; //Variable int &ref = x; //Independent reference x = 20; cout << x << " " << ref << "\n"; ref = 100; cout << x << " " << ref << "\n"; int y = 19; ref = y; // This puts y's value into x cout << x << " " << ref << "\n"; ref--; // this decrements a // it doesn't affect what ref variable refers to cout << x << " " << ref << "\n"; getch(); return 0; }
[Useful: IT Company Details] & [Education Loan Guide]
CALL BY REFRENCE USING RETURN REFERENCE PROGRAM:
// Header files #include<iostream.h> #include<conio.h> char &replace(int k); // Return a reference char a[80] = "Hi Everyone"; int main() { clrscr(); replace(5) = 'Y'; // Assign Y to space after Hi cout << a; getch(); return 0; } char &replace(int k) { return s[k]; }