Download presentation
Presentation is loading. Please wait.
1
CP1020 - Week 10 Modularising programs using Procedures
2
CP1020 Principles of programming - Steve Garner & Ian Coulson Aims of this lecture zUnderstand why we should modularise a program zUnderstand how QBasic approaches this zCreate some procedures
3
CP1020 Principles of programming - Steve Garner & Ian Coulson Why modularise a program zIt is much easier to solve any problem, by breaking it down into tasks and solve them one at a time - called procedures zIt also allows these small modules or procedures to be re-used, and cuts down the amount of coding and potential errors zThis approach is fundamental to programming, in fact it is the basis of programming in C++
4
CP1020 Principles of programming - Steve Garner & Ian Coulson A simple example PRINT “*****************************************” PRINT “** **” PRINT ”** Something **” PRINT “** **” PRINT “*****************************************” PRINT “** **” PRINT ”** More things **” PRINT “** **” PRINT “*****************************************” A program that prints a title banner surrounded by asterisks.
5
CP1020 Principles of programming - Steve Garner & Ian Coulson The example Here we have the lines PRINT “*****************************************” repeated three times in the program. We could write a small procedure that we use three times to do this. How do we do this?
6
CP1020 Principles of programming - Steve Garner & Ian Coulson Creating a sub procedure From the Edit menu select the New Sub… option, a dialogue box will appear requesting a procedure name. A new code window opens up and your lines of code are written between SUB Asterisk andprocedure name END SUB In our case this would look like SUB Asterisk PRINT “*****************************************************” END SUB
7
CP1020 Principles of programming - Steve Garner & Ian Coulson Running a procedure We’ve created the procedure and now have to use it! To run our small section of code (procedure) we simply have to use the keyword CALL Procedure Name e.g. Now write this as the main program CALL Asterisk PRINT “** **” PRINT ”** Some inconsequential tripe **” PRINT “** **” CALL Asterisk PRINT “** **” PRINT ”** More things **” PRINT “** **” CALL Asterisk
8
CP1020 Principles of programming - Steve Garner & Ian Coulson Procedures that use data May need a procedure that uses data from the main body of the program There are two aspects to this 1. We must call the procedure and give it the data 2. The procedure must be able to pick up the data example a procedure that determines a student grade from the results of 2 tests
9
CP1020 Principles of programming - Steve Garner & Ian Coulson A procedure that uses data SUB Grade(iNum1 AS INTEGER, iNum2 AS INTEGER) DIM iScore AS INTEGER iScore = iNum1 + iNum2 SELECT CASE iScore CASE IS < 40 PRINT “Sorry try harder” CASE 40 TO 59 PRINT “well done you have a pass” CASE 60 TO 100 PRINT “excellent result- you have a merit” END SELECT END SUB procedure Main program INPUT “enter your two test results” ; iTest1, iTest2 CALL Grade(iTest1, iTest2)
10
CP1020 Principles of programming - Steve Garner & Ian Coulson Passing data to a Procedure zThe call statement in the example shows how we pass data to the procedure CALL Grade(iTest1, iTest2) zThe variables iTest1 and iTest2 are placed in brackets and pass the data across. zThe data is picked up by iNum1 and iNum2 in the procedure SUB Grade(iNum1 AS INTEGER, iNum2 AS INTEGER) zWe can then use iNum1 and iNum2 in the body of the procedure zHowever it’s a bit more complicated!
11
CP1020 Principles of programming - Steve Garner & Ian Coulson Passing data by reference iNum1 and iNum2 ARE NOT new variables with copies of the data stored in variables iTest1 and iTest2 iNum1 actually refers to the data stored in iTest1 2 iTest1 iNum1 BEWARE - This means any change in the value of iNum1 in the procedure, will result in a change in the value of iTest1 in the main body of the program.
12
CP1020 Principles of programming - Steve Garner & Ian Coulson Example Main program iVal = 6 CALL Example(iVal) PRINT “iVal =“, iVal Procedure SUB Example(iNumber AS INTEGER) iNumber = iNumber + 10 END SUB What value do you think will be printed for iVal? iVal = 6 or iVal = 16 well the answer is iVal = 16
13
CP1020 Principles of programming - Steve Garner & Ian Coulson Passing data across by value You may want to pass the data into a procedure and not alter the original value in the main program. We can do this by passing it by value by enclosing the variables (or arguments) in the call statement in brackets e.g. CALL procedure name( ( argument ),( argument ) ) JARGON - an argument is the name given to the list of variables that are placed in the procedure call More JARGON - the corresponding list of dummy variables used to pick up the arguments in the procedure are known as parameters e.g Procedure name ( parameters)
14
CP1020 Principles of programming - Steve Garner & Ian Coulson example of passing by value Main program iVal1 = 5 iVal2 = 3 CALL Example( ( iVal1 ), ( iVal2 ) ) PRINT “iVal1 = “, iVal1 “and iVal2 = “, iVal2 Procedure SUB Example(iNum1 AS INTEGER, iNum2 AS INTEGER) iNum1 = iNum1 + 10 iNum2 = iNum2 + 7 END SUB Parameters of the function What value do you think will be printed for iVal1 and iVal2? Ival1 = 15 & iVal2 = 10 or iVal1 = 5 & iVal2 = 3 well the answer is -> iVal1 = 5 & iVal2 = 3
15
CP1020 Principles of programming - Steve Garner & Ian Coulson Some rules governing functions & procedures zAny variables declared within a function or procedure are local - that is the calling program or any other procedure or function cannot see or use the values stored in these variables zExample a procedure that adds up two numbers
16
CP1020 Principles of programming - Steve Garner & Ian Coulson A program that won’t work - why? SUB AddUp( iNum1 AS INTEGER, iNum2 AS INTEGER) Dim iTotal AS INTEGER iTotal = iNum1 + iNum2 END SUB DIM iNum1, iNum2 AS INTEGER INPUT “enter 2 numbers”, iNum1, iNum2 CALL AddUp(iNum1, iNum2) PRINT “the sum of your two numbers is “; iTotal Because iTotal has been declared within AddUp - therefore does not exist outside of it - so can’t print it’s value out in main program
17
CP1020 Principles of programming - Steve Garner & Ian Coulson Questions 1 Why do we modularise a program? 2 Write a call statement and procedure that will print the statement “Hello World” 3 Write a procedure to pass 2 numbers across by reference and determine the difference between the two and print it out. 4 rewrite this such that the numbers are passed by value
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.