This C++ program illustrates how to pass a variable to a function by value. The value of the parameter can not be changed by using this method since we do not pass the variable’s address by either referencing operator (&) or using pointer.
Here is the source code of the C++ program illustrates how to pass a variable to a function by value. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Illustrate Pass by Value
*/
#include <iostream>
void passByValue(int data)
{
std::cout << "Value of data in passByValue( ) is "
<< data << std::endl;
data = 20;
std::cout << "Value of data is changed now to "
<< data << std::endl;
return;
}
int main()
{
int data = 10;
std::cout << "Value of data is "
<< data << std::endl;
/* Variable data passed by value */
passByValue(data);
/* Value of data is still 10 */
std::cout << "Value after calling passByValue() is "
<< data;
return 0;
}
$ a.out Value of data is 10 Value of data in passByValue( ) is 10 Value of data is changed now to 20 Value after calling passByValue( ) is 10
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Practice Computer Science MCQs
- Apply for C++ Internship
- Practice Programming MCQs
- Check C++ Books
- Check Programming Books