example (revised from lecture): file f.m file g.m +-------------------------+ +------------------------+ | function [a,b] = f(x,y) | | function c = g(z) | | a = x+y; b = x*y; | | [d,c] = f(z-1,z+1) | +-------------------------+ +------------------------+ run $g(2+7)$ inside command window: BIG +----------------------------------+ call g | sum cumsum prod cumprod abs diff | + search for g inside "command" scope | ones zeros rand f g ... | + not found | | + search enclosing scope: BIG scope |command | + find g in BIG scope |+----+ | || | | |+----+ | +----------------------------------+ BIG g.1: draw activation record +----------------------------------+ + since found in BIG scope | sum cumsum prod cumprod abs diff | + draw activation record in BIG scope | ones zeros rand f g ... | | | |command g | |+----+ +----+ | || | | | | |+----+ +----+ | +----------------------------------+ BIG g.2 & g.3: evaluate argument(s) +----------------------------------+ and place in parameter(s) | sum cumsum prod cumprod abs diff | | ones zeros rand f g ... | + argument $9$ evaluated inside | | "command" scope (no identifiers, |command g | so doesn't matter where run, but |+----+ +---------+ | technically, it is run inside || | | +---+ | | command scope) |+----+ | z | 9 | | | | | +---+ | | | +---------+ | +----------------------------------+ BIG g.4: run g function body: call f: +----------------------------------+ f.1: draw activation record | sum cumsum prod cumprod abs diff | + run g function body inside g's | ones zeros rand f g ... | activation record | | + $[d,c] = f(z-1,z+1)$: |command g f | + search for $d$ inside g's record |+----+ +---------+ +----+ | + not found || | | +---+ | | | | + search enclosing scope: BIG scope |+----+ | z | 9 | | +----+ | + not found | | +---+ | | + search for $c$; similar | +---------+ | + search for f inside g's +----------------------------------+ activation record + not found + search enclosing scope: BIG scope + find f in BIG scope + therefore, draw activation record for f inside BIG scope BIG g.4: f.2 & f.3: evaluate arguments +----------------------------------+ and place in parameters | sum cumsum prod cumprod abs diff | | ones zeros rand f g ... | + $z-1$, $z+1$ evaluated inside | | g's activation record |command g f | + 1st argument $z-1$ placed inside |+----+ +---------+ +----------+ | 1st parameter $x$ || | | +---+ | | +---+ | | + 2nd argument $z+1$ placed inside |+----+ | z | 9 | | | x | 8 | | | 2nd parameter $y$ | | +---+ | | +---+ | | | +---------+ | +----+ | | | | y | 10 | | | | | +----+ | | | +----------+ | +----------------------------------+ BIG g.4: f.4: run f function body: a <-- +----------------------------------+ + run f function body inside f's | sum cumsum prod cumprod abs diff | activation record | ones zeros rand f g ... | + $a = x+y$: | | + evaluate lhs $a$ |command g f | + search for $a$ inside f's |+----+ +---------+ +----------+ | activation record || | | +---+ | | +---+ | | + not found |+----+ | z | 9 | | | x | 8 | | | + search enclosing scope: BIG scope | | +---+ | | +---+ | | + not found | +---------+ | +----+ | | + evaluate $x+y$: | | y | 10 | | | + search for $x$, $y$ inside f's | | +----+ | | activation record: found! | | +----+ | | + since $a$ was not found, declare it | | a | 18 | | | inside f's activation record | | +----+ | | | +----------+ | +----------------------------------+ BIG g.4: f.4: run f function body: b <-- +----------------------------------+ | sum cumsum prod cumprod abs diff | + $b = x*y$: | ones zeros rand f g ... | + evaluate lhs $b$ | | + search for $b$ inside f's |command g f | activation record |+----+ +---------+ +----------+ | + not found || | | +---+ | | +---+ | | + search enclosing scope: BIG scope |+----+ | z | 9 | | | x | 8 | | | + not found | | +---+ | | +---+ | | + evaluate $x*y$: | +---------+ | +----+ | | + search for $x$, $y$ inside f's | | y | 10 | | | activation record: found! | | +----+ | | + since $b$ was not found, declare it | | +----+ | | inside f's activation record | | a | 18 | | | | | +----+ | | | | +----+ | | | | b | 80 | | | | | +----+ | | | +----------+ | +----------------------------------+ BIG g.4: f.5 & f.6: copy out return values +----------------------------------+ and destroy activation | sum cumsum prod cumprod abs diff | record | ones zeros rand f g ... | | | |command g | |+----+ +---------+ | || | | +---+ | | |+----+ | z | 9 | | | | | +---+ | | | +---------+ | +----------------------------------+ BIG g.4: fun g function body: +----------------------------------+ c, d <-- f return values | sum cumsum prod cumprod abs diff | | ones zeros rand f g ... | + back to $[d,c] = f(z-1,z+1)$: | | put return values into $d,c$ |command g | + earlier search did not find $d$,$c$ |+----+ +----------+ | + therefore declare (create) them, || | | +---+ | | in g's activation record |+----+ | z | 9 | | | + put values into $d$, $c$ | | +---+ | | | | +----+ | | | | d | 18 | | | | | +----+ | | | | +----+ | | | | c | 80 | | | | | +----+ | | | +----------+ | +----------------------------------+ BIG g.5 & g.6: copy out return value(s) and +----------------------------------+ destroy activation record | sum cumsum prod cumprod abs diff | | ones zeros rand f g ... | | | |command | |+----+ | || | | |+----+ | +----------------------------------+ BIG: ans <-- g return value(s) +----------------------------------+ | sum cumsum prod cumprod abs diff | | ones zeros rand f g ... | | | |command | |+------------+ | || +----+ | | || ans | 80 | | | || +----+ | | |+------------+ | +----------------------------------+ _______________________________________________________________________________ note: + you can download $strhcat.m$ (used by $boxscope$) and $boxscope.m$ to help you draw your own box scope diagram pictures + (note: don't try to read the code -- it's complicated!) + pictures above were (edited from) the output of the code below clc; clear diary on big.command = []; disp('start'); disp(boxscope(big,1)) big.g = []; disp('g.1'); disp(boxscope(big,1)) big.g.z = 9; disp('g.2 & g.3'); disp(boxscope(big,1)) big.f = []; disp('g.4: f.1'); disp(boxscope(big,1)) big.f.x = 8; big.f.y = 10; disp('g.4: f.2 & f.3'); disp(boxscope(big,1)) big.f.a = 18; disp('g.4: f.4: a <--'); disp(boxscope(big,1)) big.f.b = 80; disp('g.4: f.4: b <--'); disp(boxscope(big,1)) big=rmfield(big,'f'); disp('g.4: f.5 & f.6'); disp(boxscope(big,1)) big.g.d = 18; big.g.c = 80; disp('g.4: c, d <--'); disp(boxscope(big,1)) big=rmfield(big,'g'); disp('g.5 & g.6'); disp(boxscope(big,1)) big.command.ans = 80; disp('ans <--'); disp(boxscope(big,1)) diary off % file $diary$ now should contain reasonably nice pictures