Build your professional network on facebook via our app Go to app
 
 
Posted in Community :

C++

 
Industry : IT Products Functional Area : Application Software
Activity:  0 comments  281 views  last activity : 07 06 2010 20:18:04 +0000
Share
 
 
 

How many times have you used functions that return results through reference parameters such as this:

void func(int & ra, int & rb, int & rc) ;

You would probably call this function this way:

func(a, b, c) ;

Sometimes, if you do not want the outputs of 'ra' and 'rb' arguments, but only interested in 'rc', you might be tempted to make a call to the function as follows:

int dummy, res ;

func(dummy, dummy, res) ;

You might end up saving the space that is required for that one extra 'int' argument by using 'dummy' in place of arguments 'a' and 'b'. The function may even work properly. But, what's important to note is that if the output argument 'rc' is dependent on the values already computed in 'ra' and 'rb' by func(), then we might be looking at an entirely different scenario. Take the following example implementation of func:

void func(int & ra, int & rb, int & rc)
{
 ra = 1 ;
 rb = 2 ;
 rc = ra + rb ;
}

void main()
{
 int dummy, res ;
 func(dummy, dummy, res) ;

 cout << "Value is: " << res ;
}

What do you think is the output? It's not the value '3', but it's '4'. Disaster!! You would have successfully traded accuracy / correctness for a simple saving of 4 bytes of virtual space.

Hence, in cases where you are not sure if the values of one result parameter affects other results, safely use different variables to hold output parameters.

 
0 comments on "Be careful with dummy reference arguments"
Add your comment on "Be careful with dummy reference arguments"

Rate:
Submit
Leading recruitment firm
Job Vacancy for Marketing/ Tendering in Bangalore
Vacancy for Marketing/ Tendering in Bangalore
Opportunity for Marketing/ Tendering in Bangalore
Viewers also viewed
19 th November 2011 Noida, NCR Chapter We care – do you? Connecting the Old Age Homes/Orphanages...
 
259 referals 16 comments, 205 views
Air Pollution Education Is Necessory vs Air Pollution Education is Not Necessory
 
945 referals 16 arguments, 184 views
The world's biggest social networking site Facebook faces a unique challenge next week when it...
 
1089 referals 21 arguments, 366 views
more...  
Recent Knowledge (77)
OK, lets say that a crore pages are printed (physical digital) all over the world everyday....
 
288 referals 12 comments, 111 views
We know that Ecommerce is the most happening trend of the day. After the emergence of ecommrce,...
109 referals 1 comments, 53 views
The word 'Push' has traditionally characterized FMCG sales systems in India. This is changing...
 
11 referals 5 comments, 625 views
more...  
More From Author
What is the best C++ compiler available?
How many times have you used functions that return results through reference parameters such as this: void func(int ra, int rb, int rc) ; You would probably call this function this way: func(a, b, c) ; Sometimes, if you do not want the outputs...
Has C++ been the positive step forward from C?
more...