Upcoming SlideShare
×

# Procedures, the Pop-11 stack and debugging

400 views

Published on

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
400
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
3
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Procedures, the Pop-11 stack and debugging

1. 1. AI Programming Week Two: Procedures, the Stack & Debugging Richard Price rmp@cs.bham.ac.uk
2. 2. Overview Introduction • • Recap • Procedures • The Stack 2
3. 3. Course Details http://www.cs.bham.ac.uk/internal/courses/ai-prog-a/ • Lab sessions: – Monday 3pm - 5pm. – Friday 2pm-4pm. • Assessment Deadline – Mondays 5pm. • Late submissions until: – Wednesday 10am. – 50% penalty. 3
4. 4. Recap • Variables & Expressions – Print arrow – Semicolon – Declaration – Assignment – Data Types – Comments. 4
5. 5. List • Lists: [1 2 3] => ** [1 2 3] lvars mylist = [19 18 25]; mylist => mylist(1) => ** 19 lvars listInAList = [1 [2 3]]; listInAList(2) => ** [2 3]; 5
6. 6. Procedures • Programs can become very large. • Procedures break down code into pieces. – Reusable. – Recognisable. – Named mini-program. 6
7. 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. 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
9. 9. Variables & Procedures • lvars declares local variables. – Local to a procedure. 9
10. 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. 11. Arguments & Procedures • Write a procedure called addition that: – Takes in two numbers as arguments. – Adds them together. – And prints the result. 11
12. 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. 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. 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. 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. 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
17. 17. 17
18. 18. Viewing the Stack lvars myVariable; 5 -> myVariable; myVariable => 4; 5; 6; => ** 4 5 6 18
19. 19. Procedures & the stack 3; 11; max(); => 19