Skip to content
Permalink
7ae7d3de3a
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
42 lines (35 sloc) 979 Bytes
#include <iostream>
using namespace std;
// C++ prototypes
void pass_by_reference(int &var_1);
void pass_by_value(int var_1);
// example-pass-by.cpp
// Illustrating pass by reference and value
// using C++
// Function declarations
void pass_by_reference(int &var_1)
{
var_1 = var_1 + 5;
cout << var_1 << "\n";
}
void pass_by_value(int var_1)
{
var_1 = var_1 + 10;
cout << var_1 << "\n";
}
// Program entry point
int main ()
{
int var_1 = 20;
cout << "The initial value of var_1 is: " << var_1 << "\n";
// This preserves the memory address and it
// will keep the value.
cout << "This is after it's passed by reference:" << "\n";
pass_by_reference(var_1);
cout << var_1 << "\n";
// This doesn't preserve the memory address
// and passes a copy of the value
cout << "This is after it's passed by Value." << "\n";
pass_by_value(var_1);
cout << var_1 << "\n";
}