Skip to content
Snippets Groups Projects
Commit 9f11ffa3 authored by Marcus Mohr's avatar Marcus Mohr
Browse files

Notebook demonstrating the two different ways of parameter passing

parent 99809ce8
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:e7ff4eaa-d800-41b0-b8f9-541d4f8dea11 tags:
# Call-by-Value versus Call-by-Reference
%% Cell type:code id:1259a4b8-c4e1-4417-ae0f-fc82ca8bba04 tags:
``` C++17
#include <iostream>
```
%% Cell type:code id:7a9f9c64-4826-41bf-9ae0-694127544a0a tags:
``` C++17
void swap( int aLoc, int bLoc ) {
int aux = aLoc;
std::cout << "-> in swap (1): (" << aLoc << "," << bLoc << ")\n";
aLoc = bLoc;
bLoc = aux;
std::cout << "-> in swap (2): (" << aLoc << "," << bLoc << ")\n";
}
```
%% Cell type:code id:628f629d-eaf8-4523-bd3a-c63dc02fde81 tags:
``` C++17
int main( void ) {
int a = 1;
int b = 5;
std::cout << "in main before swap: (" << a << "," << b << ")\n";
swap( a, b );
std::cout << "in main after swap: (" << a << "," << b << ")\n";
}
```
%% Cell type:code id:347cce26-5b83-45a9-b951-8db56165683d tags:
``` C++17
main();
```
%% Output
in main before swap: (1,5)
-> in swap (1): (1,5)
-> in swap (2): (5,1)
in main after swap: (1,5)
%% Cell type:markdown id:2ab255de-8324-4e09-b623-c1cb28b28089 tags:
#### Call-by-Value
- Different strategies exist for passing parameters to subprograms.
- The C language uses a technique denoted as $\color{blue}{\text{call-by-value}}$.
- The formal parameters in the interface of a function
- are local variables existing only in the scope of the function
- when the function is called the values of the **arguments are
copied into the local variables**
- Changes to local variables do not affect arguments.
%% Cell type:markdown id:d2d308f1-219f-46ae-961e-b193787e02ea tags:
#### Call-by-Reference
- A well-know alternative strategy is denoted as $\color{red}{\text{call-by-reference}}$.
- This is the variant used in the Fortran language family.
- The formal parameters in the interface of a function
- are only placeholders (dummy parameters)
- when the function is called they are replaced by memory addresses of the actual arguments
- Changes to formal parameters do affect the arguments
%% Cell type:markdown id:e4413584-2f76-44ef-982d-c067bedf4a2c tags:
In C/C++ we can *emulate* call-by-reference with the help of pointers
%% Cell type:code id:a5dea161-dffb-46d0-8146-523d5b340912 tags:
``` C++17
void swapEm( int* ap, int* bp ) {
int aux = *ap;
std::cout << "-> in swap (1): (" << *ap << "," << *bp << ")\n";
*ap = *bp;
*bp = aux;
std::cout << "-> in swap (2): (" << *ap << "," << *bp << ")\n";
}
```
%% Cell type:code id:16ec48e9-83af-4126-82e8-6eabae87b057 tags:
``` C++17
int main( void ) {
int a = 1;
int b = 5;
std::cout << "in main before swap: (" << a << "," << b << ")\n";
swapEm( &a, &b );
std::cout << "in main after swap: (" << a << "," << b << ")\n";
}
```
%% Cell type:code id:33c5b821-9e19-4660-9ab5-a2e4e7ebeb37 tags:
``` C++17
main();
```
%% Output
in main before swap: (1,5)
-> in swap (1): (1,5)
-> in swap (2): (5,1)
in main after swap: (5,1)
%% Cell type:markdown id:08427a4a-65a8-4171-9c1d-e445cec1593f tags:
Now working with pointers means we always need to do perform addresscomputations and dereferencing to work with the arguments.
In C++ we can directly use call-by-reference using references
%% Cell type:code id:0b0a1432-7b59-4d16-ac98-24780094db21 tags:
``` C++17
void swapCPP( int& aRef, int& bRef ) {
int aux = aRef;
std::cout << "-> in swap (1): (" << aRef << "," << bRef << ")\n";
aRef = bRef;
bRef = aux;
std::cout << "-> in swap (2): (" << aRef << "," << bRef << ")\n";
}
```
%% Cell type:code id:d1334cfa-4559-4320-8fc9-9ec588544c4a tags:
``` C++17
main();
```
%% Output
in main before swap: (1,5)
-> in swap (1): (1,5)
-> in swap (2): (5,1)
in main after swap: (5,1)
%% Cell type:markdown id:7155c4c4-2b9b-452e-8e97-db5aaebb806c tags:
### Language Overview:
%% Cell type:markdown id:87403469-d884-4c95-a100-40b8d5de334d tags:
- The languages currently most often used in Computational Science use different approaches
| language | supported mechanics |
|:--------:|-----------------------------------|
| C | call-by-value |
| Fortran | call-by-reference |
| C++ | call-by-value & call-by-reference |
| Python | call-by-sharing/object/assignment |
- Python: everything is an object, either mutable or immutable and no variables exist (only names)
- References in C++ cannot be copied
%% Cell type:code id:b8f3e2f7-5659-4d4b-aa3c-73a879eb0caa tags:
``` C++17
#include <vector>
// create a vector of ints
std::vector< int > a = {1,3,7,9};
```
%% Cell type:code id:211a4a4d-83e1-4ed1-9a20-62b099df03d4 tags:
``` C++17
a
```
%% Output
{ 1, 3, 7, 9 }
%% Cell type:code id:acdec801-802f-4693-91f7-25deb3f87f13 tags:
``` C++17
a[0] = -1; // change first element by direct access
a.push_back( 11 ); // append element at the end
a
```
%% Output
{ -1, 3, 7, 9, 11 }
%% Cell type:code id:e2ffe5d4-e4f9-4fff-956e-ae35238b2d96 tags:
``` C++17
#include <iostream>
#include <vector>
void test() {
// note that the std::vector stores copies
int val = 10;
std::vector< int > foo;
foo.push_back( val );
foo[0] = 2;
std::cout << foo[0] << std::endl;
std::cout << val;
}
test()
```
%% Output
2
10
%% Cell type:code id:41e83505-e0da-4251-97c5-34c0b7f9b3fa tags:
``` C++17
// create a vector of pointers to int
std::vector< int* > ptrVec;
// try to create a vector of references to int
// std::vector< int& > fail;
```
%% Cell type:code id:8555c6be-c957-46ed-9705-3a96d8349a36 tags:
``` C++17
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment