e0001 computers in engineering procedures: subprograms and functions
TRANSCRIPT
![Page 1: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/1.jpg)
E0001 Computers in Engineering
Procedures: subprograms and functions
![Page 2: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/2.jpg)
Readings and tutorial
Schneider sections 4.1 and 4.2– practice problems and exercises 4.1– practice problems and exercises 4.2
For Practice– Schneider practice problems 3.4; numbers
57,60,62,66,68– additional problems provided by the tutor
![Page 3: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/3.jpg)
This lecture:
subroutines (subprograms) user defined functions local and global variables
![Page 4: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/4.jpg)
Exam 2%
Week 11 20 multiple choice questions on
– IF THEN and SELECT CASE– INPUT, PRINT, READ AND DATA– SUBS AND FUNCTIONS– PRINT USING– GENERAL KNOWLEDGE
![Page 5: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/5.jpg)
Procedure
group of self-contained statements (modules that perform a specific task) e.g. displaying a menu
suited to structured programming– break problem into small piece– each piece becomes a procedure– reuse procedures whenever needed
![Page 6: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/6.jpg)
Kinds of procedures
two kinds of procedures– subprograms– functions
• user defined• built in
![Page 7: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/7.jpg)
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*dEND SUB
l w d
10 10 10
MAIN PROGRAM VARIABLES
Sub program variablesl w d v
10 10 10 0 1000
1000
v
0
![Page 8: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/8.jpg)
Subroutine [CALL] subprogram [(argumentlist)]
– CALL average (a,b,c,avg)– average a,b,c,avg
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
–
![Page 9: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/9.jpg)
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)
![Page 10: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/10.jpg)
CALL add (2,3)
SUB add (x,y)
PRINT “The sum of”;x;”and”;y;”is”;x+yEND SUB
SUB add (x,y)PRINT “The sum of”;x;”and”;y;”is”;x+yEND 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+yEND SUB
The sum of 6 and 20 is 26
CALL add (b+1, A*5)
SUB add (x,y)
![Page 11: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/11.jpg)
Subprograms executed with (implied) CALL statement designed to perform a particular task section of code that is executed from
another section of code can be called from any place in a program returns control to statement that called it returns 0 or more values DO NOT USE GOSUB!!!
![Page 12: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/12.jpg)
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
![Page 13: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/13.jpg)
Enter the sub name only, not variables
![Page 14: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/14.jpg)
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.
![Page 15: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/15.jpg)
![Page 16: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/16.jpg)
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
![Page 17: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/17.jpg)
Local Variables
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
![Page 18: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/18.jpg)
Global Variables
Global variable - may be used anywhere in the program
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
![Page 19: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/19.jpg)
Add the DIM SHARED statement. This means that x is now a GLOBAL variable available to all sub, functions and the main routine
![Page 20: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/20.jpg)
This now changes the output significantly. The value for x is shared between the sub and the main routine
![Page 21: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/21.jpg)
Parameter Passing Items in the parentheses of CALL - arguments
– arguments can be • CONSTANTS• VARIABLES• EXPRESSIONS
Items in the parentheses of SUB - parameters– parameters MUST be variables
Names of arguments & parameters NEED NOT MATCH
Position, type and Number MUST match
![Page 22: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/22.jpg)
DECLARE statements
DECLARE SUB name (variables) comes at the beginning of the program done automatically by qbasic on FIRST
SAVE
![Page 23: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/23.jpg)
DECLARE DONE AUTOMATICALLY ON FIRST SAVE
![Page 24: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/24.jpg)
DIM SHARED and data types
DIM can still be used to define data types
DIM SHARED x AS SINGLE, Y AS DOUBLE, a AS STRING etc
any variable NOT dimensioned as SHARED will be LOCAL
![Page 25: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/25.jpg)
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
![Page 26: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/26.jpg)
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
![Page 27: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/27.jpg)
Remember “built in” Functions
Look at the statement which uses a built in functions
x = SQR(y) total = LEN(“jack”)
variable = Function(arguments) returns ONE value into variable i.e. after
the function is executed the variable has the “answer” or the returned value
![Page 28: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/28.jpg)
Write a function to calculate the volume of a cone - use diameter and height as inputs
REM main program
INPUT diam, heightvolume = Cone (diam, height)
PRINT volume
END
FUNCTION Cone (d,h)
r = d/2
…..
Cone = 3.14 * r^2*h/3
END FUNCTION
![Page 29: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/29.jpg)
Take a closer look at the main program
REM main programINPUT diam, height
volume = Cone (diam,height)
PRINT volume
END
diam and height carried as parameters into function
Variable = function (arguments)
expression is used to call function
print variable which has the returned value from the function
![Page 30: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/30.jpg)
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
![Page 31: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/31.jpg)
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
![Page 32: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/32.jpg)
DIFFERENCES? SUBs can take any number
of variables, constants and expressions to sub
returns one or more variables
use extra arguments to return more to MAIN
executed with a [CALL] statement
FUNCTIONS can take any number
of variables, constants and expressions to sub
returns ONE value returns value in
function name with an expression
executed (called) with an EXPRESSION
![Page 33: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/33.jpg)
User-defined Functions customised to suit user call with an expression returns a single value names for functions follow the same rules
as variables used for repetitive mathematical functions value returned with function name
(expression)
![Page 34: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/34.jpg)
To create a Function
In Qbasic - Edit, new function
OR on a new line in Qbasic type
– FUNCTION name and then ENTER e.g.function volume <enter>• (this will also work for subs)
– a new screen will appear with FUNCTION and END FUNCTION
– F2 will return to MAIN program
![Page 35: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/35.jpg)
RULES
Can call a FUNCTION from within a SUB Can call a SUB from within a SUB (but
not recommended) Can call a SUB from within a FUNCTION
(but bad structure) number, type and order of parameters
and arguments MUST match but not necessarily the name
![Page 36: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/36.jpg)
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
![Page 37: E0001 Computers in Engineering Procedures: subprograms and functions](https://reader035.vdocuments.us/reader035/viewer/2022062718/56649eb55503460f94bbe866/html5/thumbnails/37.jpg)
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