answersLogoWhite

0


Best Answer

#include<iostream>

void byval(int y)

{

std::cout<<"byval: &y="<<&y<<std::endl;

}

void byref(int& z)

{

std::cout<<"byref: &z="<<&z<<std::endl;

}

int main()

{

int x=42;

std::cout<<"main : &x="<<&x<<std::endl;

byval(x);

byref(x);

return(0);

}

Example output:

main : &x=001DF800

byval: &y=001DF72C

byref: &z=001DF800

When you pass by value, the y argument has a different memory address to that of x. In other words, y is a copy of x (you pass the value of x, not x itself). Thus any changes made to y will have no affect upon x. Often this is desirable, but note that when passing a complex object by value, you will automatically invoke the object's copy constructor, which is often unnecessary. Pass by value should only be used when the function needs to modify the argument's value, but without affecting the argument that was passed.

When you pass by reference, the z argument has the same address as x. Thus z refers to x. In other words, z is an alias, an alternate name, for x, thus any changes made to the value of z will also change the value of x, since they are one and the same variable. If the function does not actually need to alter the value of the reference, then the reference should be declared constant. This assures the caller that the object's immutable members will not be altered by the function.

Note that you can also achieve pass by reference by passing a pointer to the variable. Pointers are always passed by value, thus the pointer will actually be a copy of the pointer you pass, but both pointers will still refer to the same memory address, thus the net effect is a pass by reference. However, if a pointer is guaranteed to be non-NULL, then it is more efficient to pass the address being pointed at by reference, since references can never be NULL (a NULL reference will invalidate your program).

To pass a pointer by reference, you need to add an extra level of indirection. Thus when passing a pointer to an int (int*), you need to pass a pointer to a pointer to int instead (int**). The pointer to pointer is passed by value, but the pointer to int is effectively passed by reference, thus allowing you to change the value of both the pointer and also what it is pointing at (unless one, the other, or both, are declared const).

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

The c++ concept of polymorphism can be illustrated without the use of functions.Eg:


#include

using namespace std;


int sum( int a, int b){

return a+b;

}


void sum(int a, int b, int &c){

c = a+b;

}


int main(){

int a, b, c;

cout<<"Enter the two numbers:\t";

cin>>a>>b;

c = sum(a+b);

cout<<"Sum = "<

sum(a,b,c);

cout<<"Sum = "<

return 0;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a c plus plus program to demonstrate call by value and call by reference?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a program using recursion which should take two values and display 1st value raised to the power of second value?

Write a program using recursion which should take two values and display 1st value raised to the power of second value.


Write a program using 8086 instructions to double a number?

There isn't a reason to write a complete program to do this; in any assembly language just shift the value 1 bit to the left to double it.


What is calling by reference how it is different from call by value in c program?

Calling a function by value means the variable will be copied. That means that, any changes you make to the variable will be applied to the copy, and not the real one. If you pass by reference, the actual intended variable is modified.


Write a java script program to print first ten odd natural numbers in C?

Q.1 Write a program to print first ten odd natural numbers. Q.2 Write a program to input a number. Print their table. Q.3 Write a function to print a factorial value.


A c program to square matrix?

A C program to square matrix is a math problem. In the math problem you write down all the outer boundary values of matrix in a circle, then write down the inner value.


How do you write a c program which calculates the value of money at the end of each year of investment?

Reference:cprogramming-bd.com/c_page3.aspx#calculates the value of money


Where the Memory is allocated for a variable in a program?

if a variable is of value type memory is allocated on stack memory.. if it is of reference type,memory is allocated on heap memory..


Why pointers are passed by reference?

It is by-value, actually, not by-reference.


What do you call an unchanging value in a formula in excel?

It can be called a constant or fixed value. If it is not a value but a cell reference then it can be called an absolute reference.


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).


How do you write a c program to find square of a no using call by value?

int square (int N) return N*N;


What is the value of a 30.06 rifle reference?

Entirely dependent on what the maker reference is.