BASIC Programming
1. Sub-programs
A sub-program is a group of statements that perform a single operation on execution. They are easy to design, debug, modify and document. Sub-programs are written once in a program but can be used several times in that program
There are two types of sub-programs
1.
Functions
2.
Subroutines
2. functions
functions perform arithmetic and string operations. They make the programming much simple and reduce the size of program and also save time.
A function is a routine that takes a value as input and
produce a value as output.
There are two types of functions;
2.1 system defined/ built in functions
these functions are also called library functions, they are included in the BASIC. They are used in programs. Functions are divided into two categories;
1.
Numeric functions
2.
String functions
2.1.1 Numeric Functions
ABS function:- this function provides the absolute value of
the expression x(the value without any sign). Its format is ABS(x)
Program
10 input “enter a number”, x
20 print “the absolute value is “ABS(x)
30 end
Output of the program
enter a number -5
the absolute value is 5
CINT function:- this function converts the numeric
expression into integer by rounding the fractional part of the program. Its
format is CINT(x)
Program
10 input “enter a number”, x
20 print “the integer converted value is “,CINT(x)
30 end
Output of the program
enter a number 5.9
the integer converted value is 6
INT function:- this function prints the positive whole
numbers as same regardless of their value and negative whole numbers as rounded
down to the next smaller value its format is INT(x).
Program
10 input “enter a number”, x
20 print “the integer value is “,INT(x)
30 end
Output of the program
enter a number 5.9
the integer value is 5
RND function:- this function is also referred as Random
Number Generator. It generates a number between 0 and 1. Its format is RND(x).
SGN function:- this function is referred to as Sign
function. Which reports the sign of a number which can be equal to -1 (when the
value of x is negative),1 (when the value of x is negative)and 0 when x is 0.
Its format is SGN(x)
Program
10 print “the value of -5 after using sgn function is”,
INT(-5)
20 print “the value of -5 after using sgn function is”,
INT(5)
30 print “the value of -5 after using sgn function is”,
INT(0)
Output of the program
the value of -5 after using sgn function is -1
the value of -5 after using sgn function is 1
the value of -5 after using sgn function is 0
FIX function:- FIX function is used to obtain an integer
value by simply dropping off the decimal point and numbers to the right of it.
Its format is FIX(x).
Program
10 input “enter a number” , x
20 print “the integer
value is”,FIX(x)
30 end
Output
Enter a number 5.67
the integer value is 5
SQR function:- this function finds the square root of any
positive number. Its format is SQR(x)
Program
10 input “enter a positive number to get its square root”, x
20 print “ the square root is”, SQR(x)
30 end
Output
Enter a positive number to get its square root 9
The square root is 3
LOG function:- this function is used to find the value of
natural logarithm. Its format is LOG(x).
Program
10 input “type a number”, x
20 print “natural log is”, LOG(x)
30 end
Output
Type a number 10
Natural log is 2.302585
SIN function:- the purpose of SIN function is to find the
trigonometric ration called sine of an angle x expressed in radians. Its format
is SIN(x)
Program
10 input “type the angle in radians”, x
20 print “radians =”,SIN(x)
30 end
Output
Type the angle in radians 2
Radians=
COS function:- the purpose of COS function is to find the
trigonometric ration called cosine of an angle x expressed in radians. The
format is COS(x).
Program
10 print COS(1)
20 end
Output
0.540302
TAN function:- the purpose of TAN function is to find the
trigonometric ratio called tangent of an angle x expressed in radians. The
format is TAN(x).
Program
10 print TAN(4)
20 end
Output
1.157821
13.5 String Functions
HEX$ function:- this function calculates hexadecimal number
of given decimal number x. its format is HEX$(x)
Program
10 print HEX$(20)
20 end
Output
14
LEFT$ function:- this function is used to select n left most
characters of a given string. Its format is LEFT$($,n)
Program
10 print LEFT$(“Balochistan”,6)
20 end
Output
Baloch
RIGHT$ function:- this function is used to select n right
most characters of a given string. Its format is RIGHT$($,n).
Program
10 print RIGHT$(“Baloch Khan”,4)
20 end
Output
Khan
INKEY$ function:- this function responds to any key from the
keyboard without pressing the enter key.
Program
10 print “press any key to continue”
20 let A$=INKEY$
30 if A$=”” then goto 10
40 print A$
50 end
Output
Press any key to continue
F
F
SPACE$ function:- the purpose of SPACE$ function is to leave
certain number of spaces before a character. Its format is SPACE$(n) or SPC(n)
Program
10 print “Name”, SPACE$(21),”Kit No”
20 end
Output
Name
Kit No
2.2 User Defined Functions
besides providing the built in functions, BASIC has the statement DEF FN that lets you define your own functions. These are called user defined functions.
Program
10 DEF FNSUM(x,y)=x+y
20 print FNSUM(5,8)
30 end
Output
13
3.Subroutine Subprogram
in programming often large programs are divided into some short programs called subroutines. In BASIC a subroutine is a series of statements that is written once in a program but it may be used a number of times in the program.GOSUB / RETURN Statement
the GOSUB statement will cause the transfer of control to the subroutines. Its format is Line# GOSUB #.The RETURN statement passes control back to the statement immediately following the SOSUB instruction that has called the SUBROUTINE.
Program
10 print “this is a subroutine program”
20 GOSUB 50
30 print “this print message is written before subroutine
but displays after the subroutine”
40 end
50 a=10,b=20,c=a+b
60 print c
70 RETURN
Output
this is a subroutine program
30
This print message is written before subroutine but displays
after the subroutine
4.File Handling
a computer can process large amounts of data. It is always useful to store input and output in a data file. A file consists of records stored on a secondary storage.4.1 Open and Close statements
the Open and Close statements are used for opening file for processing
and Close it after necessary processing.
No comments:
Post a Comment