0% found this document useful (0 votes)
30 views4 pages

Functions and Procedures

The document provides several QBASIC example programs demonstrating the use of functions and procedures. It includes programs for calculating the volume of a cylinder, area of a circle, curved surface area of a cylinder, circumference of a circle, and total and average marks for tests with validation. Each program illustrates the input of parameters and the use of functions or sub procedures to perform calculations.

Uploaded by

hlt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views4 pages

Functions and Procedures

The document provides several QBASIC example programs demonstrating the use of functions and procedures. It includes programs for calculating the volume of a cylinder, area of a circle, curved surface area of a cylinder, circumference of a circle, and total and average marks for tests with validation. Each program illustrates the input of parameters and the use of functions or sub procedures to perform calculations.

Uploaded by

hlt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Simple QBASIC example programs on functions and procedures

1. Write a QBASIC program to input radius and height of a cylinder


and calculate its volume [v=πr2h] using function.
Declare function vol(r, h)
Input "Enter radius"; rd
Input "Enter height "; ht
x = vol(rd, ht)
Print "Volume is "; x
End

Function vol (r, h)


vol = 3.14 * r * r * h
End Function

2. Write a QBASIC program to input radius of a circle and calculate


its area [a=πr2] using

Declare function area(r)


Input "Enter radius"; rd
x = area(rd)
Print "Area is"; x
End

Function area (r)


a = 3.14 * r * r
area = a
End Function
Simple QBASIC example programs on functions and procedures

3. Write a QBASIC program to input radius and height of a cylinder


and calculate its curved surface area [csa=2πrh] using sub
procedure.

Declare sub csa(r, h)


Input "Enter radius"; rd
Input "Enter height"; ht
Call csa(rd, ht)
End

Sub csa (r, h)


A = 2 * 3.14 * r * h
Print "CSA is"; A
End Sub

4. Write a QBASIC program to input radius of a circle and calculate


its circumference [circum=2πr] using sub procedure.

Declare sub circum(r)


Input "Enter radius"; rd
Call circum(rd)
End

Sub circum (r)


C = 2 * 3.14 * r
Print "Circumference is "; C
End Sub
Simple QBASIC example programs on functions and procedures

5. Write a QBASIC program to input 8 subject marks of two unit tests


and two term tests and output total and average for each set of
tests. Make sure to validate each subject mark.

MIN = 0
MAX = 100
Declare function totl(isl, dhi, eng, math, phy, chem, bio, optn)
Declare sub dats(isl, dhi, eng, math, phy, chem, bio, optn)
declare sub valid(mrk)

Print "INPUT UNIT TEST 1 MARKS AND OUTPUT TOTAL & AVERAGE"
Call dats(isl, dhi, eng, math, phy, chem, bio, optn)
UT1 = totl(isl, dhi, eng, math, phy, chem, bio, optn)
UAVG1 = avg(isl, dhi, eng, math, phy, chem, bio, optn)
Print "Unit Test 1 total is "; UT1
Print "Unit Test 1 average is ", UAVG1

Print "=============================================="

Print "INPUT TERM TEST 1 MARKS AND OUTPUT TOTAL & AVERAGE"
Call dats(isl, dhi, eng, math, phy, chem, bio, optn)
T1 = totl(isl, dhi, eng, math, phy, chem, bio, optn)
TAVG1 = avg(isl, dhi, eng, math, phy, chem, bio, optn)
Print "Term Test 1 total is "; T1
Print "Term Test 1 AVERAGE is "; TAVG1

Print "=============================================="

Print "INPUT UNIT TEST 2 MARKS AND OUTPUT TOTAL & AVERAGE"
Call dats(isl, dhi, eng, math, phy, chem, bio, optn)
UT2 = totl(isl, dhi, eng, math, phy, chem, bio, optn)
UAVG2 = avg(isl, dhi, eng, math, phy, chem, bio, optn)
Print "Unit Test 2 total is"; UT2
Print "Unit Test 2 average is ", UAVG2

Print "=============================================="

Print "INPUT TERM TEST 2 MARKS AND OUTPUT TOTAL & AVERAGE "
Call dats(isl, dhi, eng, math, phy, chem, bio, optn)
T2 = totl(isl, dhi, eng, math, phy, chem, bio, optn)
TAVG2 = avg(isl, dhi, eng, math, phy, chem, bio, optn)
Print "Term Test 2 total is"; T2
Print "Term Test 2 AVERAGE is "; TAVG2
End

Function totl (isl, dhi, eng, math, phy, chem, bio, optn)
x = isl + dhi + math + eng + phy + chem + bio + optn
totl = x
End Function
Simple QBASIC example programs on functions and procedures

Function avg (isl, dhi, eng, math, phy, chem, bio, optn)
y = (isl + dhi + math + eng + phy + chem + bio + optn) / 8
avg = y
End Function

Sub dats (isl, dhi, eng, math, phy, chem, bio, optn)
Input "Enter islam mark "; isl
Call valid(isl)
Input "Enter dhivehi mark "; dhi
Call valid(dhi)
Input "Enter english mark "; eng
Call valid(eng)
Input "Enter math mark "; math
Call valid(math)
Input "Enter phy mark "; phy
Call valid(phy)
Input "Enter chemistry mark "; chem
Call valid(chem)
Input "Enter biology mark "; bio
Call valid(bio)
Input "Enter optional subject mark "; optn
Call valid(optn)
End Sub

Sub valid (mrk)


While mrk < MIN Or mrk > MAX
Print "invalid mark"
Input "enter mark within 0 and 100 : ", mrk
Wend
End Sub

You might also like