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

Mention the purpose of istream class?

857


How a new operator differs from the operator new?

907


What are default parameters? How are they evaluated in c++ function?

934


What things would you remember while making an interface?

794


What causes a runtime error c++?

855


Where do I find the current c or c++ standard documents?

857


What do you mean by function overriding & function overloading in c++?

827


Why can’t you call invariants() as the first line of your constructor?

798


Evaluate as true or false: !(1 &&0 || !1) a) True b) False c) Invalid statement

873


What does namespace mean in c++?

861


what is scupper?

2123


Which software is used to run c++ program?

763


What does new in c++ do?

776


What is general form of pure virtual function? Explain?

762


Which programming language is best?

778