Explain "passing by value", "passing by pointer" and
"passing by reference" ?

Answer Posted / ranjeet garodia

Pass by value - a copy is made

Pass by pointer ( explicit pointer)
example:
void func(int * ptr_sent)
main()
{
int i;
int *p;
p = &i;
func(p);
}

void func(int * ptr_sent)
{
*ptr_sent = *ptr_sent + 2
// adds 2 to the value in location pointed by ptr_sent
}

answers given by yen .. there is one error ... in pass by
reference .. when calling function pass the variable not
the address....fun(i) should be called instead of fun(&i)

Pass by reference (implicit pointer)
example:
void func(int &ref_sent)
main()
{
int i;
func(i); // call by reference
}

void func(int &ref_sent)
{
ref_sent = ref_sent + 2
// adds 2 to the ref_sent
// Please note that you do not need * when using reference
// Any code manipulating reference reflects changes on i
}

Is This Answer Correct ?    1 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can the creation of operator** is allowed to perform the to-the-power-of operations?

777


List the issue that the auto_ptr object handles?

823


What is constant in c++ with example?

862


What is array give example?

789


What is basic if statement syntax?

787


What ANSI C++ function clears the screen a) clrscr() b) clear() c) Its not defined by the ANSI C++ standard

805


Does c++ support multilevel and multiple inheritances?

765


Explain how would you handle a situation where you cannot call the destructor of a local explicitly?

750


What is the difference between object-oriented programming and procedural programming?

910


State the difference between delete and delete[].

808


By using c++ with an example describe linked list?

807


What is a multiset c++?

793


What is a namespace in c++?

1445


What is the equivalent of Pascal's Real a) unsigned int b) float c) char

795


What is the difference between #import and #include in c++?

841