320 likes | 460 Views
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:.
E N D
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: • 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.
E.g. 3 functions are called after one another. • The numbers represent the addresses of the instructions following the calls to functions.
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.
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 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.
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).
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).
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)
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
Proc B (ByVal B1, ByValB2, ByValB3) B3 = Y3 (POP Y3) B2 = Y2 (POP Y2) B1 = Y1 (POP Y1) Return from Proc B
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.
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)
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
Proc B (ByValB1, ByValB2, ByRefB3) B3 = Y3 (POP Y3) B2 = Y2 (POP Y2) B1 = Y1 (POP Y1) Return from Proc B
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
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.
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)
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
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.
Return from Fn B b.Return value pushed on. (value returned by Fn B) Fn A c.Return value popped off.
Return from Fn A a.Return address popped off. Return from Fn A b.Return value pushed on. (value returned by Fn A)
Main Program c.Return address popped off.
Plenary • Explain the passing of parameters by reference and by value.
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.