Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Procedural Programming Paradigm Stacks and Procedures.

Similar presentations


Presentation on theme: "1 Procedural Programming Paradigm Stacks and Procedures."— Presentation transcript:

1 1 Procedural Programming Paradigm Stacks and Procedures

2 2 Learning Objectives Describe the use of parameters, local and global variables as standard programming techniques. Explain how a stack is used to handle procedure calling and parameter passing.

3 3 When a procedure or function is called: The computer needs to know where to return to when the function or procedure is completed (return address). Further, functions and procedures may call other functions and procedures which means that not only must several return addresses be stored but they must be retrieved in the right order.

4 4 E.g. 3 functions are called after one another. The numbers represent the addresses of the instructions following the calls to functions.

5 5 Note: The addresses will be stored in the order 100, 150 then 250. When the returns take place the addresses will be needed in the order 250, 150 then 100.

6 6 Use a stack to store the return addresses. As last address stored is the first address needed on returning from a function. A stack provides this Last In First Out Facility (LIFO). A stack used for these addresses is known as the calling stack. A stack used for these addresses is known as the calling stack.

7 7 A diagram to illustrate: In the previous example, the addresses will be stored in the calling stack each time a function is called and will be removed from the calling stack each time a return instruction is executed.

8 8 Calls and Returns Stack Call Function A Push return address onto stack Stack pointer 100 Call Function B Push return address onto stack Stack pointer

9 9 Calls and Returns Stack Call Function C Push return address onto stack Stack pointer Return from C Pop return address off stack 250 Stack pointer

10 10 Calls and Returns Stack Return from B Pop return address off stack Stack pointer 100 Return from A Pop return address off stack Stack pointer NULL

11 11 Parameter passing Means passing variables to a procedure when a procedure is called. Actual Parameters (variables passed to the procedure): Actual Parameters (variables passed to the procedure): Formal Parameters (variables received by the procedure). Formal Parameters (variables received by the procedure).

12 12 Passing Value Parameters Value Means the variables are not passed back to the calling procedure. Means the variables are not passed back to the calling procedure. So should not be changed. This is accomplished by passing the values in the actual parameters then storing these values in formal parameters before use so that the originals are unaltered. Essentially local copies of the variables are made (not the original variables). Essentially local copies of the variables are made (not the original variables).

13 13 Stack pointer6 (X2) 3 (X1) 200 Diagram to illustrate Stack pointer is moved each time an address or actual parameter is popped onto or popped off the stack. Call Proc A (X1, X2) PUSH 200 PUSH X1 PUSH X2 (example values of X1 & X2 – value parameters)

14 14 6 (X2) 3 (X1) Stack pointer 200 Proc A (ByVal A1, ByVal A2) A2 = X2 (POP X2) A1 = X1 (POP X1) Stack pointer 15 (Y3) 8 (Y2) 18 (Y1) Call Proc B (Y1,Y2,Y3) PUSH 400 PUSH Y1 PUSH Y2 PUSH Y3 Return address for Proc A

15 15 15 (Y3) 8 (Y2) 18 (Y1) Stack pointer Proc B (ByVal B1, ByVal B2, ByVal B3) B3 = Y3 (POP Y3) B2 = Y2 (POP Y2) B1 = Y1 (POP Y1) 15 (Y3) 8 (Y2) 18 (Y1) 400 Stack pointer 200 Return from Proc B

16 16 15 (Y3) 8 (Y2) 18 (Y1) Stack pointer NULL Return from Proc A

17 17 Passing Reference Parameters Means the variables will be passed back and so should be changed (otherwise they should be passed by value). This is accomplished by passing the addresses of variables and not their values to the calling stack. The procedures, or functions, will access the actual addresses where the original variables are stored. Procedure may change the value because it does not make a separate copy of it so any changes will be passed back to the calling program. Procedure may change the value because it does not make a separate copy of it so any changes will be passed back to the calling program.

18 18 Diagram to illustrate Stack pointer is moved each time an address or actual parameter is popped onto or popped off the stack. Call Proc A(X1, X2) PUSH 200 PUSH X1 PUSH X2 (example address of X2 – reference parameter) (example value of X1 – value parameter) Stack pointer 600 (X2) 3 (X1) 200

