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

What is increment operator in c++?

554


Explain the benefits of proper inheritance.

635


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

577


Write about an iterator class?

606


Assume studentNames and studentIDs are two parallel arrays of size N that hold student data. Write a pseudocode algorithm that sorts studentIDs array in ascending ID number order such that the two arrays remain parallel.

1710






How would you use the functions sin(), pow(), sqrt()?

736


Can the operator == be overloaded for comparing two arrays consisting of characters by using string comparison?

556


What does catch(…) mean?

607


What is a vector c++?

545


Function can be overloaded based on the parameter which is a value or a reference. Explain if the statement is true.

641


What are register variables?

638


What is a list c++?

566


what is scupper?

1888


What are signs of manipulation?

580


What is c++ programming language?

570