Presentation is loading. Please wait.

Presentation is loading. Please wait.

CP1020 - Lecture 11 Functions and advanced procedures.

Similar presentations


Presentation on theme: "CP1020 - Lecture 11 Functions and advanced procedures."— Presentation transcript:

1 CP1020 - Lecture 11 Functions and advanced procedures

2 Functions Standard functions that we can use e.g fSquareRoot = SQR(iNumber) in this example the value in iNumber is passed into the SQR function and the answer is returned to the variable fSquareRoot. QBasic allows you to create your own functions in a similar way to writing procedures.

3 Creating a function From the Edit menu select the New Function… option, a dialogue box will appear requesting a name for the function. A new code window opens up and your lines of code are written between SUB Function name (parameter list) and END SUB

4 Function example zA function that will determine the area of a triangle and return the answer to a variable Base Height Area = Base X Height 2

5 Function example Main programme fBase = 3 fHeight = 6 fArea = TriangleArea(fBase, fHeight) PRINT “the area of the triangle is “, fArea FUNCTION TriangleArea(fBottom AS SINGLE, fDrop AS SINGLE) TriangleArea = fBottom * fDrop / 2 END FUNCTION Parameters to pick up data Result returned through function name Result from function returned to this variable Arguments passed into function

6 Some rules governing functions zFunctions are similar to procedures in the way that data is passed to them either by value or by reference - same rules govern their use and behaviour - however, you should never alter their original values in a function zAs you use the function name to return the result to the main program you cannot create a variable in the function with the same name as the function FUNCTION Add(iNum1 As Integer) DIM Add AS INTEGER Declaration with same name is forbidden

7 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 i.e. Any declared in green procedure cannot be seen in the main program (orange) or blue procedure.

8 Returning more than 1 value zTo return more than one value to the main program you must use a procedure and pass values by reference

9 An example zA procedure is required to determine the cost and volume of concrete to fill a trench for a building’s foundation. zThe procedure calculates these values based on the cost of concrete per cubic meter (£120) and the volume of concrete needed. height width length Vol = ht x wd x len

10 The calling program DIM fHeight, fLen, fWid AS SINGLE Dim fVol, fCost AS SINGLE INPUT “The height, Length and Width” ; fHeight, fLen, fWid ‘at this point fVol and fCost = 0 CALL Concrete(fHeight, fLen, fWid, fVol, fCost ) ‘at this point fVol and fCost have a value determined ‘by the procedure PRINT “the volume of concrete used is ”; fVol ;” m 3 “ PRINT “ at a cost of £”; fCost

11 The procedure SUB Concrete(…. fVol AS SINGLE, fCost AS SINGLE) fVol = fHeight * fLen * fWid fCost = fVol * 120 END SUB New values returned to main program by reference

12 A second example zlets extend our exam grading program to determine the exam grade and overall mark for a student zhere we will use 2 marks to determine a students overall mark, and what grade they get.

13 A procedure that passes several values DIM iTest1, iTest2, iTotal AS INTEGER DIM sMsgAS STRING INPUT “Enter your TWO test results” ; iTest1, iTest2 CALL Grade(iTest1, iTest2, iTotal, sMsg) PRINT “Your overall mark was “; iTotal PRINT sMsg Main program

14 continued SUB Grade(……, iTotal AS INTEGER, sMsg AS STRING) iTotal = iTest1 + iTest2 SELECT CASE iTotal CASE IS < 40 sMsg = “Sorry try harder” CASE 40 TO 59 sMsg = “well done you have a pass” CASE 60 TO 100 sMsg = “excellent result- you have a merit” END SELECT END SUB As iTotal and sMsg are receiving the arguments from the main program by reference then they will pass back new values

15 Questions 1 What is the difference between a procedure and a function? 3 Write a function that will determine which of the two numbers passed to the function is the largest. 4. Write the procedure call and procedure to determine which is the largest of two numbers and the difference between the two.


Download ppt "CP1020 - Lecture 11 Functions and advanced procedures."

Similar presentations


Ads by Google