Parameter(argument) Passing

Techniques
Introduction
 Parameter passing is a mechanism for communication of data and
information between the caller and callee
 It can be achieved either by passing the value or address of the variable
 C++ supports the following three types of parameter passing techniques
 Pass by Value
 Pass by Address
 Pass by Reference
 The parameters can be classified as formal and actual parameters
Pass by value
 It does not change the contents of argument variable in the caller, even if they
are changed in the callee, because the contents of the actual parameter in a caller
is copied to the formal parameter in the callee
 The function parameter is stored in the local data area of the callee
 Changes to the parameter within the function will affect only the copy and
will have no effect on the actual arguments
Demonstrates parameter passing by value
#include<iostream.h>
void swap(int x, int y)
{
int temp;
cout<<“Before swapping in
swap():”<<x<<y<<endl;
temp=x;
y=temp;
cout<<“After swapping in
swap():”<<x<<y<<endl;
}
main( )
{
int a=1, b=2;
swap(a, b);
cout<<“After swapping in main()
:”<<a<<b<<endl;
//values remain unaltered
}
Output
Before swapping in swap(): 1 2
After swapping in swap() : 2 1
After swapping in main() : 1 2
Pass by value is not suitable in situation
 When a large class object must be passed as an argument. The time and space
costs to allocate and copy the class object on to stack
 When the values of the arguments must be modified
Pass by address
 C++ provides another means of passing values to a function known as Pass
by address
 Instead of passing the value, the address of the variable is passed
 In the function, the address of the argument is copied into a memory location
instead of the value
 The dereferencing operator is used to access the variable in the called
function
//Demonstrates parameter passing by pass by address
#include<iostream.h>
void swap( int *x, int *y)
{
int temp;
cout<<“Before
swapping:”<<a<<b<<endl;
temp=*x;
*x=*y;
*y=temp;
return;
}


main( )
{
int a=1, b=2;
swap( &a, &b);
cout<<“After swapping in main():
“<<a<<b<<endl; // value altered
}
Output
Before swapping :1 2
After swapping : 2 1
After swapping in main(): 2 1
Pass by Reference
 The function call is similar to that of call-by value
 In the function declarator , those parameters, which are to be received must
be preceded by & operator
 The reference type formal parameters are accessed in the same way as normal
value parameters

Note :
1. A reference can never be null, it must always refer to a legal object
2. Once established, a reference can never be changed so as to make it point to
a different object
3. A reference does not require any explicit mechanism to dereference the
memory address and access the actual data value
//Demonstrates argument passing by pass by
reference
#include <iostream.h>
void swap(int &x, int &y)
{
int temp:
cout<<“Before
swapping:”<<a<<b<,endl;
temp=x;
x=y;
y=temp;
return ;
}
main ( )
{
int a=1, b=2;
swap(a, b);
cout<<“After swapping in main() :
“<<a<<b<<endl;
//value altered
}

Uses and advantages of declaring a parameter as a reference
1. Function receives the lvalues of the argument rather than a copy of its value
2. Returns additional results to the calling function
3. Pass large class objects to a function
Passing objects as arguments
 Objects can be passed as parameter to a function either by pass by value or by
reference
 Other objects can be passed as arguments to function and the result is not
returned by the function
 Result is automatically stored in the object of which the function is a member
//demonstrates passing objects as parameter to a function
#include<iostream.h>
class Dist
{
int feet;
float inch;
public:
Dist() { }
Dist(int x, float y) {feet=x;inch=y;}
void show() {cout<<feet<<“\’-”<<endl;
void add_dist(Dist d, Dist d2)
{
inch=d1.inch+d2.inch;
feet=0;
if (inch >=12.0)
{
inch=inch-12.0;
feet++;
}
feet=feet+d1.feet+d2.feet;
}
};



main( )
{
Dist dist1(17, 5.75), dist2(11, 6.25);
Dist dist3;
dist3.add_dist(dist1, dist2);
cout<<“Distance1:”<<dist1.show();
cout<<“Distance2:”<<dist2.show();
cout<<“Distance3:”<<dist3.show();
}

Output:
Distance 1: 17’-5.75’’
Distance 2 : 11’-6.25”
Distance 3:29’-0”
Recursion
 Recursion is a technique of defining something in terms of itself
 In the field of mathematics and computer science, many concepts can be
explained using recursion
 In computer terminology, if a function calls itself then it is known as
recursive function
 If the function calls itself directly, then it is known as Direct recursion
Example :
void myfun()
{
----------
----------
myfun();
-----------
}
Continues….
 If the function calls itself through another function, then it is known as indirect
recursion

Example :
void myfun()
{
-----
fun();
------
}
void fun()
{
------
myfun();
----------
}
Note : the function myfun() is calling the function fun(), which in turn calls myfun().
Such a recursive call is know as indirect recursive
//Factorial of a number
#include<iostream.h>
long fact(int);
void main( )
{
int n;
cout<<“Enter the value of n”;
cin>>n;
if (n<0)
cout<<“Invalid input”;
else
cout<<“\n Factorial of “<<n<<“is”<<n<<fact(n);
}

long fact(int n)
{
if (n== 0)
return 1;
else
return n*fact(n-1);
}




Continues….
 some facts recursive function
 Recursive functions do not usually reduce the code size
 They do not improve memory utilization compared to iterative function
 Many of the recursive functions may execute a bit slower compared to
iterative functions because of the overhead of repeated function calls
 They may cause a stack overrun, as each new call to the recursive function
creates a new copy of variables and parameters and puts them into the stack
 Advantage
 Recursion function is to create clearer and simpler programs
//Demonstrates function
#include<iostream.h>
void swap(char &x, char &y)
{
char t;
t=x;
x=y;
y=t;
}
void swap(int & x, int &y)
{ int t;
t=x;
x=y;
y=t;
}
void swap(float &x, float &y)
{
float t;
t=x;
x=y;
y=t;
}
main()
{ char ch1, ch2;
cout<<“Enter two characters\n”;
cin>>ch1>>ch2;
swap(ch1,ch2);
cout<<“On swapping (ch1,ch2):”<<ch1<<“ “<<ch2<<endl;
int c1, c2;
cout<<“Enter two characters\n”;
cin>>ch1>>ch2;
swap(c1,c2);
cout<<“On swapping (c1,c2):”<<c1<<“ “<<c2<<endl;
float f1, f2;
cout<<“Enter two characters\n”;
cin>>f1>>cf2;
swap(f1,f2);
cout<<“On swapping (f1,f2):”<<f1<<“ “<<f2<<endl;
}
There may be several functions which needs to be used
frequently with different data types. The limitation of such
function is that, they operate only on a particular data type

Function Templates
Introduction
 Template is one of the features added to C++ recently
 New concept which enable us to define generic classes and functions
 Provides support for generic programming
Generic Programming : An approach where generic types are used as
parameters in algorithms , it work for a variety of suitable data types and data
Structures
 A template can be used to create a family of classes or functions
 A template can be considered as a kind of macro
 A template is defined with a parameter that would be replaced by a
specified data type at the time of actual use of the class or function
 The templates are sometimes called parameterized classes or functions


Continues..
General format of a function template
template<class T>
returntype functionname (arguments of type T)
{
//…….
//Body of function
//with type T
//wherever appropriate
}