06.07.2013 Views

Scilab Bag Of Tricks - Claymore

Scilab Bag Of Tricks - Claymore

Scilab Bag Of Tricks - Claymore

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

a =<br />

1.<br />

->bar(3.5)<br />

ans =<br />

2.<br />

->bar(-1)<br />

ans =<br />

2.<br />

->a = 2<br />

a =<br />

2.<br />

->bar(-1)<br />

ans =<br />

3.<br />

Now what is the “enclosing scope”? It is the call stack, <strong>Scilab</strong> scopes dynamically!<br />

Example 2-5. Dynamic Scoping<br />

// scoping in <strong>Scilab</strong><br />

deff(’first_local()’, ’x = ""foo"", second()’);<br />

deff(’first()’, ’second()’);<br />

deff(’second()’, ’disp(x)’);<br />

x = 1;<br />

first_local() // prints ’foo’<br />

first() // prints 1<br />

Chapter 2. Pitfalls<br />

Example 2-5 deserves a close look. Dynamic scoping can be confusing for people used to e.g. C’s<br />

lexically scoped auto variables.<br />

/* lexical scoping in C */<br />

void first_local(void);<br />

void first(void);<br />

void second(void);<br />

int x = 1;<br />

int<br />

main(void)<br />

{<br />

first_local(); /* prints 1 */<br />

first(); /* prints 1 */<br />

return 0;<br />

24

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!