SlideShare a Scribd company logo
6
Most read
7
Most read
10
Most read
BIRLA INSTITUTE OF TECHNOLOGY MESRA,
JAIPUR CAMPUS
:- TARUN TIWARI(MCA/25007/18)
:- NIKHIL AGRAWAL(MCA/25004/18)
TOPIC:- REFERENCE PARAMETERS ,Passing object by reference ,
CONSTANT PARAMETERS & DEFAULT PARAMETER
REFERENCE PARAMETERS &
CONSTANT PARAMETERS
TARUN TIWARI
REFERENCE PARAMETERS
• In call by reference method the called function does not create its own copy rather it
refers to original value only by different name i.e. reference.
• When function is called by reference then, the formal parameter become
reference or alias to the actual parameter in calling function.
• To have a function with multiple outputs, we have to use pass by
reference.
• We use &to denote a parameter that is passed by reference:
Syntax:
<type>& <variable>
Examples:
• void Increment(int& Number);
• void SumAve (double, double, double&, double&);
• The corresponding argument must be a variable.
Increment(Inc);
SumAve(2.5, y+3, sum, mean);
Example of Call By Reference
#include <iostream.h>
void duplicate (int& a, int& b, int& c)
{ a*=2; b*=2; c*=2;
}
int main ( )
{
int x=1, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ", z="
<< z;
return 0;
}
x=2, y=6, z=14
Void duplicate (int& a, int& b, int& c)
x y z
duplicate( x , y , z )
Advantages of reference parameters
• We can return multiple values from a function
• Because a copy of the argument is not made, it is fast, even when
used with large structs or classes.
• Reference must be initialized
Disadvantages of reference parameters
• Many potential scenarios can occur
• Programs are difficult to understand sometimes
• Un-wanted Aliases
• The const modifier can be applied to formal parameter declarations
• The constant parameter received by the function can not be changed
in the body of the function.
CONSTANT PARAMETERS
• const indicates that the function may not modify the parameter
const type parameter
DECLARATION OF CONSTANT PARAMETERS
return_type FunName(const type1 parameter1, const
type2 parameter2, ..., const typeN parameterN)
{
...
}
EXAMPLE OF CONSTANT PARAMETERS
double Area( const double radius)
{
return double (3.1415 * radius * radius);
}
double area;
area = Area(5.5); area = 95.0304
double r = 7.88;
area = Area(r); area = 195.07
• If you try to change the value of the radius constant in the Area()
function, for example
radius = 2.88;
• the compiler will generate an error:
'radius': you cannot assign to a variable that is const.
PASSING OBJECTS BY
REFERENCE &
DEFAULT PARAMETERS.
NIKHIL AGRAWAL
PASSING OBJECT BY
REFERENCE
 One situation where it is absolutely mandatory to use a reference parameter is when we
pass a Stream object to a function.
 The reason is, When we extract data from a stream or insert data into a stream ,we are
changing the stream.
 As we know, if we passed the stream object by value, any changes that we made to the
stream object would be made to a copy of the stream object, not to the actual object.
 Thus whenever we need to pass a stream to a function, we must pass the stream by
reference.
Example :The following function writes an integer value to a stream
that is passed as the parameter to the function :-
Void OutputValue(ostream &out, int value){
out <<“ Value is “<< Value<<endl;
return;
}
The next line of code uses the OutputValue() to write the value k to
the stream cout :-
OutputValue(cout,k);
• Functions that output values to a stream are
useful when we need to output several values along
with labelling information from several places in a
program.
• We might use such a function to write data to the
display as well as to a log file for later examination.
• Another use would be to help debug the program.
 Example : Suppose , for example, that we wish to “watch” Sort3() function as it sorts the values :-
