Review Of Library Function In QBASIC Class 10 Computer Science

 Review Of Library Functions In QBASIC Class 10 Computer Science

Review Of Library Function In QBASIC Class 10 Computer Science
Review Of Library Function In QBASIC Class 10 Computer Science


 Introduction
 A function in QBASIC is a readymade program or user-made small program which helps us to perform a specific task. The function manipulates the data passed to it and returns a string value or a numeric value. QBASIC supports two different kinds of functions.
·         User-defined functions
·         Library functions
 
A user-defined function is created by a user whenever the user needs to perform a certain task that cannot be performed by using library functions. You can create user-defined functions by using the FUNCTION END FUNCTION statement.
 
Library functions are readymade programs that accept the data and return a value. These functions were written by the developer of QBASIC at the time of the development of QBASIC. QBASIC has a large set of library functions. These functions can be used in any QBASIC program to simplify some useful common functions like calculations of square root, length of the string, changing of case extracting characters, etc.
 
Library function is also known as built in function or routine function. A library function in QBASIC may be a string function for numeric function. All string library functions can manipulate either string or numeric data and return a string value. A function name having a dollar sign ($) is a string function. RIGHT$(). CHR$(), UCASE$(), DATE$(), etc are some examples of string function. A numeric library function can manipulate either string or numeric data and returns a numeric value. A numeric library function has no type declaration sign. LEN(), ASC(), ABS(), INT(), VAL(), etc are some examples of numeric library functions.
 
 Calling a function
To use a function in a program a function must be called. Calling a function means using the function in a program. When we call a function we need to use the function name and provide one or more data. These data are kept in parentheses and known as actual parameters or arguments. The argument can be constant or variable. A function returns a value for the result on the basis of arguments passed to it. Supplying arguments to a function is known as passing arguments. 
A function can be called in following ways:

a.          By using print statement.
A function can be called directly by using PRINT statement in order to display its return value.
For example: PRINT SQR (25)
Here, SQR function is called by using PRINT statement with passing constant value i.e. 25 as Argument and the returned value i.e. is displayed on the screen.

