170 likes | 291 Views
CP1020 - Week 10. Modularising programs using Procedures. Modularising programs using Procedures. Modularising programs using Procedures. Aims of this lecture. Understand why we should modularise a program Understand how QBasic approaches this Create some procedures.
E N D
CP1020 - Week 10 Modularising programs using Procedures Modularising programs using Procedures Modularising programs using Procedures
Aims of this lecture • Understand why we should modularise a program • Understand how QBasic approaches this • Create some procedures
Why modularise a program • It is much easier to solve any problem, by breaking it down into tasks and solve them one at a time - called procedures • It also allows these small modules or procedures to be re-used, and cuts down the amount of coding and potential errors • This approach is fundamental to programming, in fact it is the basis of programming in C++
A simple example A program that prints a title banner surrounded by asterisks. PRINT “*****************************************” PRINT “*****************************************” PRINT “** **” PRINT ”** Something **” PRINT “** **” PRINT “*****************************************” PRINT “*****************************************” PRINT “** **” PRINT ”** More things **” PRINT “** **” PRINT “*****************************************” PRINT “*****************************************”
The example Here we have the lines PRINT “*****************************************” 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?
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 • and procedure name • END SUB Creating a sub procedure In our case this would look like SUB Asterisk PRINT “*****************************************************” PRINT “*****************************************************” END SUB
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
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
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 INPUT “enter your two test results” ; iTest1, iTest2 CALL Grade(iTest1, iTest2) Main program procedure A procedure that uses data
Passing data to a Procedure • The call statement in the example shows how we pass data to the procedure CALL Grade(iTest1, iTest2) • The variables iTest1 and iTest2 are placed in brackets and pass the data across. • The data is picked up by iNum1 and iNum2 in the procedure SUB Grade(iNum1AS INTEGER, iNum2AS INTEGER) • We can then use iNum1 and iNum2 in the body of the procedure • However it’s a bit more complicated!
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 iNum1 iTest1 2 Passing data by reference 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.
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
JARGON - an argument is the name given to the list of variables that are placed in the procedure call 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) ) 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)
Parameters of the function 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 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
Some rules governing functions & procedures • Any 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 • Example a procedure that adds up two numbers
A program that won’t work - why? 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 • SUB AddUp( iNum1 AS INTEGER, iNum2 AS INTEGER) • Dim iTotal AS INTEGER • iTotal = iNum1 + iNum2 • END SUB
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