SlideShare a Scribd company logo
2
Most read
5
Most read
8
Most read
Call by value in
C++
• 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.
• 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();
• }
•
• 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();
• }
• 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;
• }
• 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();
• }
Call by reference
in
c++
• 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.
• Simple Program:-
• #include <iostream.h>
• #include <conio.h>
• void swap( int &a, int &b ) {
• int tmp;
• tmp = a;
• a = b;
• b = tmp;
• return;
• }
• 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();
• }
• Using Class Example:-
• #include <iostream.h>
• #include <conio.h>
• class fastest
• {
• public:
• void show() {
• cout<<"Hello! Friends ";
• }
• };
• void print(fastest &c) {
• c.show();
• }
• int main(void) {
• fastest b;
• print(b);
• getch();
• }
THANK YOU!
Learn more and Enjoy More

More Related Content

PPTX
Parameter passing to_functions_in_c
PPTX
Call by value
PPTX
Constructors and destructors
PPTX
C++ string
PPTX
Function C programming
PDF
Function overloading ppt
PPTX
Array in C
PPTX
Inline function
Parameter passing to_functions_in_c
Call by value
Constructors and destructors
C++ string
Function C programming
Function overloading ppt
Array in C
Inline function

What's hot (20)

PPTX
Scope rules : local and global variables
PPTX
Functions in c++
PPTX
PPTX
INLINE FUNCTION IN C++
PPTX
classes and objects in C++
PPTX
Functions in C
PPT
File handling in c
PPTX
Nested loops
PDF
C++ Files and Streams
PPT
RECURSION IN C
PPTX
07. Virtual Functions
PPTX
Functions in python slide share
PPT
friend function(c++)
PPTX
Header files of c++ unit 3 -topic 3
PPT
Function overloading(c++)
PPTX
Functions in C
PPTX
Exception handling c++
PPTX
Functions in c
PDF
Python tuple
PPT
Multidimensional array in C
Scope rules : local and global variables
Functions in c++
INLINE FUNCTION IN C++
classes and objects in C++
Functions in C
File handling in c
Nested loops
C++ Files and Streams
RECURSION IN C
07. Virtual Functions
Functions in python slide share
friend function(c++)
Header files of c++ unit 3 -topic 3
Function overloading(c++)
Functions in C
Exception handling c++
Functions in c
Python tuple
Multidimensional array in C
Ad

Similar to Call by value or call by reference in C++ (20)

PDF
Functions_C++ power point presentation s
PPTX
Classes function overloading
PPTX
FUNCTIONS, CLASSES AND OBJECTS.pptx
PPTX
OOP-Module-1-Section-4-LectureNo1-5.pptx
PPTX
Function Overloading Call by value and call by reference
PPTX
Presentation.pptx
PPTX
C++ FUNCTIONS-1.pptx
PPTX
2-Concept of Pointers in c programming.pptx
PPTX
Fundamental of programming Fundamental of programming
PPTX
Pointers in c++ programming presentation
PPTX
Pointers lesson 5 (double pointer, call by value, call_by_reference)
PPT
3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger
PPTX
6. Functions in C ++ programming object oriented programming
PPTX
Call by value V/S Call by reference with example
PPT
DOCX
Chapter 5
PPTX
Functions1
PPTX
practices of C programming function concepts
PPTX
3 Function & Storage Class.pptx
DOCX
PPS 6.6.FUNCTION INTRODUCTION & WRITING FUNCTIONS, SCOPE OF VARIABLES FUNCTIONS
Functions_C++ power point presentation s
Classes function overloading
FUNCTIONS, CLASSES AND OBJECTS.pptx
OOP-Module-1-Section-4-LectureNo1-5.pptx
Function Overloading Call by value and call by reference
Presentation.pptx
C++ FUNCTIONS-1.pptx
2-Concept of Pointers in c programming.pptx
Fundamental of programming Fundamental of programming
Pointers in c++ programming presentation
Pointers lesson 5 (double pointer, call by value, call_by_reference)
3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger
6. Functions in C ++ programming object oriented programming
Call by value V/S Call by reference with example
Chapter 5
Functions1
practices of C programming function concepts
3 Function & Storage Class.pptx
PPS 6.6.FUNCTION INTRODUCTION & WRITING FUNCTIONS, SCOPE OF VARIABLES FUNCTIONS
Ad

Recently uploaded (20)

PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Institutional Correction lecture only . . .
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Business Ethics Teaching Materials for college
PDF
Classroom Observation Tools for Teachers
PDF
Insiders guide to clinical Medicine.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
102 student loan defaulters named and shamed – Is someone you know on the list?
O7-L3 Supply Chain Operations - ICLT Program
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Microbial diseases, their pathogenesis and prophylaxis
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
VCE English Exam - Section C Student Revision Booklet
FourierSeries-QuestionsWithAnswers(Part-A).pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Institutional Correction lecture only . . .
Renaissance Architecture: A Journey from Faith to Humanism
Business Ethics Teaching Materials for college
Classroom Observation Tools for Teachers
Insiders guide to clinical Medicine.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Abdominal Access Techniques with Prof. Dr. R K Mishra
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester

Call by value or call by reference in C++

  • 1. Call by value in C++
  • 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(); • }
  • 11. • Using Class Example:- • #include <iostream.h> • #include <conio.h> • class fastest • { • public: • void show() { • cout<<"Hello! Friends "; • } • }; • void print(fastest &c) { • c.show(); • } • int main(void) { • fastest b; • print(b); • getch(); • }
  • 12. THANK YOU! Learn more and Enjoy More