Presentation is loading. Please wait.

Presentation is loading. Please wait.

E0001 Computers in Engineering Procedures: subprograms and functions.

Similar presentations


Presentation on theme: "E0001 Computers in Engineering Procedures: subprograms and functions."— Presentation transcript:

1 E0001 Computers in Engineering Procedures: subprograms and functions

2 Readings and tutorial n Schneider sections 4.1 and 4.2 –practice problems and exercises 4.1 –practice problems and exercises 4.2 n For Practice –Schneider practice problems 3.4; numbers 57,60,62,66,68 –additional problems provided by the tutor

3 This lecture: n subroutines (subprograms) n user defined functions n local and global variables

4 Exam 2% n Week 11 n 20 multiple choice questions on –IF THEN and SELECT CASE –INPUT, PRINT, READ AND DATA –SUBS AND FUNCTIONS –PRINT USING –GENERAL KNOWLEDGE

5 Procedure n group of self-contained statements (modules that perform a specific task) e.g. displaying a menu n suited to structured programming –break problem into small piece –each piece becomes a procedure –reuse procedures whenever needed

6 Kinds of procedures n two kinds of procedures –subprograms –functions user defined built in

7 Program ‘MAIN program to calculate volumes REM length, width,depth INPUT l,w,d [CALL] volume (l,w,d,v) PRINT v END SUB volume (l,w,d,v) v=l*w*d END SUB lwdlwd 101010 MAIN PROGRAM VARIABLES Sub program variables lwdv 1010100 1000 v0v0

8 Subroutine n [CALL] subprogram [(argumentlist)] –CALL average (a,b,c,avg) –average a,b,c,avg n SUB subprogram (parameterlist) –SUB average (x,y,z,avg) sub program name must match name used in CALL statement arguments and parameters must match in number, order and type of variables BUT NOT NECESSARILY NAME argument list of CALL statements can also contain data (numbers) and arithmetic expressions –

9 For example CALL draw (x,y$,z) will look for a sub program call “draw”. The name on the subprogram must match EXACTLY Valid subprogram statements: SUB draw (x,y$,z) SUB draw (a,b$,z) SUB draw (number, name$, age)

10 CALL add (2,3) SUB add (x,y) PRINT “The sum of”;x;”and”;y;”is”;x+y END SUB SUB add (x,y) PRINT “The sum of”;x;”and”;y;”is”;x+y END SUB MAIN PROGRAM CALL add (2,3) A = 4 b = 5 CALL add (A, b) CALL add (b+1, A*5) SUB add (x,y) The sum of 2 of 3 is 5 The sum of 4 and 5 is 9 CALL add (A,b) SUB add (x,y) PRINT “The sum of”;x;”and”;y;”is”;x+y END SUB The sum of 6 and 20 is 26 CALL add (b+1, A*5) SUB add (x,y)

11 Subprograms n executed with (implied) CALL statement n designed to perform a particular task n section of code that is executed from another section of code n can be called from any place in a program n returns control to statement that called it n returns 0 or more values n DO NOT USE GOSUB!!!

12 This is the main section of code entered in the usual way. It calls a subroutine called “trivial” To enter the subroutine use the Edit Pull Down menu - NEW Sub

13 Enter the sub name only, not variables

14 The SUB name and END SUB are written automatically. If necessary add the sub parameters now and type the body of the sub. The F2 key will show you other subs in the program and allow you to select which “page”you wish to view.

15

16 MAIN PROGRAM x=2 PRINT “mainx=“;x CALL trivial PRINT “mainx=“;x CALL trivial PRINT “mainx=“;x END SUB trivial PRINT “trivial x=“;x x = 3 PRINT “trivial x = “;x END SUB

17 Local Variables n Local variable - variables used in subroutines or functions which are not passed into the sub or function are said to be local. The values of local variables cannot be used by other subs or functions or by the main body of the program unless passed in the argument list

18 Global Variables n Global variable - may be used anywhere in the program n declared with a DIM SHARED statement –DIM SHARED argumentlist –DIM SHARED a,b,c,avg a,b,c,avg are available to all functions and subs without being passed in the argument list

19 Add the DIM SHARED statement. This means that x is now a GLOBAL variable available to all sub, functions and the main routine

20 This now changes the output significantly. The value for x is shared between the sub and the main routine

