Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack.
-
Upload
armani-mayfield -
Category
Documents
-
view
224 -
download
4
Transcript of Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack.
Module Invocation & Parameters Tracing the Execution of Instructions
with the Activation Stack
Module Invocation & Parameters
As Modules are Invoked
algorithm Nonsense// dummy alg to show calls
this_var isoftype Num this_var <- 6 this_var <- Square(this_var) Output_Results(this_var)
endalgorithm // Nonsense
• Use parameters to specify which of your variables you wish to have communicate with the subroutine you are calling.
• In this case, this_var is being passed first to function Square, and then is passed to procedure Output_Results.
As Modules are Declared
function Square returnsa Num (the_num isoftype in Num)
// returns the square of the_num Square returns (the_num * the_num)endfunction
procedure Output_Results (answer isoftype in Num)// prints out answer with text message print(“The answer is”, answer)endprocedure
• Each parameter variable has two types: a parameter type and a data type
• Each function also has its own data type specifying the type of data of its result
Specifying Multiple Parameters
function Min returnsa Num (num_one, num_two,num_three isoftype in Num)
//returns smallest of three numbers if ((num_one <= num_two) AND (num_one <= num_three )) then Min returns num_one elseif ((num_two <= num_three ) AND (num_two <= num_one)) then Min returns num_two else Min returns num_three endifendfunction
Using Multiple Parameters
In use…
algorithm Example
small, medium, large, smallest isoftype Num
small <- 4
medium <- 6
large <- 34
smallest <- Min(small, medium, large)
print(“The smallest is”, smallest)
endalgorithm
Formal Parameter Lists
In the declaration header:
function Min returnsa Num (num_one,num_two,num_three isoftype in Num)
the formal parameter list (parameter variables and their types are declared here)
Actual Parameter Lists
In the call to the module:
smallest <- Min(small,medium,large)
the actual parameter list
(cannot tell what their
types are from here)
Rules for Parameter Lists
• The number of parameters in the actual and formal parameter lists must be consistent.
• Parameter association is positional: the first actual parameter matches the first formal parameter, the second matches the second, and so on.
• Actual parameters and formal parameters must be of the same data type
Information Flow Between Modules
In the calling module/algorithm (client)
Do_Something(param1, param2, param3)
Procedure Do_Something ( my_num isoftype in num, my_char isoftype in/out char, your_string isoftype out string)
In the called module (server)
actual parameters
formal parameters
Input Parameters
• Pass a copy of the original to a module• Advantage: IN parameters protect data integrity• The module that receives an input parameter can
modify only its own copy of it. It cannot modify the original, thus is safer.
• Can pass any expression as input parameters (since the original will not be changed)
• For functions, all parameters must be input parameters (no side-effects allowed)
• For procedures, use input parameters unless you have a reason to use one of the other parameter types
Output Parameters
• Output parameters are a means by which procedures may return values
• Output parameters overwrite the original values (if any) of variables given in the actual parameter list.
• Actual output parameters can not be literals, constants, or expressions – they must be variables
• The module does not know the original values of the actual parameters
• Functions cannot have outside effects, so output parameters are not permitted in functions
Input / Output Parameters
Input/Output parameters allow data to be passed both ways.
The original value of the actual parameter is accessible to the procedure.
The original value can be overwritten by the procedure.
The calling algorithm gives the procedure access to both read and overwrite the original value of the actual parameter.
In/out parameters can only be variables (so that we can write information into them).
Comparing Parameter Types
INCopies value of actual parameter into formal parameter when procedure starts; the actual parameter’s value cannot be changed, and only the module’s copy of it is modified.
OUTFormal parameter is pseudonym for actual parameter, the module writes its value into actual parameter, overwrites the actual parameter’s original value, and cannot see what data was originally stored there.
IN/OUTFormal parameter is pseudonym for actual parameter and can access its original value and writes its values into actual parameter, overwriting the original.
procedure myread (out_num isoftype ??? Num) print(“Enter a number:”) read(out_num) // out_num <- ?endprocedure
function double returnsa Num (in_num iot ??? Num) double returns 2 * in_numendfunction
algorithm Get_Numbers num_one, num_two isoftype Num myread(num_one) num_one <- double(num_one) myread(num_two) num_two <- double(num_two) print(num_one, num_two)endalgorithm
out
in
procedure double_them (num1, num2 iot ?????? Num)
num1 <- num1 * 2
num2 <- num2 * 2
endprocedure // double_them
algorithm Number_Magic
num_three, num_four isoftype Num
print (“Please enter two numbers”)
read (num_three, num_four)
double_them(num_three, num_four)
print(“Double values:”, num_three, num_four)
endalgorithm
in/out
Summary of Parameter Usage
• Use parameters for all communication between modules
• Input parameters are safe; the module gets a copy• Output parameters are used to pass values back
from a procedure– The procedure gets write access but no read
access to the original, actual parameter• Input/Output parameters give a procedure both
read and write access to the original• Functions may only have input parameters• Procedures may use all three types as needed• Use input parameter unless there is a reason to do
otherwise
procedure demo(a iot in Num, b iot in/out Num,
c iot out Num)
a <- a * 2
b <- b * 2
c <- c * 2
c <- a + b
print(a, b, c)
endprocedure
algorithm test
x, y, z isoftype Num
x <- 2
y <- 4
z <- 6
print(x, y, z)
demo(x, y, z)
print(x, y, z)
endalgorithm
Pop Quiz
LB
// ILLEGAL!
Questions?
Tracing the Execution of Instructionswith the Activation Stack
Stacks
• Is a “pile” or “collection” or “set” of items. • Items can only be added to the top• Items can only be taken off the top• “Last-in-first-out” (”LIFO”)
Thing 1
Thing 2
Thing 3Push Pop
What is a Stack?
A data structure for storing items A data structure for storing items which are to be accessed in last-which are to be accessed in last-
in first-out order (LIFO).in first-out order (LIFO).
Thing 1
PUSH
A data structure for storing items A data structure for storing items which are to be accessed in last-which are to be accessed in last-
in first-out order (LIFO).in first-out order (LIFO).
What is a Stack?
Thing 1
Thing 2
PUSH
What is a Stack?
A data structure for storing items A data structure for storing items which are to be accessed in last-which are to be accessed in last-
in first-out order (LIFO).in first-out order (LIFO).
Thing 1
pop
What is a Stack?
A data structure for storing items A data structure for storing items which are to be accessed in last-which are to be accessed in last-
in first-out order (LIFO).in first-out order (LIFO).
Thing 1
Thing 2
PUSH
What is a Stack?
A data structure for storing items A data structure for storing items which are to be accessed in last-which are to be accessed in last-
in first-out order (LIFO).in first-out order (LIFO).
Thing 1
Thing 2
Thing 3
PUSH
What is a Stack?
A data structure for storing items A data structure for storing items which are to be accessed in last-which are to be accessed in last-
in first-out order (LIFO).in first-out order (LIFO).
Thing 1
Thing 2
pop
What is a Stack?
A data structure for storing items A data structure for storing items which are to be accessed in last-which are to be accessed in last-
in first-out order (LIFO).in first-out order (LIFO).
Thing 1
pop
What is a Stack?
A data structure for storing items A data structure for storing items which are to be accessed in last-which are to be accessed in last-
in first-out order (LIFO).in first-out order (LIFO).
pop
What is a Stack?
A data structure for storing items A data structure for storing items which are to be accessed in last-which are to be accessed in last-
in first-out order (LIFO).in first-out order (LIFO).
The Activation Stack
• The white box represents the computers memory.• The algorithm gets the first frame at the bottom of
the stack.• Variables which are used within a module reside
in that module’s stack frame.
Algo var1 var2 var3
Algo var1 var2 var3
Proc_1 this_var that_var
Adding Modules to the Stack
• When main calls a module, the module gets its own frame “pushed” on the stack.
• The module’s variables live in that new frame.• ONLYONLY the top frame is active. All frames
underneath it are stopped.
Proc_1 this_var that_var
Algo var1 var2 var3
Removing modules from the stack
• When the module completes its instructions, it’s frame is “popped” off the stack and all of its data dies.
• To survive, they must actually be stored in a frame that still exists (passed back).
How In Parameters Work
In formal parameters get a copy of the matching actual parameter.
In the algorithm:
Proc_One (this_var)
In the module:
procedure Proc_One(this_one iot in num)
Proc_One this_one 4
Algo 4
How In Parameters Work
In formal parameters get a copy of the matching actual parameter.
this_var that_var other_var7 9
4
How In/Out Parameters Work
In/out formal parameters act as a reference to the matching actual parameter.
Reading and writing are allowed.
In the algorithm:
Proc_One (this_var)
In the module:
procedure Proc_One(this_one iot in/out num)
Algo 7
Proc_One this_one
How In/Out Parameters Work
In/out formal parameters act as a reference to the matching actual parameter.
Reading and writing are allowed.
R this_one <- 7this_one <- 171
this_var that_var other_var 9471
Print(this_one)
1
How Out Parameters Work
Out formal parameters act as a reference to the matching actual parameter.
Writing is only allowed at first.
After writing, reading is also allowed.
In the algorithm:
Proc_One (this_var)
In the module:
procedure Proc_One(this_one iot out num)
How Out Parameters Work
Out formal parameters act as a reference to the matching actual parameter.
Writing is only allowed at first.
After writing, reading is also allowed.
Proc_One this_one R this_one <- 88
Algo this_var that_var other_var4 7 98
Stack Trace Exampleprocedure juggle (x isoftype in num,
y isoftype out num, z isoftype in/out num)
y <- x + z print (x, y, z) x <- z + 4 z <- x + 2endprocedure
algorithm TraceExample a, b, c isoftype num a <- 1 b <- 2 c <- 3 print(a, b, c) juggle(c, a, b) print(a, b, c)endalgorithm
Procedure Juggle(x iot in Num, y iot out Num, z iot in/out Num) y <- x + z print(x,y,z) x <- z + 4 z <- x - 2endprocedure
Simple Stack Trace Example
Algorithm Demo
a, b, c isoftype num
a <- 1 b <- 2 c <- 3 print(a,b,c) juggle(c,a,b) print(a,b,c)endalgorithm
Output
Demo a b c1 2 3
1 2 3
Juggle x y z3 3 5 2
5 4 3
RR6
5 4
Questions?