Skip to content
Permalink
Browse files

Added slideshows for call-by-value and call-by-reference

  • Loading branch information...
CorySanin committed Jun 3, 2018
1 parent 7e70b38 commit 051b22d8ed0d3f60c76dc031d1e8447eaed79073
@@ -0,0 +1,116 @@
$(document).ready(function() {
"use strict";
var av_name = "paramPassingByRef";

var av = new JSAV(av_name);

// Relative offsets
var leftMargin = 50;
var topMargin = 4;

av.umsg("main() begins execution.");

var pseudo = av.code(["void foo(int r, int s){",
" s = r;",
" g = 3;",
" r = a[g];",
" a[g] = a[2];",
"}",
"int main() {",
" int g = 2;",
" foo(g, a[g]);",
"}"],
{left: leftMargin, top: topMargin, lineNumbers: false});

av.label("g", {left: leftMargin+220, top: topMargin + 4});
var gVar = av.ds.array([0], {indexed: false, left: leftMargin+235, top: topMargin});
av.label("a", {left: leftMargin+220, top: topMargin+36});
var aArray = av.ds.array([-9,-1,-6,4,10],
{indexed: true, left: leftMargin+235, top: topMargin+32});

av.label("main", {left: leftMargin+220, top: topMargin+80});
av.g.rect(leftMargin+220, topMargin+122, 150, 40);

pseudo.setCurrentLine(7);

av.displayInit();
av.step();

av.umsg("main's g is initialized to 2.");

pseudo.setCurrentLine(8);
av.label("g", {left: leftMargin+225, top: topMargin+116});
var mainGVar = av.ds.array([2], {indexed: false, left: leftMargin+240, top: topMargin+112});

av.step();

av.umsg("foo is called, with a reference to main's g and a[2] passed in.",{preserve: false});
pseudo.setCurrentLine(9);

av.step();

pseudo.setCurrentLine(1);

/*var fooBoxLbl = av.label("foo", {left: leftMargin+375, top: topMargin+68});
var fooBox = av.g.rect(leftMargin+375, topMargin+110, 150, 40);*/

var rPointer = av.pointer('r', mainGVar,
{targetIndex:0, top:68, right:42, arrowAnchor:"center bottom"});
var sPointer = av.pointer('s', aArray,
{targetIndex:2, bottom:68, left:32});

av.step();

av.umsg("a[2] is set to the value of main's g.");
pseudo.setCurrentLine(2);

aArray.value(2, mainGVar.value(0));
aArray.highlight(2);
mainGVar.highlight(0);

av.step();

av.umsg("g is set to 3.");
pseudo.setCurrentLine(3);

mainGVar.unhighlight(true);
aArray.unhighlight(true);
gVar.highlight(0);
gVar.value(0,3);

av.step();

av.umsg("main's g is set to the value of a[3].");
pseudo.setCurrentLine(4);

gVar.unhighlight(true);
mainGVar.highlight(0);
aArray.highlight(3);
mainGVar.value(0, aArray.value(3));

av.step();

av.umsg("a[3] is set to the value of a[2].");
pseudo.setCurrentLine(5);

mainGVar.unhighlight(true);
aArray.unhighlight(true);
aArray.highlight([2,3]);
aArray.value(3,aArray.value(2));

av.step();

av.umsg("In this context, r = 4, s = 2, and g = 3.");

aArray.unhighlight(true);

av.step();

av.umsg("In this context, g = 4. The values stored in array a can be seen below.");
pseudo.setCurrentLine(9);

rPointer.hide();//makes the slideshow area super tall
sPointer.hide();//don't know why

av.recorded();
});
@@ -0,0 +1,119 @@
$(document).ready(function() {
"use strict";
var av_name = "paramPassingByVal";

var av = new JSAV(av_name);

// Relative offsets
var leftMargin = 50;
var topMargin = 4;

av.umsg("main() begins execution.");

var pseudo = av.code(["void foo(int r, int s){",
" s = r;",
" g = 3;",
" r = a[g];",
" a[g] = a[2];",
"}",
"int main() {",
" int g = 2;",
" foo(g, a[g]);",
"}"],
{left: leftMargin, top: topMargin, lineNumbers: false});

av.label("g", {left: leftMargin+220, top: topMargin + 4});
var gVar = av.ds.array([0], {indexed: false, left: leftMargin+235, top: topMargin});
av.label("a", {left: leftMargin+220, top: topMargin+36});
var aArray = av.ds.array([-9,-1,-6,4,10],
{indexed: true, left: leftMargin+235, top: topMargin+32});

av.label("main", {left: leftMargin+220, top: topMargin+80});
var mainBox = av.g.rect(leftMargin+220, topMargin+122, 150, 74);

pseudo.setCurrentLine(7);

av.displayInit();
av.step();

av.umsg("main's g is initialized to 2.");

pseudo.setCurrentLine(8);
av.label("g", {left: leftMargin+225, top: topMargin+116});
var mainGVar = av.ds.array([2], {indexed: false, left: leftMargin+240, top: topMargin+112});

av.step();

av.umsg("foo is called, with a copy of main's g and a[2] passed in.",{preserve: false});
pseudo.setCurrentLine(9);

av.step();

pseudo.setCurrentLine(1);

var fooBox = av.g.rect(leftMargin+375, topMargin+122, 150, 74);
var fooBoxLbl = av.label("foo",
{relativeTo:fooBox, anchor:"left top", myAnchor:"left bottom", left:0, bottom:40})

var rVarLbl = av.label("r",
{relativeTo:fooBox, anchor:"left top", myAnchor:"left top", left:5, top:10});
var rVar = av.ds.array([2],
{relativeTo:rVarLbl, anchor:"right center", myAnchor:"left center", indexed:false, left:8, top:8});
var sVarLbl = av.label("s",
{relativeTo:rVarLbl, anchor:"center bottom", myAnchor:"center top", left:0, top:12});
var sVar = av.ds.array([-6],
{relativeTo:sVarLbl, anchor:"right center", myAnchor:"left center", indexed:false, left:8, top:8});


av.step();

av.umsg("s is set to the value of r.");
pseudo.setCurrentLine(2);

sVar.value(0, rVar.value(0));
sVar.highlight(0);
rVar.highlight(0);

av.step();

av.umsg("g is set to 3.");
pseudo.setCurrentLine(3);

sVar.unhighlight(true);
rVar.unhighlight(true);
gVar.highlight(0);
gVar.value(0,3);

av.step();

av.umsg("r is set to the value of a[3].");
pseudo.setCurrentLine(4);

gVar.unhighlight(true);
rVar.highlight(0);
aArray.highlight(3);
rVar.value(0, aArray.value(3));

av.step();

av.umsg("a[3] is set to the value of a[2].");
pseudo.setCurrentLine(5);

rVar.unhighlight(true);
aArray.unhighlight(true);
aArray.highlight([2,3]);
aArray.value(3,aArray.value(2));

av.step();

av.umsg("In this context, r = 4, s = 2, and g = 3.");

aArray.unhighlight(true);

av.step();

av.umsg("In this context, g = 2. The values stored in array a can be seen below.");
pseudo.setCurrentLine(9);

av.recorded();
});
@@ -3,7 +3,7 @@
.. Copyright (c) 2012-13 by the OpenDSA Project Contributors, and
.. distributed under an MIT open source license.
.. avmetadata::
.. avmetadata::
:author: David Furcy and Tom Naps


@@ -25,11 +25,11 @@ Parameter-passing techniques may be broken down as follows:

- Lazy evaluation (normal order) techniques

- Macro expansion
- Macro expansion

- Call-by-name
- Call-by-name

- Call-by-need
- Call-by-need

The difference between call-by-value and call-by-reference is
exemplified by the difference between denoted values in our
@@ -38,7 +38,27 @@ argument for a function parameter is a copy of the value of the
argument whereas, in call-by-reference, the function is given the
address of the argument. Given the address, the function has the
capability of modifying the argument.


Consider the following code:

.. codeinclude:: ParamPassing/ParamPassing

If the parameter passing method call-by-value is used...

.. inlineav:: paramPassingByVal ss
:long_name: Parameter Passing By Value
:links:
:scripts: AV/PL/paramPassingByVal.js
:output: show

If the parameter passing method call-by-reference is used...

.. inlineav:: paramPassingByRef ss
:long_name: Parameter Passing By Reference
:links:
:scripts: AV/PL/paramPassingByRef.js
:output: show

This problem will help you review the difference between *call by
value* and *call by reference* in C++, where the presence of an
ampersand in front of the parameter's name is used to indicate
@@ -72,7 +92,7 @@ the following C++ code.

#include <iostream>
using namespace std;

void by_value(int a, int b) {
a = b;
b = 6;
@@ -84,9 +104,9 @@ the following C++ code.
void by_copy_restore(int &a, int &b) {
int copya, copyb;
copya = a; // copy-in phase
copyb = b;
copyb = b;
copya = copyb; // function proper
copyb = 6;
copyb = 6;
a = copya; // copy-out phase
b = copyb;
}
@@ -189,7 +209,7 @@ it is actually used in the body of the macro.
}


**Implementation of macro-expansion in C++**
**Implementation of macro-expansion in C++**

One possible implementation of macro-expansion is to perform a double
textual substitution. For example, the C++
@@ -206,15 +226,15 @@ following code.

::

#include <iostream>
#include <iostream>

using namespace std;
using namespace std;

int n = 6;
int n = 6;

#define dynamic_scoping { cout << n << endl; }
#define dynamic_scoping { cout << n << endl; }

void static_scoping() { cout << n << endl; }
void static_scoping() { cout << n << endl; }

void test_dynamic() {
int n = 5;
@@ -232,7 +252,7 @@ following code.
test_dynamic();
test_static();
}



This problem will help you review the differences among *call by
@@ -288,4 +308,3 @@ depth in the context of a specific example known as a *lazy list*.

.. .. avembed:: Exercises/PL/RP31part2.html ka
.. :long_name: RP set #31, question #2
@@ -0,0 +1,14 @@
int g = 0;
int a[5] = { -9, -1, -6, 4, 10 };

void foo(int r, int s){
s = r;
g = 3;
r = a[g];
a[g] = a[2];
}

int main() {
int g = 2;
foo(g, a[g]);
}

0 comments on commit 051b22d

Please sign in to comment.
You can’t perform that action at this time.