SlideShare a Scribd company logo
Call by Value
• A function is a collection of statements that
accept inputs, carry out certain calculations,
and output the results.
• The concept is to group similar or often
performed actions into a function so that we
may call the function rather than writing the
same code again for various inputs.
• A function is a section of code that executes
only when it is called, to put it simply.
Function Overloading Call by value and call by reference
• When a function is called, new elements are
created on the stack memory to store the
essential information about the functions as
well as allocated memory space for parameters
and the return value.
The call-by-value method allows you to copy the
actual parameter to a formal parameter. In this
case, if we change the formal parameter then
the actual parameter doesn’t change.
• In other words, the value of the parameter is duplicated
into the memory location designated for the function’s
parameter. Consequently, two memory locations now hold
the same value. C++ uses call by value method by default.
• Upon exiting the function, the memory on the program
stack is ‘popped,’ leading to the removal of all data
associated with the function call. This includes the memory
location allocated for the parameters used within the
function. Consequently, any alterations made to values
within the function’s scope do not impact the values of
variables outside the function.
Use of Call by Value
• Call by value is used in certain conditions like:
• When you do not want to change the actual
parameters of the function.
• When you want to make copies of the data
instead of the actual data.
• When space is not an issue.
• Usually, when you do not deal with recursion
or backtracking.
Limitations of using Call by Value
• Although call by value is so useful for us while programming
in C++, it has a few limitations too:
• Data passed is then stored in temporary memory
• Can’t operate over the actual data rather need to work on
temporary copy made of the data, because of which changes
made in the data in the function is not reflected in main
function.
• Memory Space required while using string ,array , vector, etc
can be huge.
• Tackling Backtracking and recursion can be complex using
call by values.
Sample Code
// C++ Program to implement
// Swapping using Call by function
#include <iostream>
using namespace std;
// Swap function to demonstrate
// call by value method
void swap(int x, int y)
{
int t = x;
x = y;
y = t;
cout << "After Swapping in function x: " << x
<< ", y: " << y << endl;
}
// Driver Code
int main()
{
int x = 1, y = 2;
cout << "Before Swapping: ";
cout << "x: " << x << ", y: " << y << endl;
swap(x, y);
cout << "After Swapping: ";
cout << "x: " << x << ", y: " << y << endl;
return 0;
}
Call by Reference
• The call by reference method of passing arguments to a
function copies the reference of an argument into the formal
parameter. Inside the function, the reference is used to access
the actual argument used in the call. This means that changes
made to the parameter affect the passed argument.
• To pass the value by reference, argument reference is passed
to the functions just like any other value. So accordingly you
need to declare the function parameters as reference types as
in the following function swap(), which exchanges the values
of the two integer variables pointed to by its arguments.
// function definition to swap the values.
void swap(int &x, int &y)
{
int temp; temp = x; /* save the value at address x*/
x = y; /* put y into x */
y = temp; /* put x into y */
return;
}
Function Overloading Call by value and call by reference
Generic Programming in C++
Tushar Aneyrao
Generic Programming
• Generic programming is about generalizing
software components so that they can be
easily reused in a wide variety of situations. In
C++, class and function templates are
particularly effective mechanisms for generic
programming because they make the
generalization possible without sacrificing
efficiency.
• Generics is the idea to allow type (Integer, String, … etc
and user-defined types) to be a parameter to methods,
classes and interfaces. For example, classes like an array,
map, etc, which can be used using generics very
efficiently. We can use them for any type.
• The method of Generic Programming is implemented to
increase the efficiency of the code. Generic Programming
enables the programmer to write a general algorithm
which will work with all data types. It eliminates the need
to create different algorithms if the data type is an integer,
string or a character.
The advantages of Generic Programming are
• Code Reusability
• Avoid Function Overloading
• Once written it can be used for multiple times
and cases.
• Generics can be implemented in C++ using
Templates. Template is a simple and yet very
powerful tool in C++.
• The simple idea is to pass data type as a parameter
so that we don’t need to write the same code for
different data types.
• For example, a software company may need sort()
for different data types. Rather than writing and
maintaining the multiple codes, we can write one
sort() and pass data type as a parameter.
Generic Functions using Template:
• We write a generic function that can be used
for different data types. Examples of function
templates are sort(), max(), min(), printArray()
#include <iostream>
using namespace std;
template <class T, class U>
class A {
T x;
U y;
public:
A()
{
cout << "Constructor Called" << endl;
}
};
int main()
{
A<char, char> a;
A<int, double> b;
return 0;
}
Syntax for Template
Template <class T> //T can be int, float, char etc.
Class item {
T*arr;
Public:
Vector (T*arr)
{
//code
}
//Other methods
}
• Int main()
• {
• Vector <int> myVect (ptr);
• Vector <float> myVect(ptr);
Function Overloading
Tushar Aneyrao
Function Overloading
• Function overloading is a feature of object-oriented
programming where two or more functions can
have the same name but different parameters.
When a function name is overloaded with different
jobs it is called Function Overloading.
• In Function Overloading “Function” name should be
the same and the arguments should be different.
Function overloading can be considered as an
example of a polymorphism feature in C++.
• If multiple functions having same name but parameters
of the functions should be different is known as
Function Overloading.
• If we have to perform only one operation and having
same name of the functions increases the readability of
the program.
• Suppose you have to perform addition of the given
numbers but there can be any number of arguments, if
you write the function such as a(int,int) for two
parameters, and b(int,int,int) for three parameters then
it may be difficult for you to understand the behavior of
the function because its name differs
• The parameters should follow any one or more than one
of the following conditions for Function overloading:
• Parameters should have a different type
add(int a, int b)
add(double a, double b)
• Parameters should have a different number
add(int a, int b)
add(int a, int b, int c)
• Parameters should have a different sequence of
parameters.
add(int a, double b)
add(double a, int b)
How does Function Overloading work?
• Exact match:- (Function name and Parameter)
• If a not exact match is found:–
• ->Char, Unsigned char, and short are
promoted to an int.
• ->Float is promoted to double
• If no match is found:
• ->C++ tries to find a match through the
standard conversion.
• ELSE ERROR

More Related Content

PPTX
6. Functions in C ++ programming object oriented programming
DOCX
Functions in c++
PPTX
Classes function overloading
PPTX
FUNCTIONS, CLASSES AND OBJECTS.pptx
PPTX
Functions and modular programming.pptx
PPT
Material 3 (4).ppt this ppt is about the
PPT
PPTX
Functions in C++ (OOP)
6. Functions in C ++ programming object oriented programming
Functions in c++
Classes function overloading
FUNCTIONS, CLASSES AND OBJECTS.pptx
Functions and modular programming.pptx
Material 3 (4).ppt this ppt is about the
Functions in C++ (OOP)

Similar to Function Overloading Call by value and call by reference (20)

PPTX
Silde of the cse fundamentals a deep analysis
PDF
Functions in C++.pdf
PDF
PPTX
C++ FUNCTIONS-1.pptx
PPT
3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger
PPTX
Functions1
PPTX
Functions in c++, presentation, short and sweet presentation, and details of ...
 
PPTX
OOP-Module-1-Section-4-LectureNo1-5.pptx
DOCX
Chapter 5
PPTX
Lecture 4: Functions
PPT
16717 functions in C++
 
PDF
03 function overloading
PPTX
c++ introduction, array, pointers included.pptx
PPTX
C++_Functions_Detailed_Presentation.pptx
PPTX
chapter 5 Templates-Introduction in C++.pptx
PDF
Functions_C++ power point presentation s
PPT
PPTX
Chapter 6 - Modular Programming- in C++.pptx
PPTX
Presentation on polymorphism in c++.pptx
PPTX
Functions in C++
Silde of the cse fundamentals a deep analysis
Functions in C++.pdf
C++ FUNCTIONS-1.pptx
3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger
Functions1
Functions in c++, presentation, short and sweet presentation, and details of ...
 
OOP-Module-1-Section-4-LectureNo1-5.pptx
Chapter 5
Lecture 4: Functions
16717 functions in C++
 
03 function overloading
c++ introduction, array, pointers included.pptx
C++_Functions_Detailed_Presentation.pptx
chapter 5 Templates-Introduction in C++.pptx
Functions_C++ power point presentation s
Chapter 6 - Modular Programming- in C++.pptx
Presentation on polymorphism in c++.pptx
Functions in C++
Ad

Recently uploaded (20)

PPTX
GDM (1) (1).pptx small presentation for students
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Computing-Curriculum for Schools in Ghana
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Pre independence Education in Inndia.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
GDM (1) (1).pptx small presentation for students
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
O5-L3 Freight Transport Ops (International) V1.pdf
RMMM.pdf make it easy to upload and study
FourierSeries-QuestionsWithAnswers(Part-A).pdf
human mycosis Human fungal infections are called human mycosis..pptx
VCE English Exam - Section C Student Revision Booklet
Computing-Curriculum for Schools in Ghana
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
O7-L3 Supply Chain Operations - ICLT Program
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pre independence Education in Inndia.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
TR - Agricultural Crops Production NC III.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Final Presentation General Medicine 03-08-2024.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Abdominal Access Techniques with Prof. Dr. R K Mishra
Ad

Function Overloading Call by value and call by reference

  • 1. Call by Value • A function is a collection of statements that accept inputs, carry out certain calculations, and output the results. • The concept is to group similar or often performed actions into a function so that we may call the function rather than writing the same code again for various inputs. • A function is a section of code that executes only when it is called, to put it simply.
  • 3. • When a function is called, new elements are created on the stack memory to store the essential information about the functions as well as allocated memory space for parameters and the return value. The call-by-value method allows you to copy the actual parameter to a formal parameter. In this case, if we change the formal parameter then the actual parameter doesn’t change.
  • 4. • In other words, the value of the parameter is duplicated into the memory location designated for the function’s parameter. Consequently, two memory locations now hold the same value. C++ uses call by value method by default. • Upon exiting the function, the memory on the program stack is ‘popped,’ leading to the removal of all data associated with the function call. This includes the memory location allocated for the parameters used within the function. Consequently, any alterations made to values within the function’s scope do not impact the values of variables outside the function.
  • 5. Use of Call by Value • Call by value is used in certain conditions like: • When you do not want to change the actual parameters of the function. • When you want to make copies of the data instead of the actual data. • When space is not an issue. • Usually, when you do not deal with recursion or backtracking.
  • 6. Limitations of using Call by Value • Although call by value is so useful for us while programming in C++, it has a few limitations too: • Data passed is then stored in temporary memory • Can’t operate over the actual data rather need to work on temporary copy made of the data, because of which changes made in the data in the function is not reflected in main function. • Memory Space required while using string ,array , vector, etc can be huge. • Tackling Backtracking and recursion can be complex using call by values.
  • 7. Sample Code // C++ Program to implement // Swapping using Call by function #include <iostream> using namespace std; // Swap function to demonstrate // call by value method void swap(int x, int y) { int t = x; x = y; y = t; cout << "After Swapping in function x: " << x << ", y: " << y << endl; }
  • 8. // Driver Code int main() { int x = 1, y = 2; cout << "Before Swapping: "; cout << "x: " << x << ", y: " << y << endl; swap(x, y); cout << "After Swapping: "; cout << "x: " << x << ", y: " << y << endl; return 0; }
  • 9. Call by Reference • The call by reference method of passing arguments to a function copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument. • To pass the value by reference, argument reference is passed to the functions just like any other value. So accordingly you need to declare the function parameters as reference types as in the following function swap(), which exchanges the values of the two integer variables pointed to by its arguments.
  • 10. // function definition to swap the values. void swap(int &x, int &y) { int temp; temp = x; /* save the value at address x*/ x = y; /* put y into x */ y = temp; /* put x into y */ return; }
  • 12. Generic Programming in C++ Tushar Aneyrao
  • 13. Generic Programming • Generic programming is about generalizing software components so that they can be easily reused in a wide variety of situations. In C++, class and function templates are particularly effective mechanisms for generic programming because they make the generalization possible without sacrificing efficiency.
  • 14. • Generics is the idea to allow type (Integer, String, … etc and user-defined types) to be a parameter to methods, classes and interfaces. For example, classes like an array, map, etc, which can be used using generics very efficiently. We can use them for any type. • The method of Generic Programming is implemented to increase the efficiency of the code. Generic Programming enables the programmer to write a general algorithm which will work with all data types. It eliminates the need to create different algorithms if the data type is an integer, string or a character.
  • 15. The advantages of Generic Programming are • Code Reusability • Avoid Function Overloading • Once written it can be used for multiple times and cases.
  • 16. • Generics can be implemented in C++ using Templates. Template is a simple and yet very powerful tool in C++. • The simple idea is to pass data type as a parameter so that we don’t need to write the same code for different data types. • For example, a software company may need sort() for different data types. Rather than writing and maintaining the multiple codes, we can write one sort() and pass data type as a parameter.
  • 17. Generic Functions using Template: • We write a generic function that can be used for different data types. Examples of function templates are sort(), max(), min(), printArray()
  • 18. #include <iostream> using namespace std; template <class T, class U> class A { T x; U y; public: A() { cout << "Constructor Called" << endl; } }; int main() { A<char, char> a; A<int, double> b; return 0; }
  • 19. Syntax for Template Template <class T> //T can be int, float, char etc. Class item { T*arr; Public: Vector (T*arr) { //code } //Other methods }
  • 20. • Int main() • { • Vector <int> myVect (ptr); • Vector <float> myVect(ptr);
  • 22. Function Overloading • Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. • In Function Overloading “Function” name should be the same and the arguments should be different. Function overloading can be considered as an example of a polymorphism feature in C++.
  • 23. • If multiple functions having same name but parameters of the functions should be different is known as Function Overloading. • If we have to perform only one operation and having same name of the functions increases the readability of the program. • Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the function such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it may be difficult for you to understand the behavior of the function because its name differs • The parameters should follow any one or more than one of the following conditions for Function overloading:
  • 24. • Parameters should have a different type add(int a, int b) add(double a, double b) • Parameters should have a different number add(int a, int b) add(int a, int b, int c) • Parameters should have a different sequence of parameters. add(int a, double b) add(double a, int b)
  • 25. How does Function Overloading work? • Exact match:- (Function name and Parameter) • If a not exact match is found:– • ->Char, Unsigned char, and short are promoted to an int. • ->Float is promoted to double • If no match is found: • ->C++ tries to find a match through the standard conversion. • ELSE ERROR