19 (X2) 3 (X1) Stack pointer 200 Proc A (ByVal A1, ByRef A2) A2 = X2 (POP X2) A1 = X1 (POP X1) Stack pointer 1500 (Y3) 8 (Y2) 18 (Y1) Call Proc B(Y1,Y2,Y3) PUSH 400 PUSH Y1 PUSH Y2 PUSH Y3 Return address for Proc A

20 (Y3) 8 (Y2) 18 (Y1) Stack pointer Proc B (ByVal B1, ByVal B2, ByRef B3) B3 = Y3 (POP Y3) B2 = Y2 (POP Y2) B1 = Y1 (POP Y1) 1500 (Y3) 8 (Y2) 18 (Y1) 400 Stack pointer 200 Return from Proc B

21 (Y3) 8 (Y2) 18 (Y1) Stack pointer NULL Return from Proc A

22 22 How do functions return values? Functions do not have to return their “results” by passing a parameter by reference. (Variable name) = (Function Name)(Parameters) (Variable name) = (Function Name)(Parameters) (Control Name).Text = (Function Name)(Parameters) (Control Name).Text = (Function Name)(Parameters) Private Function …(ByVal … As …, ByVal… As …) Private Function …(ByVal … As …, ByVal… As …) Dim (Variable name to be returned) As (Data Type) Dim (Variable name to be returned) As (Data Type) … Return (Variable name to be returned) Return (Variable name to be returned) End Function End Function

23 23 How do functions return values (without use of reference parameters)? They push the value on the stack immediately before returning. The calling program can then pop the value off the stack. N.B. The return address has to be popped off the stack before pushing the return value onto the stack. The return address has to be popped off the stack before pushing the return value onto the stack.

24 24 Stack pointer6 (X2) 3 (X1) 200 Diagram to illustrate Stack pointer is moved each time an address or actual parameter is popped onto or popped off the stack. Fn A (X1, X2) PUSH 200 PUSH X1 PUSH X2 (example values of X1 & X2 – value parameters)

25 25 6 (X2) 3 (X1) Stack pointer 200 Fn A (A1,A2) A2 = X2 (POP X2) A1 = X1 (POP X1) Stack pointer 15 (Y3) 8 (Y2) 18 (Y1) Fn B (Y1,Y2,Y3) PUSH 400 PUSH Y1 PUSH Y2 PUSH Y3 Return address for Fn A

26 26 15 (Y3) 8 (Y2) 18 (Y1) Stack pointer Fn B(B1,B2,B3) B3 = Y3 (POP Y3) B2 = Y2 (POP Y2) B1 = Y1 (POP Y1) 15 (Y3) 8 (Y2) 18 (Y1) 400 Stack pointer 200 Return from Fn B Return address popped off a. Return address popped off.

27 27 15 (Y3) 8 (Y2) 18 (Y1) Stack pointer Return from Fn B Return value pushed on. b. Return value pushed on. (value returned by Fn B) 15 (Y3) 8 (Y2) 18 (Y1) 16 Stack pointer 200 Fn A Return value popped off. c. Return value popped off.

28 28 15 (Y3) 8 (Y2) 18 (Y1) 16 Stack pointer 32 Return from Fn A Return value pushed on. b. Return value pushed on. (value returned by Fn A) 15 (Y3) 8 (Y2) 18 (Y1) Stack pointer NULL Return from Fn A Return address popped off a. Return address popped off.

29 29 15 (Y3) 8 (Y2) 18 (Y1) Stack pointer NULL Main Program Return address popped off c. Return address popped off.

30 30 Plenary Explain the passing of parameters by reference and by value.

31 31 Plenary Value A constant or value of a variable is passed to a procedure using the calling stack. A constant or value of a variable is passed to a procedure using the calling stack. Procedure makes a copy of it before using it so that the original is unaltered. Procedure makes a copy of it before using it so that the original is unaltered.Reference The address of the value is passed to the procedure using the calling stack. The address of the value is passed to the procedure using the calling stack. Procedure may change the value because it does not make a separate copy of it so any changes will be passed back to the calling program. Procedure may change the value because it does not make a separate copy of it so any changes will be passed back to the calling program.


Download ppt "1 Procedural Programming Paradigm Stacks and Procedures."

Similar presentations


Ads by Google