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 );
}
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).
The default is to pass by value.
Yes, if you need a C to pass, and you have a C+, you pass.
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.
any number
16
You can write it into a file as a floating-point value.
yes it is
In C++ all false relational expressions have a mathematical value of 0.
True - A C++ constructor cannot return a value.
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.....
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.