Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack.

Similar presentations


Presentation on theme: "Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack."— Presentation transcript:

1 Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack

2 Module Invocation & Parameters

3 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.

4 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

5 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 endif endfunction

6 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

7 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)

8 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)

9 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

10 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

11 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

12 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

13 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).

14 Comparing Parameter Types IN Copies 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. OUT Formal 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/OUT Formal parameter is pseudonym for actual parameter and can access its original value and writes its values into actual parameter, overwriting the original.

15 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_num endfunction 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

16 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

17 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

18 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!

19 Questions?

20 Tracing the Execution of Instructions with the Activation Stack

21 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 3 PushPop

22 What is a Stack? A data structure for storing items which are to be accessed in last- in first-out order (LIFO). A data structure for storing items which are to be accessed in last- in first-out order (LIFO).

23 Thing 1 PUSH A data structure for storing items which are to be accessed in last- in first-out order (LIFO). A data structure for storing items which are to be accessed in last- in first-out order (LIFO). What is a Stack?

24 Thing 1 Thing 2 PUSH What is a Stack? A data structure for storing items which are to be accessed in last- in first-out order (LIFO). A data structure for storing items which are to be accessed in last- in first-out order (LIFO).

25 Thing 1 pop What is a Stack? A data structure for storing items which are to be accessed in last- in first-out order (LIFO). A data structure for storing items which are to be accessed in last- in first-out order (LIFO).

26 Thing 1 Thing 2 PUSH What is a Stack? A data structure for storing items which are to be accessed in last- in first-out order (LIFO). A data structure for storing items which are to be accessed in last- in first-out order (LIFO).

27 Thing 1Thing 2 Thing 3 PUSH What is a Stack? A data structure for storing items which are to be accessed in last- in first-out order (LIFO). A data structure for storing items which are to be accessed in last- in first-out order (LIFO).

28 Thing 1Thing 2 pop What is a Stack? A data structure for storing items which are to be accessed in last- in first-out order (LIFO). A data structure for storing items which are to be accessed in last- in first-out order (LIFO).

29 Thing 1 pop What is a Stack? A data structure for storing items which are to be accessed in last- in first-out order (LIFO). A data structure for storing items which are to be accessed in last- in first-out order (LIFO).

30 pop What is a Stack? A data structure for storing items which are to be accessed in last- in first-out order (LIFO). A data structure for storing items which are to be accessed in last- in first-out order (LIFO).

31 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 var2var3

32 Algo var1var2var3 Proc_1 this_varthat_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.

33 Proc_1 this_varthat_var Algo var1var2var3 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).

34 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)

35 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_var 7 9 4

36 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)

37 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 <- 7 this_one <- 1 7 1 this_var that_var other_var 9 4 7 1 Print(this_one) 1

38 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)

39 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 <- 8 8 Algo this_var that_var other_var 4 7 9 8

40 Stack Trace Example procedure 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 + 2 endprocedure 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

41 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 - 2 endprocedure 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 abc 1 2 3 1 2 3 Juggle xyz 3 3 5 2 5 4 3 R R 6 5 4

42 Questions?

43


Download ppt "Module Invocation & Parameters Tracing the Execution of Instructions with the Activation Stack."

Similar presentations


Ads by Google