b.          Assigning returned value to a variable.
A function can be called by storing the returned value to a variable.
For example,
Sroot= SQR (25)
FirstNaam$=LEFT$(N$, 5) Where, N$=”Mohan Thapa”
In the first example, the returned value (i.e. 5) is stored in numeric variable Sroot.
In the second example, the returned value (i.e. Mohan is stored in string variable. 

c.  By using expression
A function can be called directly by using it in the expression. The example below shows the calling of function by using it in the expression.
For example,
IF LEN(“KATHMANDU”)>LEN(“POKHARA”) THEN
PRINT “YOU are correct”
END IF
 
Example:
CLS
LET A=25
IF SQR(A)>= 4 THEN
N=10+SQR(A)
ELSE
N=5+SQR(A)
END IF
PRINT N
END
 
Built-in String Manipulation Functions
QBASIC has many string manipulation functions which operate on string data in order to return either string or numeric data. Some of the string functions are discussed below:

LEN() FUNCTION
LEN function is a numeric function that returns a number of characters (including spaces if any) in a string, i.e. Length of a string.
Syntax:
              LEN(StringExpression$)
Example:
CLS
REM displays length of word.
A$ = ”Computer Science”
PRINT LEN (A$)
END
The output will be 16 as there are 16 characters including space.

UCASE$() FUNCTION
UCASE$ function is a string function that converts lowercase letters into uppercase and returns uppercase letters as a value.
Syntax:
              UCASE$(StringExpression$)
Where, StringExpression is any string expression.

LCASE$() FUNCTION
LCASE$ function is a string function that converts uppercase letters into lowercase and returns lowercase letters as a value.
Syntax:
              LCASE$(StringExpression$)
Where, StringExpression is any string expression.
 
Example:
CLS
REM Use of LCASE$ AND UCASE$ FUNCTION
LET CAPITAL$=”KATHMANDU”
LET COUNTRY$=” nepal”
PRINT” The capital of “ ; UCASE$(COUNTRY$) ;“ is “; LCASE$(CAPITAL$)
END
In the above program, the output will be “ The capital of NEPAL is Kathmandu.

LEFT$() FUNCTION
LEFT$ function returns number of characters from the left side of the string.
Syntax:
LEFT$(StringExpression, n)
Where, n is the number of characters to be extracted.

Example:
CLS
REM generates string pattern
LET A$=”NEPAL”
FOR I = 1 TO LEN(A$)
PRINT LEFT$(A$, I)
NEXT I
END

Output:
N
NE
NEP
NEPA
NEPAL

RIGHT$() FUNCTION
RIGHT$ function returns number of characters from the right side of the string.
Syntax:
RIGHT$(StringExpression, n)
Where, n is the number of characters to be extracted.

Example:
CLS
REM generates string pattern
LET A$=”NEPAL”
FOR I = 1 TO LEN(A$)
PRINT RIGHT$(A$, I)
NEXT I
END

Output:
L
AL
PAL
EPAL
NEPAL

MID$() FUNCTION     
MID$ function returns a number of characters from the specified location of a string.
Syntax:
MID$(StringExpression, m, n)

Where,
m is the position in a string from where the character is to be extracted
n is the number of characters to be extracted.

Example:
CLS
LET A$= “COMPUTER”
FOR I = 1 TO LEN(A$) STEP 2
PRINT MID$(A$, I, 1);  
NEXT I
END
OUTPUT:           C             M            U             E             
 
Example:
REM counts vowel letters in a given string
CLS
INPUT "ENTER ANY STRING"; S$
C = 0
FOR I = 1 TO LEN(S$)
A$ = MID$(S$, I, 1)
B$ = UCASE$(B$)
IF B$ = "A" OR B$ = "E" OR B$ = "I" OR B$ = "O" OR B$ = "U" THEN
C = C + 1
END IF
NEXT I
PRINT” Total number of vowel letter “; C
END

INPUT$() FUNCTION
The INPUT$ function returns a string of characters read from a keyboard.
Syntax:
              INPUT$(N)
Where,
 N is the number of characters to read.

Example:
CLS
PRINT” ENTER A WORD OF FIVE CHARACTERS”
A$=INPUT$(5)
PRINT ”YOU HAVE ENTERED “; A$
END

ASC FUNCTION
ASC function returns the ASCII code for the first character in a string expression.
Syntax:
           ASC (stringExpression$)
 
 Example:
CLS
PRINT ASC(“APPLE”)
PRINT ASC(“apple”)
END
This program displays 65 and 97 i.e. the ASCII value of A and a respectively.
 
 Note:
 The ASCII codes in decimal for commonly used characters are as follows
 Character                 ASCII code
 A to Z                                    65 to 90
 A to z                        97 to 122
0 to 9                          48 to 57
Enter Key                  13
Space Key                 32
Escape Key                27
 
Example:
REM converts lowercase into uppercase and vice-versa.
CLS
INPUT” ENTER A WORD”; W$
FOR P = 1 TO LEN(W$)
           E$=MID$(W$, P, 1)
           N=ASC(E$)
SELECT CASE N
CASE 65 TO 90
N=N+32
CASE 97 TO 122
N=N-32
END SELECT
WORD$=WORD$+CHR$(N)
NEXT P
PRINT” The word is”; WORD$
END
 
CHR$ FUNCTION
The CHR$ function returns a character of the corresponding ASCII in decimal.
Syntax:
CHR$(n)
Where,
 n is any number or numeric variable whose value ranges from 0 to 255.
 
Example:
REM displays corresponding character of ASCII code.
CLS
FOR P = 33 TO 122
C=C+1
PRINT “ASCII”; P; “represents “; CHR$(P)
IF C = 22 THEN
           PRINT” Press any key to continue”
C=0
A$=INPUT$(1)
END IF
NEXT P
END
 
VAL FUNCTION
The VAL function converts a string containing of numeric digits into a number and returns the number as a value.
Syntax:
        VAL(StringExpression$)
 
 Note:
·                  If the first character of a string is not a number, then the function returns 0
·                  If the string expression contains numbers with other characters, then it converts and returns the number which is before any other character. It means it discards the number which is after another character.

Example:
REM Use of VAL function
CLS
LET A$=”12ab13”
LET B$=”8A17”
LET DOB$=”05/07/2002”
PRINT VAL(A$)+ VAL(B$)
PRINT VAL(A$+B$)
PRINT” BIRTH MONTH IS”; VAL(DOB$)
END

STR$ FUNCTION
The STR$ function converts numbers into string data.
Syntax:
              STR$(n)
Note: In case of positive number, the string returned by a STR$() function contains a leading blank space.

Example:
REM converts decimal number into binary number.
CLS
INPUT” ENTER A DECIMAL NUMBER”; N
M=N
DO WHILE N<>0
R=N MOD 2
B$=STR$(R) +B$
N=INT(N/2)
LOOP
PRINT” The equivalent of “; M; “is “; VAL(B$)
END

LTRIM$ FUNCTION
The LTRIM$ function removes the leading spaces of a string and returns the string without leading spaces.
Syntax:
              LTRIM$(StringExpression$)

RTRIM$ FUNCTION
The RTRIM$ function removes the trailing spaces of a string and returns the string without trailing spaces.
Syntax:
              RTRIM$(StringExpression$)
Example:
CLS
A$=”    LOAD SHEDDING”
B$=”NEPAL      
PRINT B$;” IS SUFFERING FROM”; A$
PRINT RTRIM$(B$);” IS SUFFERING FROM”; LTRIM$(A$)
END

SPACE$ FUNCTION
The SPACE function returns a string of spaces.
Syntax:
              SPACE$(n%)
Where,
n% specifies a number of spaces that to be added.
Example:
CLS
LET A$=”SONGS”
LET B$=”MELLODIUS”
PRINTB$;A$
PRINT B$; SPACE$(2);A$
C$=B$+ SPACE$(1) + A$
PRINT C$
END

STRING$ FUNCTION
The string function returns a string of specified character to the specified times. This function repeats the character to the specified times.
Syntax:
              STRING$(Length%, (ASCII_code| StringExpression$))
Where,
Length% specifies the number of times that a string character to be repeated.
ASCII_Code is the ASCII code of a character of the stringexpression.
 
Example:
CLS
PRINT STRING$(5, “*”)
PRINT STRING$(4, “NEPAL”)
PRINT STRING$(8, 65)
END
The above program displays
*****
NNNN
AAAAAAAA

TIME$ FUNCTION
The TIME$ function returns the system time.
Syntax:
              TIME$
Example:
CLS
PRINT” Current time”; TIME$
End

TIME$ STATEMENT
The TIME$ statement is used for setting new system time.
Syntax:
        TIME$=StringExpression$
Where,
StringExpression is the string expression in hh: mm:ss format
Note:
The time format must be in 24 hours clock system.

Example:
CLS
PRINT” Current time is:”;TIME$
T$=”15:20:30”
TIME$=T$
PRINT” New system time is:”; TIME$
END

DATE$ FUNCTION
The DATE$ function returns the system date in string format as mm-dd-yyyy
Syntax:
              DATE$

DATE$ STATEMENT
The DATE$ statement is used for setting new system date.
Syntax:
              DATE$=string$
Where, String$ may be one of the formats: mm-dd-yyyy or mm/dd/yyyy

Example:
REM displays age of a person.
CLS
INPUT” Enter you date of birth(mm/dd/yyyy)”;db$
LET CurrentYear=VAL(RIGHT$(DATE$,4))
AGE=CurrentYear-VAL(RIGHT$(db$,4))
PRINT” You are”; AGE; “old”
END

INKEY$ FUNCTION
The INKEY$ function reads a character from the keyboard and returns the character. It is very useful to store a character pressed in the keyboard.
Syntax:
              INKEY$
Example:
CLS
PRINT “Enter a word”
DO
A$=INKEY$
W$=W$+A$
LOOP WHILE A$<> CHR$(13)
PRINT” The word you typed is”; W$
END
 
Mathematical Calculation Function
There are many mathematical functions that help us to do common calculation tasks easily. Some mathematical functions are described below:

ABS FUNCTION
ABS function returns the absolute value of a number. In other words, ABS function converts a negative number to a positive number.
Syntax:
              ABS(numeric_expression)
Where,
Numeric_expression may be positive or negative number.

Example:
CLS
REM use of ABS Function
PRINT ABS(12)
PRINT ABS(-13)
END
The output of the above program is
12
13

INT FUNCTION
INT function returns the largest integer less than or equal to a numeric expression.
Syntax:
              INT(numeric_expression)
Example:
CLS
PRINT INT(12.54)
PRINT INT(-99.4)
END
The output of the above program is
12
-100

SQR FUNCTION
This numeric function returns square root of a number.
Syntax:
              SQR(number)
Where,
Number is a positive number
Example:
CLS
LET P=25
PRINT SQR(P)
END
The output of the above program is 5.

SGN FUNCTION
The SGN function returns the sign of a number. SGn function returns 1, -1, or 0 for positive, negative or zero number respectively.
Syntax:
              SGN(NumericExpression)
Where,
NumericExpression may be positive or negative number.

Example:
CLS
REM Use of SGN function
PRINT SGN (23)
PRINT SGN (-121)
PRINT SGN (0)
END
The program displays 1, -1, and 0 respectively.

Example:
CLS
INPUT” Enter a number”;N
IF SGN(N)=1 THEN
PRINT” POSITIVE NUMBER”
ELSEIF SGN(N)=-1 THEN
PRINT” NEGATIVE NUMBER”
ELSE
PRINT” THE NUMBER IS ZERO”
END IF
END

CINT FUNCTION
The CINT function rounds a number and returns an integer value. A decimal value below 5 are rounded down and decimal value 5 or above 5 are rounded up.
Syntax:
              CINT(number)
Where,
Number may be in the range from -32768 to 32767

Example:
CLS
PRINT CINT(14.45)
PRINT CINT(15.50
PRINT CINT(17.75)
END

SIN, COS AND TAN FUNCTION
The SIN, COS and TAN functions are the trigonometric functions of QBASIC.
The SIN function returns the sine of a specified angle.
The COS function returns the cosine of a specified angle.
The TAN function returns the tangent of a specified angle.
Syntax:
SIN(angle)
COS(angle)
TAN(angle)
Where,
Angle must be in radian. The formula for converting degree into radian is radian=degree*(π/180)

Example:
CLS
REM displays values of Sin60, cos30, and tan45
CONST PI = 3.1416
PRINT” SIN60”;                 SIN(60*(PI/180))
PRINT” COS30”; COS(30*(PI/180))
PRINT” TAN45”; TAN(45*(PI/180))
END

Comments