21 Parameter Passing n Items in the parentheses of CALL - arguments –arguments can be CONSTANTS VARIABLES EXPRESSIONS n Items in the parentheses of SUB - parameters –parameters MUST be variables n Names of arguments & parameters NEED NOT MATCH n Position, type and Number MUST match

22 DECLARE statements n DECLARE SUB name (variables) n comes at the beginning of the program n done automatically by qbasic on FIRST SAVE

23 DECLARE DONE AUTOMATICALLY ON FIRST SAVE

24 DIM SHARED and data types n DIM can still be used to define data types n DIM SHARED x AS SINGLE, Y AS DOUBLE, a AS STRING etc n any variable NOT dimensioned as SHARED will be LOCAL

25 What is the output? Call ConvertMoney (“Mark”, 5,.6165) End Sub ConvertMoney (currency$, dollars, Factor) money = dollars * Factor PRINT dollars; “dollars equal to”; money, currency$; “s” END SUB

26 What is the output? ‘main program M=5 n=8 CALL calc (M, n, total) PRINT M, n, total END ‘sub program SUB calc (sum, A, B) sum = A + B END ANS is 8 8 0

27 Remember “built in” Functions n Look at the statement which uses a built in functions x = SQR(y)total = LEN(“jack”) variable = Function(arguments) n returns ONE value into variable i.e. after the function is executed the variable has the “answer” or the returned value

28 Write a function to calculate the volume of a cone - use diameter and height as inputs REM main program INPUT diam, height volume = Cone (diam, height) PRINT volume END FUNCTION Cone (d,h) r = d/2 ….. Cone = 3.14 * r^2*h/3 END FUNCTION

29 Take a closer look at the main program REM main program INPUT diam, height volume = Cone (diam,height) PRINT volume END n diam and height carried as parameters into function Variable = function (arguments) expression is used to call function n print variable which has the returned value from the function

30 Take a closer look at the Function FUNCTION Cone (d,h) r = d/2 ….. Cone = 3.14 * r^2*h/3 END FUNCTION Function name matches the name in expression volume = Cone (diam, height) number and type of variables match I.e diam d and height h function name =expression calculated value of expression stored in function name and returned to main

31 Comparison of SUBs and FUNCTIONS INPUT d, h CALL cone(d,h,v,s) PRINT v,s END SUB cone (x,y,z,s) r = x/2 z = calc for volume s = calc for surface area END SUB INPUT diam, height volume = Cone (diam, height) PRINT volume END FUNCTION Cone (d,h) r = d/2 Cone = 3.14 * r^2*h/3 s = calc for surface ares PRINT S END FUNCTION d=>x; h=>y; v=>z; s=>s x=>d; y=>h; z=>v; s=>s diam=>d; height=>h d=>diam; height=>h; Cone=>volume

32 DIFFERENCES? n SUBs n can take any number of variables, constants and expressions to sub n returns one or more variables n use extra arguments to return more to MAIN n executed with a [CALL] statement n FUNCTIONS n can take any number of variables, constants and expressions to sub n returns ONE value n returns value in function name with an expression n executed (called) with an EXPRESSION

33 User-defined Functions n customised to suit user n call with an expression n returns a single value n names for functions follow the same rules as variables n used for repetitive mathematical functions n value returned with function name (expression)

34 To create a Function n In Qbasic - Edit, new function OR n on a new line in Qbasic type –FUNCTION name and then ENTER e.g. function volume (this will also work for subs) –a new screen will appear with FUNCTION and END FUNCTION –F2 will return to MAIN program

35 RULES n Can call a FUNCTION from within a SUB n Can call a SUB from within a SUB (but not recommended) n Can call a SUB from within a FUNCTION (but bad structure) n number, type and order of parameters and arguments MUST match but not necessarily the name

36 Here is a program to determine the area of a rectangle. Rewrite it using a function CLS READ length, width LET area = length *width PRINT “Area is”;area DATA 4,5 CLS READ length, width a = rect (length, width) PRINT “Area is”;a END FUNCTION rect (l,w) rect = l*w END FUNCTION

37 Write a program with a function that converts Celsius to Fahrenheit. F = 9/5C +32 INPUT celsius f = convert (celsius) PRINT f END FUNCTION convert (c ) convert = 9/5*c + 32 END FUNCTION INPUT celsius PRINT convert (celsius) END FUNCTION convert (c ) convert = 9/5*c + 32 END FUNCTION


Download ppt "E0001 Computers in Engineering Procedures: subprograms and functions."

Similar presentations


Ads by Google