1 / 15

CP1020 - Lecture 11

CP1020 - Lecture 11. Functions and advanced procedures. 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 .

nieve
Download Presentation

CP1020 - Lecture 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. Area = Base X Height 2 Height Base Function example • A function that will determine the area of a triangle and return the answer to a variable

  5. Arguments passed into function Result from function returned to this variable Parameters to pick up data Result returned through function name 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

  6. Declaration with same name is forbidden FUNCTION Add(iNum1 As Integer) DIM Add AS INTEGER Some rules governing functions • Functions 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 • As 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

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

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

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

  10. The calling program DIM fHeight, fLen, fWid AS SINGLE DimfVol, fCostAS 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 ;” m3 “ PRINT “ at a cost of £”; fCost

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

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

  13. Main program 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 PRINTsMsg

  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.

More Related