1 / 43

Understanding Module Invocation & Parameters in Algorithm Design

Learn how to trace the execution of instructions using the activation stack and passing parameters in algorithms. See examples, rules, and advantage of using parameters effectively.

jakel
Download Presentation

Understanding Module Invocation & Parameters in Algorithm Design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 5 Module Invocation & Parameters Tracing the Execution of Instructionswith 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 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.

  15. out 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 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. LB Pop Quiz 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 // ILLEGAL!

  19. Questions?

  20. Tracing the Execution of Instructionswith 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”) Push Pop Thing 3 Thing 2 Thing 1

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

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

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

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

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

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

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

  29. pop Thing 1 What is a Stack? 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).

  31. var2 var3 var1 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

  32. 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. • ONLY the top frame is active. All frames underneath it are stopped. Algo var1 var2 var3

  33. Proc_1 this_var that_var 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). Algo var1 var2 var3

  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. this_one Proc_One 4 Algo How In Parameters Work In formal parameters get a copy of the matching actual parameter. 4 7 4 9 this_var that_var other_var

  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. 1 this_one Proc_One 7 1 Algo How In/Out Parameters Work In/out formal parameters act as a reference to the matching actual parameter. Reading and writing are allowed. Print(this_one) this_one <- 1 this_one <- 7 R 7 9 4 this_var that_var 1 other_var 7

  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. this_one Proc_One 8 Algo 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. this_one <- 8 R 4 9 7 this_var that_var other_var 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. Juggle x y z Demo a c b 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 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 Output 1 2 3 3 3 5 2 6 R R 5 4 3 1 3 2 5 4

  42. Questions?

More Related