1 procedural programming paradigm stacks and procedures
TRANSCRIPT
![Page 1: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/1.jpg)
11
Procedural Programming Procedural Programming ParadigmParadigm
Stacks and ProceduresStacks and Procedures
![Page 2: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/2.jpg)
22
Learning ObjectivesLearning 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.
![Page 3: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/3.jpg)
33
When a procedure or function is When a procedure or function is called:called:
The computer needs to know where to The computer needs to know where to return to when the function or procedure is return to when the function or procedure is completed (completed (return addressreturn address).).
Further, functions and procedures may call Further, functions and procedures may call other functions and procedures which other functions and procedures which means that not only must several return means that not only must several return addresses be stored but they must be addresses be stored but they must be retrieved in the right order. retrieved in the right order.
![Page 4: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/4.jpg)
44
E.g. 3 functions are called after one another.E.g. 3 functions are called after one another.
The numbers represent the addresses of the The numbers represent the addresses of the instructions following the calls to functions. instructions following the calls to functions.
![Page 5: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/5.jpg)
55
Note:Note:
The addresses will be stored in the order The addresses will be stored in the order 100, 150 then 250. 100, 150 then 250.
When the returns take place the When the returns take place the addresses will be needed in the order 250, addresses will be needed in the order 250, 150 then 100.150 then 100.
![Page 6: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/6.jpg)
66
Use a stack to store the return Use a stack to store the return addresses.addresses.
As last address stored is the first address As last address stored is the first address needed on returning from a function.needed on returning from a function.
A stack provides this A stack provides this LLast ast IIn n FFirst irst OOut ut FFacility (acility (LIFOLIFO).). A stack used for these addresses is known as A stack used for these addresses is known as
the calling stack.the calling stack.
![Page 7: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/7.jpg)
77
A diagram to illustrate:A diagram to illustrate:
In the previous example, the addresses In the previous example, the addresses will be stored in the calling stack each time will be stored in the calling stack each time a function is called and will be removed a function is called and will be removed from the calling stack each time a return from the calling stack each time a return instruction is executed. instruction is executed.
![Page 8: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/8.jpg)
88
Calls and Returns Calls and Returns StackStackCall Function ACall Function A
Push return address Push return address
onto stackonto stack
Stack pointerStack pointer 100100
Call Function BCall Function B
Push return address Push return address
onto stackonto stack
Stack pointerStack pointer 150 150
100100
![Page 9: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/9.jpg)
99
Calls and Returns Calls and Returns StackStackCall Function CCall Function C
Push return address Push return address
onto stackonto stack Stack pointerStack pointer 250250150150100100
Return from CReturn from C
Pop return addressPop return address
off stackoff stack250250
Stack pointerStack pointer 150 150
100100
![Page 10: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/10.jpg)
1010
Calls and Returns Calls and Returns StackStackReturn from BReturn from B
Pop return addressPop return address
off stackoff stack 250250150150
Stack pointerStack pointer 100100
Return from AReturn from A
Pop return addressPop return address
off stackoff stack250250
150 150
100100
Stack pointer NULLStack pointer NULL
![Page 11: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/11.jpg)
1111
Parameter passingParameter passing
Means passing variables to a procedure Means passing variables to a procedure when a procedure is called.when a procedure is called.
Actual Parameters (variables passed to the Actual Parameters (variables passed to the procedure):procedure):
Formal Parameters (variables received by Formal Parameters (variables received by the procedure).the procedure).
![Page 12: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/12.jpg)
1212
Passing Value ParametersPassing Value Parameters
ValueValue Means the variables are not passed back to Means the variables are not passed back to
the calling procedure.the calling procedure.So should not be changed.So should not be changed.
This is accomplished by passing the This is accomplished by passing the values in the actual parameters then values in the actual parameters then storing these values in formalstoring these values in formal parameters parameters before use so that the originals are before use so that the originals are unaltered.unaltered. Essentially local copies of the variables are Essentially local copies of the variables are
made (not the original variables).made (not the original variables).
![Page 13: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/13.jpg)
1313
Stack pointer 6 (X2)
3 (X1)
200
Diagram to illustrateDiagram to illustrate
Stack pointer is moved each time an address or Stack pointer is moved each time an address or actual parameter is popped onto or popped off actual parameter is popped onto or popped off the stack.the stack.
Call Proc A (X1, X2)PUSH 200PUSH X1PUSH X2
(example values of X1 & X2 – value parameters)
![Page 14: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/14.jpg)
1414
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)
400
200
Call Proc B (Y1,Y2,Y3)PUSH 400PUSH Y1PUSH Y2PUSH Y3
Return address for Proc A
![Page 15: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/15.jpg)
1515
15 (Y3)
8 (Y2)
18 (Y1)
Stack pointer 400
200
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
![Page 16: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/16.jpg)
1616
15 (Y3)
8 (Y2)
18 (Y1)
400
200
Stack pointer NULL Stack pointer NULL
Return from Proc A
![Page 17: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/17.jpg)
1717
Passing Reference ParametersPassing Reference Parameters
Means the variables will be passed back and so Means the variables will be passed back and so should be changed should be changed (otherwise they should be passed (otherwise they should be passed by value)by value)..This is accomplished by passing the addresses This is accomplished by passing the addresses of variables and not their values to the calling of variables and not their values to the calling stack. stack. The procedures, or functions, will access the The procedures, or functions, will access the actual addresses where the original variables actual addresses where the original variables are stored. are stored. Procedure may change the value because it does not Procedure may change the value because it does not
make a separate copy of it so any changes will be make a separate copy of it so any changes will be passed back to the calling program.passed back to the calling program.
![Page 18: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/18.jpg)
1818
Diagram to illustrateDiagram to illustrate
Stack pointer is moved each time an address or Stack pointer is moved each time an address or actual parameter is popped onto or popped off actual parameter is popped onto or popped off the stack.the stack.
Call Proc A(X1, X2)PUSH 200PUSH X1PUSH X2
(example address of X2 – reference parameter)
(example value of X1 – value parameter)
Stack pointer 600 (X2)
3 (X1)
200
![Page 19: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/19.jpg)
1919
600 (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)
400
200
Call Proc B(Y1,Y2,Y3)PUSH 400PUSH Y1PUSH Y2PUSH Y3
Return address for Proc A
![Page 20: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/20.jpg)
2020
1500 (Y3)
8 (Y2)
18 (Y1)
Stack pointer 400
200
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
![Page 21: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/21.jpg)
2121
1500 (Y3)
8 (Y2)
18 (Y1)
400
200
Stack pointer NULL Stack pointer NULL
Return from Proc A
![Page 22: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/22.jpg)
2222
How do functions return values?How do functions return values?
Functions do not have to return their “results” Functions do not have to return their “results” by passing a parameter by reference. by passing a parameter by reference. (Variable name)(Variable name) = = (Function Name)(Function Name)(Parameters)(Parameters) (Control Name)(Control Name).Text = .Text = (Function Name)(Function Name)(Parameters)(Parameters)
Private Function …(ByVal … As …, ByVal… As …)Private Function …(ByVal … As …, ByVal… As …) DimDim (Variable name to be returned) (Variable name to be returned) As As (Data Type)(Data Type) …… Return (Variable name to be returned) Return (Variable name to be returned) End FunctionEnd Function
![Page 23: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/23.jpg)
2323
How do functions return values How do functions return values (without use of reference parameters)?(without use of reference parameters)?
They push the value on the stack They push the value on the stack immediately before returning. immediately before returning. The calling program can then pop the The calling program can then pop the value off the stack. value off the stack.
N.B. N.B. The return address has to be popped off the The return address has to be popped off the
stack before pushing the return value onto the stack before pushing the return value onto the stack.stack.
![Page 24: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/24.jpg)
2424
Stack pointer 6 (X2)
3 (X1)
200
Diagram to illustrateDiagram to illustrate
Stack pointer is moved each time an address or Stack pointer is moved each time an address or actual parameter is popped onto or popped off actual parameter is popped onto or popped off the stack.the stack.
Fn A (X1, X2)PUSH 200PUSH X1PUSH X2
(example values of X1 & X2 – value parameters)
![Page 25: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/25.jpg)
2525
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)
400
200
Fn B (Y1,Y2,Y3)PUSH 400PUSH Y1PUSH Y2PUSH Y3
Return address for Fn A
![Page 26: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/26.jpg)
2626
15 (Y3)
8 (Y2)
18 (Y1)
Stack pointer 400
200
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 Ba. Return address popped offReturn address popped off.
![Page 27: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/27.jpg)
2727
15 (Y3)
8 (Y2)
18 (Y1)
Stack pointer 16
200
Return from Fn Bb. Return value pushed on.Return value pushed on.
(value returned by Fn B)
15 (Y3)
8 (Y2)
18 (Y1)
16
Stack pointer 200
Fn Ac. Return value popped off.Return value popped off.
![Page 28: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/28.jpg)
2828
15 (Y3)
8 (Y2)
18 (Y1)
16
Stack pointer 32
Return from Fn Ab. Return value pushed on.Return value pushed on.
(value returned by Fn A)
15 (Y3)
8 (Y2)
18 (Y1)
16
200
Stack pointer NULL Stack pointer NULL
Return from Fn Aa. Return address popped offReturn address popped off.
![Page 29: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/29.jpg)
2929
15 (Y3)
8 (Y2)
18 (Y1)
16
32
Stack pointer NULL Stack pointer NULL
Main Programc. Return address popped offReturn address popped off.
![Page 30: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/30.jpg)
3030
PlenaryPlenary
Explain the passing of parameters by Explain the passing of parameters by reference and by value. reference and by value.
![Page 31: 1 Procedural Programming Paradigm Stacks and Procedures](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649c765503460f94929d04/html5/thumbnails/31.jpg)
3131
PlenaryPlenary
ValueValue A constant or value of a variable is passed to a A constant or value of a variable is passed to a
procedure using the calling stack.procedure using the calling stack. Procedure makes a copy of it before using it so that Procedure makes a copy of it before using it so that
the original is unaltered.the original is unaltered.
ReferenceReference The address of the value is passed to the procedure The address of the value is passed to the procedure
using the calling stack.using the calling stack. Procedure may change the value because it does Procedure may change the value because it does
not make a separate copy of it so any changes will not make a separate copy of it so any changes will be passed back to the calling program.be passed back to the calling program.