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

Answer Posted / ven

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
}

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

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 ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is c++ hiding?

627


A company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9 percent of their gross sales for that week. For example, a saleperson who sells $5000 worth of merchandise in a week receives $200 plus 9 percent of $5000, or a total of $650. You have been supplied with a list of items sold by each salesperson. The values of these items are as follows: Item Value A 239.99 B 129.75 C 99.95 D 350.89 Write a program that inputs one salesperson's items sold in a week (how many of item A? of item B? etc.) and calculates and displays that salesperson's earnings for that week.

3419


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

593


What is ios :: in in c++?

652


Explain the concept of friend function in c++?

613






What are the important differences between c++ and java?

617


Why is c++ a mid-level programming language?

603


Is c++ faster than c?

606


Which programming language's unsatisfactory performance led to the discovery of c++?

822


Where can I run c++ program?

610


write a corrected statement in c++ so that the statement will work properly. x = y = z + 3a;

1466


State the difference between pre and post increment/decrement operations.

616


What is the use of setfill in c++?

598


What is a pointer with example?

669


What are proxy objects in c++?

651