Returning an argument or local variable ?

A

Alex6996xelA

Which one sounds not better ?

type ya(type a1){
type a2;

//do lilt somethin' for ya

return a1; // return a2;
}

Soo sorry for newbie question because I am not a professional or
intermediate coder. Thank you in advance..
 
M

marbac

Alex6996xelA said:
Which one sounds not better ?
It depends on what you want to do with the function.
I would prefer return a1, if you don`t need a temporary variable to do
some calculations

If you need a temporary variable, you can return a1 or a2, depends
what value holds the result first you want to return after the "do lilt
somethin' for ya"

regards marbac
 
M

marbac

Alex6996xelA said:
Which one sounds not better ?

type ya(type a1){
type a2;

//do lilt somethin' for ya

return a1; // return a2;
}

It depends on what you want to do with the function.
I would prefer return a1, if you don`t need a temporary variable to do
some calculations

If you need a temporary variable, you can return a1 or a2, depends
what variable holds the result you want to return after the "do lilt
somethin' for ya"

regards marbac
 
C

codigo

Alex6996xelA said:
Which one sounds not better ?

type ya(type a1){
type a2;

//do lilt somethin' for ya

return a1; // return a2;
}

Soo sorry for newbie question because I am not a professional or
intermediate coder. Thank you in advance..

either return the a2 temporary or pass a1 by reference (which doesn't need a
return).

void Ya(type& a1)
{
a1 = 10;
}

a1 is an alias for the passed parameter (pass by reference)

Its equivalent to someone calling you by your name or nickname, both names
denote the same instance of person. Additionally, the relationship between
the reference and its object can't be broken in void Ya(...). The reference
is permanantly bound until the scope ends.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top