Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
added some pass by reference and value examples in C++ but need the a…
…ll clear first. TODO: come back to this after the sign off.
- Loading branch information
Showing
6 changed files
with
108 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env python3 | ||
# example-2.py | ||
# showing pass by reference | ||
var = 20 | ||
print("Initial variable value and object number:") | ||
print(var) | ||
print(id(var)) | ||
|
||
def pass_by_reference(): | ||
global var | ||
print("Initial ") | ||
print(id(var)) | ||
var += 20 | ||
print(var) | ||
print(id(var)) | ||
|
||
pass_by_reference() | ||
print(var) | ||
print(id(var)) | ||
|
||
def pass_by_value(var): | ||
total = var | ||
total = total + 20 | ||
print(total) | ||
print(id(total)) | ||
return total | ||
|
||
pass_by_value(var) | ||
print(var) | ||
print(id(var)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#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"; | ||
} |
Binary file not shown.