E0001 Computers in Engineering Procedures: subprograms and functions.

37
E0001 Computers in Engineering Procedures: subprograms and functions

Transcript of E0001 Computers in Engineering Procedures: subprograms and functions.

Page 1: E0001 Computers in Engineering Procedures: subprograms and functions.

E0001 Computers in Engineering

Procedures: subprograms and functions

Page 2: E0001 Computers in Engineering Procedures: subprograms and functions.

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.

This lecture:

subroutines (subprograms) user defined functions local and global variables

Page 4: E0001 Computers in Engineering Procedures: subprograms and functions.

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.

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.

Kinds of procedures

two kinds of procedures– subprograms– functions

• user defined• built in

Page 7: E0001 Computers in Engineering Procedures: subprograms and functions.

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.

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.

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.

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.

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.

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.

Enter the sub name only, not variables

Page 14: E0001 Computers in Engineering Procedures: subprograms and functions.

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.
Page 16: E0001 Computers in Engineering Procedures: subprograms and functions.

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.

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.

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.

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.

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.

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.

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.

DECLARE DONE AUTOMATICALLY ON FIRST SAVE

Page 24: E0001 Computers in Engineering Procedures: subprograms and functions.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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