Procedures, the Pop-11 stack and debugging
Upcoming SlideShare
Loading in...5
×
 

Procedures, the Pop-11 stack and debugging

on

  • 617 views

 

Statistics

Views

Total Views
617
Slideshare-icon Views on SlideShare
617
Embed Views
0

Actions

Likes
0
Downloads
1
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Procedures, the Pop-11 stack and debugging Procedures, the Pop-11 stack and debugging Presentation Transcript

    • AI Programming Week Two: Procedures, the Stack & Debugging Richard Price rmp@cs.bham.ac.uk
    • Overview Introduction • • Recap • Procedures • The Stack 2
    • 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
    • Recap • Variables & Expressions – Print arrow – Semicolon – Declaration – Assignment – Data Types – Comments. 4
    • 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
    • Procedures • Programs can become very large. • Procedures break down code into pieces. – Reusable. – Recognisable. – Named mini-program. 6
    • 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
    • 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
    • Variables & Procedures • lvars declares local variables. – Local to a procedure. 9
    • Arguments & Procedues • Procedures often take in arguments. – +(oneNumber, anotherNumber); – max(argument1, argument2); define printTwice(valueToPrint); valueToPrint => valueToPrint => enddefine; printTwice(‘Rich’); ** Rich ** Rich 10
    • Arguments & Procedures • Write a procedure called addition that: – Takes in two numbers as arguments. – Adds them together. – And prints the result. 11
    • Returning results • Procedures often return results: – Max returns the maximum of two numbers. define max(firstNumber, secondNumber) -> largerNumber; … enddefine; max(5,25) => ** 25 12
    • 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
    • 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
    • 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
    • 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
    • Viewing the Stack lvars myVariable; 5 -> myVariable; myVariable => 4; 5; 6; => ** 4 5 6 18
    • Procedures & the stack 3; 11; max(); => 19