answersLogoWhite

0

Consider the code that follows. In the main() function we declare an integer, num, and initialise it to the value 100. We then call three separate functions which accept num as an argument, first by value, then by reference, and finally by pointer. In each case we double the value of the parameter to show the effect it has upon num itself.

Note how the PassByValue function makes a copy of num. We know this because the parameter val has a completely different memory address to that of num. This means that anything we do to val will not affect num.

However PassByRef passes num directly because refis a reference to num (they both refer to the same memory address). Thus anything we do to ref we actually do to num. Thus num is doubled when we double ref.

Finally, PassByPtr proves that pointers are just variables that are passed by value, because ptr has its own memory address allocated to it. The value stored in this memory address is the memory address of num. Dereferencing this memory address shows us the value of num. Thus doubling the dereferenced value of ptr doubles the value of num.

#include

void PassByVal( int val )

{

printf( "PassByVal(int val)\taddress of val: 0x%.8x, value of val: %d\n" , &val, val );

val *= 2;

}

void PassByRef( int & ref )

{

printf( "PassByRef(int & ref)\taddress of ref: 0x%.8x, value of ref: %d\n" , &ref, ref );

ref *= 2;

}

void PassByPtr( int * ptr )

{

printf( "PassByPtr(int * ptr)\taddress of ptr: 0x%.8x, value of ptr: 0x%.8x, dereferenced value of ptr: %d\n" , &ptr, ptr, *ptr );

*ptr *= 2;

}

int main()

{

int num = 100;

printf( "main()\t\t\taddress of num: 0x%.8x, value of num: %d\n" , &num, num, );

PassByVal( num );

printf( "main()\t\t\taddress of num: 0x%.8x, value of num: %d\n" , &num, num );

PassByRef( num );

printf( "main()\t\t\taddress of num: 0x%.8x, value of num: %d\n" , &num, num );

PassByPtr( &num );

printf( "main()\t\t\taddress of num: 0x%.8x, value of num: %d\n" , &num, num );

}

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

What are the different parameter passing methods used by c plus plus?

Pass by value, constant value, reference and constant reference. Pass by value is the default in C++ (pass by reference is the default in Java).


Which is the default parameter passing technique in c plus plus language?

The default is to pass by value.


If you got a c plus and you need a c do you pass?

Yes, if you need a C to pass, and you have a C+, you pass.


Can a variable name can be passed to a value parameter in c plus plus?

No. Pass by value always receives a copy of the value being passed. Even if it were possible to physically pass a user-defined identifier into a function by value, the compiled code would not recognise the name since all identifiers are stripped out by the compiler and replaced with memory addresses. Strictly speaking, even pass by reference does not pass the variable name, as the function argument is simply an alias, an alternate but informal name, for the formal name you actually pass. In essence you are passing the memory address of the variable, rather the value of the memory address as you would with pass by value.


What is the value of a b and c in the equation ax2 plus bx plus c?

any number


What value of c makes 4x2 plus 16 plus c a perfect square?

16


How do you save a radian value in c plus plus?

You can write it into a file as a floating-point value.


Is C higher than Pass plus in dancing?

yes it is


In C plus plus all false relational expressions have a mathematical value of?

In C++ all false relational expressions have a mathematical value of 0.


True or False A C plus plus class constructor cannot return a function value?

True - A C++ constructor cannot return a value.


Would you Write c plus plus program using array for Fibonacci number?

You can write a C++ fib pro using arrays but the problem is the prog becomes very complicated since u need to pass the next adding value in an array.....


Why you pass arguments to function in c plus plus?

You pass arguments to functions because that is how you tell the function what you want it to do. If you had, for instance, a function that calculated the square root of something, you would pass that something as an argument, such as a = sqrt (b). In this case sqrt is the function name, b is passed as its argument, and the return value is assigned to a.