Void ShowValues(ostream &out , int a, int b, int c) {
Out <<“a: “<<a<<endl;
Out<<“b: “<<b<<endl;
Out<<“c: “<<c<<endl;
return;
}
//sort three numbers into ascending order
Void Sort3(int &a, int &b, int &c) {
If(a>b)
swap(a,b);
ShowValues(clog, a, b, c);
If(a>c)
swap(a,c);
ShowValues(clog, a, b, c);
If(b>c)
swap(b,c);
ShowValues(clog, a, b, c);
 We can also put the ability to pass an input stream as a parameter to a function to good
use.
 For example :
• Suppose we are writing a program to process data from a file.
• Program development and testing will go faster if the program takes the input from the keyboard.
• We can quickly test the program by typing in data, rather than creating a test file each time we
want to test the program on different data.
• However ,eventually the program will be reading data from a file.
• We do not want to write the program so that it accepts input from the keyboard and then , when the
program is complete , have to make extensive changes to the program so that it operates on a
different stream.
• We can handle this situation by writing any functions that extract input to accept as a parameter
the stream to read data from .
Program :
bool ReadValues (istream &in, int &v1, int &v2, int &v3)
{
if ( in == cin)
cout<<“ Please enter three numbers”;
if ( in >> v1 >> v2 >> v3)
return true;
else
return false;
}
DEFAULT ARGUMENTS
 A default argument is a value provided in function declaration that is automatically
assigned by the compiler if caller of the function doesn’t provide a value for the argument
with default value.
 Syntax: f _type f_name (arg1,arg2,arg3=value).
 When a default argument is placed then all the other arguments after that must be
default arguments only.
DEFAULT ARGUMENTS (EXAMPLE)
int sum(int x,int y,int z=0,int w=0)
{
return(x+y+z+w);
}
int main()
{
cout<<sum(10,15)<<endl;
cout<<sum(10,15,25)<<endl;
cout<<sum(10,15,25,30)<<endl;
return 0;
}
• OUTPUT:
25
50
80
DEFAULT ARGUMENTS
 Default arguments are overwritten when calling function provides values for them. For
example, calling of function sum(10, 15, 25, 30) overwrites the value of z and w to 25 and
30 respectively.
 During calling of function, arguments from calling function to called function are copied
from left to right. Therefore, sum(10, 15, 25) will assign 10, 15 and 25 to x, y and z.
Therefore, default value is used for w only.
DEFAULT ARGUMENTS
The default parameter method is especially useful
when one wants to set default criteria so that the
function can be called with or without parameters.
Default arguments are different from constant
arguments as constant arguments can’t be changed
whereas default arguments can be overwritten if
required.
THANK YOU.

More Related Content

What's hot (20)

Programmers model of 8086
Programmers model of 8086
KunalPatel260
 
integrity constraints
integrity constraints
madhav bansal
 
Query processing
Query processing
Ravinder Kamboj
 
8085-microprocessor
8085-microprocessor
ATTO RATHORE
 
TCP IP Addressing
TCP IP Addressing
Ritul Sonania
 
Control structures in C
Control structures in C
baabtra.com - No. 1 supplier of quality freshers
 
Ch8 (1) morris mano
Ch8 (1) morris mano
KIRTI89
 
Subnetting
Subnetting
selvakumar_b1985
 
Anomalies in database
Anomalies in database
baabtra.com - No. 1 supplier of quality freshers
 
Computer Organization and 8085 microprocessor notes
Computer Organization and 8085 microprocessor notes
Lakshmi Sarvani Videla
 
Instruction codes
Instruction codes
pradeepa velmurugan
 
02 c++ Array Pointer
02 c++ Array Pointer
Tareq Hasan
 
Microprocessors
Microprocessors
Rajat Dhiman
 
Computer data type and Terminologies
Computer data type and Terminologies
glyvive
 
Unit 1 introduction to data structure
Unit 1 introduction to data structure
kalyanineve
 
Tail recursion
Tail recursion
Rumman Ansari
 
Lecture 3 instruction set
Lecture 3 instruction set
Pradeep Kumar TS
 
Chapter 5 ds
Chapter 5 ds
Hanif Durad
 
Ipv4 and Ipv6
Ipv4 and Ipv6
rahul kundu
 
Parity Generator and Parity Checker
Parity Generator and Parity Checker
Jignesh Navdiya
 

Similar to Reference Parameter, Passing object by reference, constant parameter & Default parameter (20)

Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
LidetAdmassu
 
Amit user defined functions xi (2)
Amit user defined functions xi (2)
Arpit Meena
 
FUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptx
DeepasCSE
 
16717 functions in C++
16717 functions in C++
LPU
 
Functions assignment
Functions assignment
Ahmad Kamal
 
Parameter passing to_functions_in_c
Parameter passing to_functions_in_c
ForwardBlog Enewzletter
 
Functions
Functions
Amanda Winona Batayola
 
Dd3.15 thru-3.21-advanced-functions
Dd3.15 thru-3.21-advanced-functions
temkin abdlkader
 
Classes function overloading
Classes function overloading
ankush_kumar
 
Functions1
Functions1
DrUjwala1
 
Programming Fundamentals lecture-10.pptx
Programming Fundamentals lecture-10.pptx
singyali199
 
Functionssssssssssssssssssssssssssss.pdf
Functionssssssssssssssssssssssssssss.pdf
bodzzaa21
 
Functions
Functions
PatriciaPabalan
 
Chapter 4
Chapter 4
temkin abdlkader
 
Oop1
Oop1
Vaibhav Bajaj
 
Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++
NUST Stuff
 
C++ lecture 03
C++ lecture 03
HNDE Labuduwa Galle
 
Function Overloading Call by value and call by reference
Function Overloading Call by value and call by reference
TusharAneyrao1
 
Functions in C++.pdf
Functions in C++.pdf
LadallaRajKumar
 
Function C++
Function C++
Shahzad Afridi
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
LidetAdmassu
 
Amit user defined functions xi (2)
Amit user defined functions xi (2)
Arpit Meena
 
FUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptx
DeepasCSE
 
16717 functions in C++
16717 functions in C++
LPU
 
Functions assignment
Functions assignment
Ahmad Kamal
 
Dd3.15 thru-3.21-advanced-functions
Dd3.15 thru-3.21-advanced-functions
temkin abdlkader
 
Classes function overloading
Classes function overloading
ankush_kumar
 
Programming Fundamentals lecture-10.pptx
Programming Fundamentals lecture-10.pptx
singyali199
 
Functionssssssssssssssssssssssssssss.pdf
Functionssssssssssssssssssssssssssss.pdf
bodzzaa21
 
Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++
NUST Stuff
 
Function Overloading Call by value and call by reference
Function Overloading Call by value and call by reference
TusharAneyrao1
 
Ad

More from Meghaj Mallick (20)

24 partial-orderings
24 partial-orderings
Meghaj Mallick
 
PORTFOLIO BY USING HTML & CSS
PORTFOLIO BY USING HTML & CSS
Meghaj Mallick
 
Introduction to Software Testing
Introduction to Software Testing
Meghaj Mallick
 
Introduction to System Programming
Introduction to System Programming
Meghaj Mallick
 
MACRO ASSEBLER
MACRO ASSEBLER
Meghaj Mallick
 
Icons, Image & Multimedia
Icons, Image & Multimedia
Meghaj Mallick
 
Project Tracking & SPC
Project Tracking & SPC
Meghaj Mallick
 
Peephole Optimization
Peephole Optimization
Meghaj Mallick
 
Routing in MANET
Routing in MANET
Meghaj Mallick
 
Macro assembler
Macro assembler
Meghaj Mallick
 
Architecture and security in Vanet PPT
Architecture and security in Vanet PPT
Meghaj Mallick
 
Design Model & User Interface Design in Software Engineering
Design Model & User Interface Design in Software Engineering
Meghaj Mallick
 
Text Mining of Twitter in Data Mining
Text Mining of Twitter in Data Mining
Meghaj Mallick
 
DFS & BFS in Computer Algorithm
DFS & BFS in Computer Algorithm
Meghaj Mallick
 
Software Development Method
Software Development Method
Meghaj Mallick
 
Secant method in Numerical & Statistical Method
Secant method in Numerical & Statistical Method
Meghaj Mallick
 
Motivation in Organization
Motivation in Organization
Meghaj Mallick
 
Communication Skill
Communication Skill
Meghaj Mallick
 
Partial-Orderings in Discrete Mathematics
Partial-Orderings in Discrete Mathematics
Meghaj Mallick
 
Hashing In Data Structure
Hashing In Data Structure
Meghaj Mallick
 
PORTFOLIO BY USING HTML & CSS
PORTFOLIO BY USING HTML & CSS
Meghaj Mallick
 
Introduction to Software Testing
Introduction to Software Testing
Meghaj Mallick
 
Introduction to System Programming
Introduction to System Programming
Meghaj Mallick
 
Icons, Image & Multimedia
Icons, Image & Multimedia
Meghaj Mallick
 
Project Tracking & SPC
Project Tracking & SPC
Meghaj Mallick
 
Architecture and security in Vanet PPT
Architecture and security in Vanet PPT
Meghaj Mallick
 
Design Model & User Interface Design in Software Engineering
Design Model & User Interface Design in Software Engineering
Meghaj Mallick
 
Text Mining of Twitter in Data Mining
Text Mining of Twitter in Data Mining
Meghaj Mallick
 
DFS & BFS in Computer Algorithm
DFS & BFS in Computer Algorithm
Meghaj Mallick
 
Software Development Method
Software Development Method
Meghaj Mallick
 
Secant method in Numerical & Statistical Method
Secant method in Numerical & Statistical Method
Meghaj Mallick
 
Motivation in Organization
Motivation in Organization
Meghaj Mallick
 
Partial-Orderings in Discrete Mathematics
Partial-Orderings in Discrete Mathematics
Meghaj Mallick
 
Hashing In Data Structure
Hashing In Data Structure
Meghaj Mallick
 
Ad

Recently uploaded (20)

Pentecost Sunday A Promise of Power.pptx
Pentecost Sunday A Promise of Power.pptx
FamilyWorshipCenterD
 
The Power of religious Symbols: A scientific and spiritual analysis
The Power of religious Symbols: A scientific and spiritual analysis
Dr. Anshula Garg
 
The Caribbean Challenge: Fostering Growth and Resilience Amidst Global Uncert...
The Caribbean Challenge: Fostering Growth and Resilience Amidst Global Uncert...
Caribbean Development Bank
 
ART INTAGRATED PROJECT in chemistry about resources
ART INTAGRATED PROJECT in chemistry about resources
mcarchana74
 
From Idea to Impact: Maximizing Your Hackathon Performance
From Idea to Impact: Maximizing Your Hackathon Performance
outsystemspuneusergr
 
Diddy Baby oil making tutorial (natural ingresients.pptx
Diddy Baby oil making tutorial (natural ingresients.pptx
RanitMal
 
FUTURE OF FITNESS 2025 KEYNOTE BRYAN OROURKE BEYOND ACTIV SINGAPORE 2025
FUTURE OF FITNESS 2025 KEYNOTE BRYAN OROURKE BEYOND ACTIV SINGAPORE 2025
Bryan K. O'Rourke
 
THE INTERIOR REVIEW MEDIA KIT - THE INTERIOR REVIEW
THE INTERIOR REVIEW MEDIA KIT - THE INTERIOR REVIEW
rspyamin
 
Seminar Presented by Natnael Dechasa Title: Brain Cheat Codes: The Science-Ba...
Seminar Presented by Natnael Dechasa Title: Brain Cheat Codes: The Science-Ba...
Nati1986
 
Assesement_PPT Designer -----------Final
Assesement_PPT Designer -----------Final
RajeshKumarKumre
 
Retail Store Scavenger Hunt experience!!
Retail Store Scavenger Hunt experience!!
Samally Dávila
 
Jadual Waktu dan Jadual Bertugas kelas.pptx
Jadual Waktu dan Jadual Bertugas kelas.pptx
roslan17
 
Types of Information Sources (Primary, Secondary, and Tertiary Sources)
Types of Information Sources (Primary, Secondary, and Tertiary Sources)
jenicahmendoza1
 
Presenation - compensation plan - Mining Race - NEW - June 2025
Presenation - compensation plan - Mining Race - NEW - June 2025
Mining RACE
 
Road safety presentation for high school
Road safety presentation for high school
nikhithavarghese77
 
كتاب تتتتتماين من انتااا كليه علوم حلوان
كتاب تتتتتماين من انتااا كليه علوم حلوان
ziadwaleed55op
 
2025-06-08 Abraham 02 (shared slides).pptx
2025-06-08 Abraham 02 (shared slides).pptx
Dale Wells
 
THE HISTORY AND EVOLUTION OF VARIOUS SWORDS.pdf
THE HISTORY AND EVOLUTION OF VARIOUS SWORDS.pdf
sethjamcam
 
Sample work (PL Product Research) Joseph_Juntilla.pdf
Sample work (PL Product Research) Joseph_Juntilla.pdf
Joseph Juntilla
 
Timothy J. Los, JD, LL.M. (Tax) – Global Investor, Family Office & Profession...
Timothy J. Los, JD, LL.M. (Tax) – Global Investor, Family Office & Profession...
Timothy Los
 
Pentecost Sunday A Promise of Power.pptx
Pentecost Sunday A Promise of Power.pptx
FamilyWorshipCenterD
 
The Power of religious Symbols: A scientific and spiritual analysis
The Power of religious Symbols: A scientific and spiritual analysis
Dr. Anshula Garg
 
The Caribbean Challenge: Fostering Growth and Resilience Amidst Global Uncert...
The Caribbean Challenge: Fostering Growth and Resilience Amidst Global Uncert...
Caribbean Development Bank
 
ART INTAGRATED PROJECT in chemistry about resources
ART INTAGRATED PROJECT in chemistry about resources
mcarchana74
 
From Idea to Impact: Maximizing Your Hackathon Performance
From Idea to Impact: Maximizing Your Hackathon Performance
outsystemspuneusergr
 
Diddy Baby oil making tutorial (natural ingresients.pptx
Diddy Baby oil making tutorial (natural ingresients.pptx
RanitMal
 
FUTURE OF FITNESS 2025 KEYNOTE BRYAN OROURKE BEYOND ACTIV SINGAPORE 2025
FUTURE OF FITNESS 2025 KEYNOTE BRYAN OROURKE BEYOND ACTIV SINGAPORE 2025
Bryan K. O'Rourke
 
THE INTERIOR REVIEW MEDIA KIT - THE INTERIOR REVIEW
THE INTERIOR REVIEW MEDIA KIT - THE INTERIOR REVIEW
rspyamin
 
Seminar Presented by Natnael Dechasa Title: Brain Cheat Codes: The Science-Ba...
Seminar Presented by Natnael Dechasa Title: Brain Cheat Codes: The Science-Ba...
Nati1986
 
Assesement_PPT Designer -----------Final
Assesement_PPT Designer -----------Final
RajeshKumarKumre
 
Retail Store Scavenger Hunt experience!!
Retail Store Scavenger Hunt experience!!
Samally Dávila
 
Jadual Waktu dan Jadual Bertugas kelas.pptx
Jadual Waktu dan Jadual Bertugas kelas.pptx
roslan17
 
Types of Information Sources (Primary, Secondary, and Tertiary Sources)
Types of Information Sources (Primary, Secondary, and Tertiary Sources)
jenicahmendoza1
 
Presenation - compensation plan - Mining Race - NEW - June 2025
Presenation - compensation plan - Mining Race - NEW - June 2025
Mining RACE
 
Road safety presentation for high school
Road safety presentation for high school
nikhithavarghese77
 
كتاب تتتتتماين من انتااا كليه علوم حلوان
كتاب تتتتتماين من انتااا كليه علوم حلوان
ziadwaleed55op
 
2025-06-08 Abraham 02 (shared slides).pptx
2025-06-08 Abraham 02 (shared slides).pptx
Dale Wells
 
THE HISTORY AND EVOLUTION OF VARIOUS SWORDS.pdf
THE HISTORY AND EVOLUTION OF VARIOUS SWORDS.pdf
sethjamcam
 
Sample work (PL Product Research) Joseph_Juntilla.pdf
Sample work (PL Product Research) Joseph_Juntilla.pdf
Joseph Juntilla
 
Timothy J. Los, JD, LL.M. (Tax) – Global Investor, Family Office & Profession...
Timothy J. Los, JD, LL.M. (Tax) – Global Investor, Family Office & Profession...
Timothy Los
 

Reference Parameter, Passing object by reference, constant parameter & Default parameter

  • 1. BIRLA INSTITUTE OF TECHNOLOGY MESRA, JAIPUR CAMPUS :- TARUN TIWARI(MCA/25007/18) :- NIKHIL AGRAWAL(MCA/25004/18) TOPIC:- REFERENCE PARAMETERS ,Passing object by reference , CONSTANT PARAMETERS & DEFAULT PARAMETER
  • 2. REFERENCE PARAMETERS & CONSTANT PARAMETERS TARUN TIWARI
  • 3. REFERENCE PARAMETERS • In call by reference method the called function does not create its own copy rather it refers to original value only by different name i.e. reference. • When function is called by reference then, the formal parameter become reference or alias to the actual parameter in calling function. • To have a function with multiple outputs, we have to use pass by reference.
  • 4. • We use &to denote a parameter that is passed by reference: Syntax: <type>& <variable> Examples: • void Increment(int& Number); • void SumAve (double, double, double&, double&); • The corresponding argument must be a variable. Increment(Inc); SumAve(2.5, y+3, sum, mean);
  • 5. Example of Call By Reference #include <iostream.h> void duplicate (int& a, int& b, int& c) { a*=2; b*=2; c*=2; } int main ( ) { int x=1, y=3, z=7; duplicate (x, y, z); cout << "x=" << x << ", y=" << y << ", z=" << z; return 0; } x=2, y=6, z=14 Void duplicate (int& a, int& b, int& c) x y z duplicate( x , y , z )
  • 6. Advantages of reference parameters • We can return multiple values from a function • Because a copy of the argument is not made, it is fast, even when used with large structs or classes. • Reference must be initialized Disadvantages of reference parameters • Many potential scenarios can occur • Programs are difficult to understand sometimes • Un-wanted Aliases
  • 7. • The const modifier can be applied to formal parameter declarations • The constant parameter received by the function can not be changed in the body of the function. CONSTANT PARAMETERS • const indicates that the function may not modify the parameter
  • 8. const type parameter DECLARATION OF CONSTANT PARAMETERS return_type FunName(const type1 parameter1, const type2 parameter2, ..., const typeN parameterN) { ... }
  • 9. EXAMPLE OF CONSTANT PARAMETERS double Area( const double radius) { return double (3.1415 * radius * radius); } double area; area = Area(5.5); area = 95.0304 double r = 7.88; area = Area(r); area = 195.07
  • 10. • If you try to change the value of the radius constant in the Area() function, for example radius = 2.88; • the compiler will generate an error: 'radius': you cannot assign to a variable that is const.
  • 11. PASSING OBJECTS BY REFERENCE & DEFAULT PARAMETERS. NIKHIL AGRAWAL
  • 12. PASSING OBJECT BY REFERENCE  One situation where it is absolutely mandatory to use a reference parameter is when we pass a Stream object to a function.  The reason is, When we extract data from a stream or insert data into a stream ,we are changing the stream.  As we know, if we passed the stream object by value, any changes that we made to the stream object would be made to a copy of the stream object, not to the actual object.  Thus whenever we need to pass a stream to a function, we must pass the stream by reference.
  • 13. Example :The following function writes an integer value to a stream that is passed as the parameter to the function :- Void OutputValue(ostream &out, int value){ out <<“ Value is “<< Value<<endl; return; } The next line of code uses the OutputValue() to write the value k to the stream cout :- OutputValue(cout,k);
  • 14. • Functions that output values to a stream are useful when we need to output several values along with labelling information from several places in a program. • We might use such a function to write data to the display as well as to a log file for later examination. • Another use would be to help debug the program.
  • 15.  Example : Suppose , for example, that we wish to “watch” Sort3() function as it sorts the values :- Void ShowValues(ostream &out , int a, int b, int c) { Out <<“a: “<<a<<endl; Out<<“b: “<<b<<endl; Out<<“c: “<<c<<endl; return; } //sort three numbers into ascending order Void Sort3(int &a, int &b, int &c) { If(a>b) swap(a,b); ShowValues(clog, a, b, c); If(a>c) swap(a,c); ShowValues(clog, a, b, c); If(b>c) swap(b,c); ShowValues(clog, a, b, c);
  • 16.  We can also put the ability to pass an input stream as a parameter to a function to good use.  For example : • Suppose we are writing a program to process data from a file. • Program development and testing will go faster if the program takes the input from the keyboard. • We can quickly test the program by typing in data, rather than creating a test file each time we want to test the program on different data. • However ,eventually the program will be reading data from a file. • We do not want to write the program so that it accepts input from the keyboard and then , when the program is complete , have to make extensive changes to the program so that it operates on a different stream. • We can handle this situation by writing any functions that extract input to accept as a parameter the stream to read data from .
  • 17. Program : bool ReadValues (istream &in, int &v1, int &v2, int &v3) { if ( in == cin) cout<<“ Please enter three numbers”; if ( in >> v1 >> v2 >> v3) return true; else return false; }
  • 18. DEFAULT ARGUMENTS  A default argument is a value provided in function declaration that is automatically assigned by the compiler if caller of the function doesn’t provide a value for the argument with default value.  Syntax: f _type f_name (arg1,arg2,arg3=value).  When a default argument is placed then all the other arguments after that must be default arguments only.
  • 19. DEFAULT ARGUMENTS (EXAMPLE) int sum(int x,int y,int z=0,int w=0) { return(x+y+z+w); } int main() { cout<<sum(10,15)<<endl; cout<<sum(10,15,25)<<endl; cout<<sum(10,15,25,30)<<endl; return 0; } • OUTPUT: 25 50 80
  • 20. DEFAULT ARGUMENTS  Default arguments are overwritten when calling function provides values for them. For example, calling of function sum(10, 15, 25, 30) overwrites the value of z and w to 25 and 30 respectively.  During calling of function, arguments from calling function to called function are copied from left to right. Therefore, sum(10, 15, 25) will assign 10, 15 and 25 to x, y and z. Therefore, default value is used for w only.
  • 21. DEFAULT ARGUMENTS The default parameter method is especially useful when one wants to set default criteria so that the function can be called with or without parameters. Default arguments are different from constant arguments as constant arguments can’t be changed whereas default arguments can be overwritten if required.