Eldiridge Academic Library
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Lesson 19

Go down

Lesson 19  Empty Lesson 19

Post  Admin Thu Oct 11, 2012 11:14 am

[Only admins are allowed to see this link]





Static Scope = which X are we talking about? The closest enclosing one that you can see.

* Compilers knows where the variables are at. I.e. how many steps do I need to get out of the current record to see if that is the "x" variable you are referring to.
* Procedure => Static Link => Difference between Dynamic? Static points to thing that defined it. A procedure might be called from many different places.

Dynamic Scope =

Scoping itself is how you search for a variable with a given name. A variable has a scope which is the whole area in which that variable can be accessed by name. If there is a reference to a variable "a" then how does the compiler or interpreter find it?

In lexical scoping (and if you're the interpreter), you search in the local function (the function which is running now), then you search in the function (or scope) in which that function was defined, then you search in the function (scope) in which that function was defined, and so forth. "Lexical" here refers to text, in that you can find out what variable is being referred to by looking at the nesting of scopes in the program text.

In dynamic scoping, by contrast, you search in the local function first, then you search in the function that called the local function, then you search in the function that called that function, and so on, up the call stack. "Dynamic" refers to change, in that the call stack can be different every time a given function is called, and so the function might hit different variables depending on where it is called from.

Dynamic scoping is useful as a substitute for globally scoped variables. A function can say "let current_numeric_base = 16; call other functions;" and the other functions will all print in hexadecimal. Then when they return, and the base-setting function returns, the base will return to whatever it was. SOURCE: [Only admins are allowed to see this link]


Code:
Function crust(pi)
    pi * areaOfCircle(6)


Code:
Constant pi = 3.1415
Function circumferenceOfCircle(r) is
    2 * pi * r;

Function areaOfCircle(r)
    pi * r * r;

What is the weakness of Dynamic Scoping in the code above?

You need to know every name that exists anywhere else in the code. Modularity and information hiding is gone. Why is this useful? You don't need to write as much code. Saves code writing and is more efficient. Can two people who are writing two separate functions that are picking for names used locally for those two functions, can they pick those names independently without coordinating. With dynamic scoping, you have to coordinate and this will lead to problems.

* Name Space pollution. One person is going to be able to use the variable x.... everyone else will have to use something else.





Call-By-Value

* consider a procedure with a parameter:
Code:
proc test(x) is
    output(x);

* what happens when it is called?
Code:
test(5 + 3); // outputs 8.

Step1: evaluate argument (5 + 3 -> 8 )
Step2: Pass value of argument to procedure. what got passed was 8. what didn't get passed was the expression 5 + 3.
Step3: Inside procedure, allocate new variable x.
Step4: Execute body of procedure. (prints 8 ).
Step5: De allocate new variable x.

Known as call-by-value.
Default rule in BS, Scala, Java, and ADA.
* In Ada, this is the IN parameter

call-by-value
* what does this program print?
Code:
var x := 10;
proc increment(x) is
    x:= x + 1; [color=green]// crux of problem, it doesn't know which x you are referring to.[/color]
increment(x);
output(x);

* It would print 10, not 11.

Outside Parameters
* in call-by-value, changes to parameters invisible outside procedure.
* but sometimes we want changes visible!
* use call-by-reference instead
proc increment(ref x ) is // ref means call by reference
x := x + 1;
[NOT FINISHED]

Output parameters
* in call-by-value, changes to parameters insibile outside procedure
* but sometimes we want changes visible

Call-By-Reference
Now program prints 11

Code:
var x:= 10;
proc increment( ref x) is
    x := x _ 1;
increment(x); //passes memory location, not its value.
output(x);

Call-By-Reference
Step1: evaluate the address of argument
Step2: pass the address of the procedure
Step3: Inside procedure, create new variable with the same address as the argument.

Admin
Admin
Admin

Posts : 9
Points : 24
Join date : 2012-10-10

https://metisscrolls.board-directory.net

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum