6. Procedures
• Programs can become very large.
• Procedures break down code into pieces.
– Reusable.
– Recognisable.
– Named mini-program.
6
7. Built in procedures
• Addition and division are built in
procedures.
• Other examples:
max(2,5) =>
** 5
min(2,5) =>
** 2
• In Xved, press Enter then type help max.
7
8. Write your own procedures
• Note the brackets in max(2,5);
define myprocedure()
;;; code goes here
enddefine;
myprocedure();
define printTwo()
2 =>
enddefine;
printTwo();
** 2
8
10. Arguments & Procedues
• Procedures often take in arguments.
– +(oneNumber, anotherNumber);
– max(argument1, argument2);
define printTwice(valueToPrint);
valueToPrint =>
valueToPrint =>
enddefine;
printTwice(‘Rich’);
** Rich
** Rich
10
11. Arguments & Procedures
• Write a procedure called addition that:
– Takes in two numbers as arguments.
– Adds them together.
– And prints the result.
11
12. Returning results
• Procedures often return results:
– Max returns the maximum of two numbers.
define max(firstNumber, secondNumber) -> largerNumber;
…
enddefine;
max(5,25) =>
** 25
12
13. Multiple Arguments & Results
• Procedures often return results:
– Max returns the maximum of two numbers.
define printAPlusBPlusC(a,b,c)
a + b + c =>
enddefine;
define returnAthenB(a,b) -> (result1, result2);
a -> result1;
b -> result2;
enddefine;
13
14. Procedures in Procedures
• Procedures can be used within
procedures:
define minOfThree(number1, number2, number3) -> smallestOfThree;
lvars smallestOfTwo;
min(number1, number2) -> smallestOfTwo;
min(number3, smallestOfTwo) -> smallestOfThree;
enddefine;
14
15. The Pop-11 Stack
• Almost everything in Pop-11 uses the
stack.
• If variables are just containers.
• Then the stack is a pile of boxes.
15
16. Assignment and the Stack
• Assignment in Pop-11
– Uses the stack!
– lvars creates the x variable.
– 4 is place on the stack
– Assignment pops 4 off.
– Into the x variable.
16