Permalink
Cannot retrieve contributors at this time
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?
py-quickstart/docs/functions/user-functions/example-pass-by.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
42 lines (35 sloc)
979 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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"; | |
} |