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.

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

  • 1.
    BIRLA INSTITUTE OFTECHNOLOGY 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 & CONSTANTPARAMETERS TARUN TIWARI
  • 3.
    REFERENCE PARAMETERS • Incall 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 CallBy 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 referenceparameters • 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 constmodifier 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 DECLARATIONOF CONSTANT PARAMETERS return_type FunName(const type1 parameter1, const type2 parameter2, ..., const typeN parameterN) { ... }
  • 9.
    EXAMPLE OF CONSTANTPARAMETERS 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 youtry 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 followingfunction 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 thatoutput 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 canalso 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  Adefault 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) intsum(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  Defaultarguments 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 defaultparameter 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.
  • 22.