1 / 31

Procedural Programming Paradigm

Procedural Programming Paradigm. Stacks and Procedures. 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. When a procedure or function is called:.

Download Presentation

Procedural Programming Paradigm

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. Procedural Programming Paradigm Stacks and Procedures

  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. 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. E.g. 3 functions are called after one another. • The numbers represent the addresses of the instructions following the calls to functions.

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

  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. Parameter passing • Means passing variables to a procedure when a procedure is called. • Actual Parameters (variables passed to the procedure): • Formal Parameters (variables received by the procedure).

  9. Passing Value Parameters • Value • 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 formalparameters before use so that the originals are unaltered. • Essentially local copies of the variables are made (not the original variables).

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

  11. Proc A (ByVal A1, ByVal A2) A2 = X2 (POP X2) A1 = X1 (POP X1) Call Proc B (Y1,Y2,Y3) PUSH 400 PUSH Y1 PUSH Y2 PUSH Y3 Return address for Proc A

  12. Proc B (ByVal B1, ByValB2, ByValB3) B3 = Y3 (POP Y3) B2 = Y2 (POP Y2) B1 = Y1 (POP Y1) Return from Proc B

  13. Return from Proc A

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

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

  16. Proc A (ByVal A1, ByRef A2) A2 = X2 (POP X2) A1 = X1 (POP X1) Call Proc B(Y1,Y2,Y3) PUSH 400 PUSH Y1 PUSH Y2 PUSH Y3 Return address for Proc A

  17. Proc B (ByValB1, ByValB2, ByRefB3) B3 = Y3 (POP Y3) B2 = Y2 (POP Y2) B1 = Y1 (POP Y1) Return from Proc B

  18. Return from Proc A

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

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

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

  22. Fn A (A1,A2) A2 = X2 (POP X2) A1 = X1 (POP X1) Fn B (Y1,Y2,Y3) PUSH 400 PUSH Y1 PUSH Y2 PUSH Y3 Return address for Fn A

  23. Fn B(B1,B2,B3) B3 = Y3 (POP Y3) B2 = Y2 (POP Y2) B1 = Y1 (POP Y1) Return from Fn B a.Return address popped off.

  24. Return from Fn B b.Return value pushed on. (value returned by Fn B) Fn A c.Return value popped off.

  25. Return from Fn A a.Return address popped off. Return from Fn A b.Return value pushed on. (value returned by Fn A)

  26. Main Program c.Return address popped off.

  27. Plenary • Explain the passing of parameters by reference and by value.

  28. Plenary • Value • 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. • Reference • 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.

More Related