You are on page 1of 11

FUNCTIONS and SUB PROCEDURES

Sunday, May 12, 2013

CONCEPT OF MODULAR PROGRAMMING FUNCTION: A function in QBASIC is a readymade program or user made (small) program which helps to perform a specific task. TYPES OF FUNCTIONS: QBASIC supports two types of function: a) User defined function b) Library function a) User defined function: A user defined function is created by a user whenever the user needs to perform a certain task that cannot be performed by using a library function. ( a user defined function is created by using FUNCTION .. END FUNCTION statement) b) Library function: Library functions are the ready- made programs that accept data and returns a value. These functions are written by the developer of QBASIC at the time of development of QBASIC. Library functions are also called routine functions or built-in-functions. The two commonly used library functions are: i) String functions/manipulators (e.g. LEN, RIGHT$, MID$ etc) ii) Mathematical functions (e.g. ABS, INT, MOD etc) Linear programming: When a program becomes complex and unmanageable, this type of programming structure is called linear programming. Modular programming: The way of writing a program by breaking the complex program into small, logical and manageable part is known as modular programming. Module/Procedure: The small logical and manageable part of the program is called a module or procedure, which sometimes are also referred as a sub program or a sub module, that contains codes i.e. instructions for performing a certain task. (since the modular programming uses small block of functional codes it is also called structured programming) Advantages of procedure/module: a) A procedure can be reused more than once without rewriting it, so it reduces the length of a program

b) The procedures can be tested and debugged separately. c) Improves the readability of the program. Note : A modular program has a main module and may have many sub-modules. The main module contains the entry and ending point of the program. The statements or codes written in a main module are known as module level codes. Concept of passing parameters/arguments to a function or procedure: There are two ways to pass parameters to a function or procedure. They are: a) Passing by value b) Passing by reference a) Passing by value: When arguments are passed by value , it does not make any effect to the value of the variables which are passed to a procedure even if they are changed in the procedure. ( to pass arguments by value method each argument is enclosed in individual parenthesis in the calling statement ) b) Passing by reference: The address of the variables are passed to the procedure, that is the parameter uses the memory location of the argument. If the value of variable is changed in the procedure then it changes the value of the variable in the main module. Concept of local and global variable Local variable: A variable which is declared inside a module and which cannot be accessed by all the modules is known as local variable. ( All variables that is declared implicitly or explicitly without using SHARED attribute are local variables ) Global variable: A variable in main module which can be accessed from any sub module or a procedure of a program is known as global variable. ( a global variable is declared in the main module by using DIM or COMMON statement with SHARED attribute ) Example: COMMON SHARED <VARIABLE LIST> DIM SHARED <VARIABLE LIST>

Types of procedure: There are two types of procedure: a) FUNCTION procedure b) SUB procedure FUNCTION Procedure: A FUNCTION procedure performs the specific task and returns a value to the main program. To use the FUNCTION procedure it is needed to CALL. Since a function procedure returns a value, it is called either by using PRINT statement or storing a returned value into a variable. A FUNCTION procedure is defined by using FUNCTION . END FUNCTION Syntax FUNCTION NAME <PARAMETERLIST> < STATEMENTS > NAME = <EXPRESION> END FUNCTION

NAME = Where NAME is a name of a FUNCTION procedure . The rule for naming a function is the same as rules for naming a variable. Since a function returns a value, a function may be a string or numeric. A string function name is followed by the type declaration symbol dollar ($) whereas numeric function name may be followed by either %, &, ! Or # symbol. Parameter list: parameter list is the list of variables that accepts the values, passes to it when the function is called. Each variable or constant is separated by commas. Expression: Expression is the value that has to be returned to the calling module. DECLARE: Function-Declares a FUNCTION or SUB procedure and invokes argument, data type checking. Syntax DECLARE {FUNCTION/SUB} NAME (PARAMETER LIST) Example 1. Program to find the sum of two values using FUNCTION END FUNCTION DECLARE FUNCTION SUM ( A, B ) CLS INPUT ENTER THE FIRST VALUE; A INPUT ENTER THE SECOND VALUE;B PRINT THE SUM OF TWO VALUES; SUM (A,B) END FUNCTION SUM (A,B) SUM= A+B END FUNCTION Example 2. Program to find the average of two values DECLARE FUNCTION AVG ( X, Y ) CLS INPUT ENTER THE FIRST VALUE; X INPUT ENTER THE SECOND VALUE;Y PRINT THE AVERAGE OF TWO VALUES; AVG (X,Y) END FUNCTION AVG (X,Y) AVG= (X+Y)/2 END FUNCTION

SUB Procedure: A SUB procedure is a small logical and manageable part of a program which performs the specific task and does not return any value. A SUB procedure is called by using the CALL statement. A SUB procedure is defined by using SUB . END SUB statement. SUB .. END SUB Function : The SUB statement is used to define a sub procedure. Syntax: SUB NAME <PARAMETERLIST> STATEMENT BLOCK

END SUB CALL: Function: Transfers control to a SUB procedure. Syntax : CALL NAME <PARAMETERLIST> Example 1. Program to find the sum of two values using SUB END SUB REM PARAMETER PASSING BY REFERENCE DECLARE SUB SUM (A,B) CLS INPUT ENTER FIRST VALUE;A INPUTENTER SECOND VALUE;B CALL SUM (A,B) END SUB SUM (A,B) SUMT=A+B PRINT THE SUM OF TWO VALUES;SUMT END SUB Example 2. Program to find the sum of two values using SUB END SUB REM PARAMETER PASSING BY VALUE DECLARE SUB SUM (A,B) CLS CALL SUM (7,9) END SUB SUM (A,B) SUMT=A+B PRINT THE SUM OF TWO VALUES;SUMT END SUB

Example 3. Program to find the average of two values DECLARE SUB AVG ( X, Y ) CLS INPUT ENTER THE FIRST VALUE; X INPUT ENTER THE SECOND VALUE;Y CALL AVG (X,Y) END SUB AVG (X,Y) AVGT= (X+Y)/2 PRINT THE AVERAGE OF TWO VALUES; AVGT END SUB i) 1. Sample Programs using FUNCTION .. END FUNCTION: Program to find the circumference of a circle

DECLARE FUNCTION CIRC(R) CLS INPUT ENTER THE RADIUS OF THE CIRCLE; R PRINT THE CIRCUMFERENCE OF THE CIRCLE; CIRC(R) END FUNCTION CIRC(R) CIRC = 2*3.14*R END FUNCTION 2. Program to find the area of a rectangle DECLARE FUNCTION RECT ( L,B) CLS INPUT LENGTH OF RECTANGLE; L INPUT BREADTH OF RECTANGLE;B PRINT THE AREA OF RECTANGLE; RECT(L,B) END FUNCTION AREA (L,B) AREA = L*B END FUNCTION 3. Program to find the volume of a cylinder DECALRE FUNCTION VOL (R,H) CLS INPUT INPUT RADIUS;R INPUT INPUT HEIGHT;H PRINT THE VOLUME OF CYLINDER; VOL(R,H) END FUNCTION VOL (R,H) VOL=3.14*R^2*H END FUNCTION ii) 1. Sample Programs using SUB .. END SUB: Program to generate the 1,1,2,3,5 series upto 10th term. DECLARE SUB XYZ () CLS CALL XYZ END SUB XYZ X=1 Y=1 FOR I= 1 TO 10

PRINT X Z=X+Y X=Y Y=Z NEXT END SUB 2. Program to generate the following series 5,10,15,20 upto 10th term DECLARE SUB SERIES () CLS CALL SERIES END SUB SERIES N=5 FOR I= 1 TO 10 PRINT N N=N+5 END SUB 3. Program to generate the following series 2,22,222 upto 10th term DECLARE SUB SERIES CLS CALL SERIES END SUB SERIES N=2 FOR I=1 TO 10 PRINT N N=N*10+2 END SUB

Examples of programs using string manipulators: a. Program to reverse a string which is passed as a parameter using SUB END SUB DECLARE SUB REV (N$) N$=NEPAL CALL REV (N$) END SUB REV (N$) FOR I = LEN (N$) TO 1 STEP -1 R$ = MID$ (N$, I, 1) PRINT R$ NEXT I

END SUB

b. Program to reverse a string which is given (input) by the user. DECLARE SUB REV (W$) CLS INPUTENTER A STRING; W$ CALL REV (W$) END SUB REV (W$) FOR I = LEN(W$) TO 1 STEP -1 R$ = MID$ (W$, I, 1) PRINT R$ NEXT I END SUB c. Program to check the entered string is Palindrome or not. DECLARE SUB PAL (S$) CLS INPUTENTER A STRING; S$ REV$ = PAL (S$) IF REV$= S$ THEN CALL PALL (S$) ELSE PRINT THE WORD IS NOT A PALINDROME END SUB PAL (S$) L= LEN (S$) FOR I = L TO 1 STEP -1 REV$ = REV$ + MID$ (S$, I, L) NEXT I PAL$ = REV$ PRINT THE WORD IS A PALINDROME END SUB d. Program to find vowels from a entered word DECLARE SUB VOWEL (V$) CLS INPUT ENTER A STRING; V$ CALL VOWEL (V$) END SUB VOWEL (V$) L = LEN (V$) FOR I = 1 TO L

CH$ = MID$ (V$, I, L) IF CH$ = A OR CH$= a OR CH$ = E OR e OR CH$ = I OR CH$ = i OR CH$ = O OR CH$ = o OR CH$ = U OR CH$ =u THEN PRINT CH$; END IF NEXT I END SUB e. Program to find consonant from a entered word DECLARE SUB XYZ CLS CALL XYZ END SUB XYZ INPUT ENTER A WORD; W$ FOR I = 1 TO LEN (W$) X$ = MID$ (W$, I, 1) X$= UCASE$(X$) IF X$ <> A AND X$ <> E AND X$ <> I AND X$ <> O AND X$ <> U THEN C = C+1 NEXT PRINT TOTAL CONSONANT = ;C END SUB *** Examples of General programs*** a) Program to convert Celsius to Fahrenheit where F = (9/5 * C ) + 32 DECLARE SUB CTOF ( C ) CLS INPUT ENTER DEGREE IN CELSIUS ; C CALL CTOF ( C ) END SUB CTOF ( C ) F = (9 * C /5 ) + 32 PRINT IN FAHRENHIET ; F END SUB

b) Program to calculate simple interests DECLARE SUB SIM (P,T,R,) CLS INPUT ENTER PRINCIPAL AMOUNT; P INPUT ENTER TIME; T

INPUT ENTER RATE; R CALL SIM (P,T,R) END SUB SIM (P,T,R) SI = ( P*T*R )/100 PRINT SIMPLE INTEREST =; SI END SUB -------------------------------------------------------------------------------------------------------------

File processing /File Handling in Qbasic Types of files in QBasic a. i) Program file b. ii) Data file Program file: A program file has a set of instruction that is needed for processing data whatever tasks needs to be performed that is done by executing a program file. Program files do not store data for future use. Data file: A data file is a collection of related data stored in a secondary storage. In order to store the output of the program in the disk for later use, data file needs to be created using a program. Types of data files Depending on the way in which the data is stored and accessed. Data files are categorized as: Sequential access file Random access file Sequential access file: In a sequential access data file, data are stored in sequential order. The data of a sequential access file can only be accessed sequentially. Disadvantages of sequential access file. A particular record of a file cannot be accessed directly. So the time taken to access the data is longer. Random access file : A user can read or write any record directly in a random access file. Thus, reading and writing of data is faster compared to sequential access file. Opening a data file Points to remember I. A sequential data file is needed to open before it can be used. II. Opening of a data file could be for different purposes such as: a. To write data b. To read data

i. ii.

a.

c.

To delete or add some more data etc. As the requirement, a sequential files need to be opened in different mode. The different mode and their purpose are as follows.

MODE OUTPUT INPUT APPEND

Purpose To create a new data file and write data. To retrieve or read the contents of an existing data file. To add more records in the existing data file

File-Input-Output Statements The commands under this group enables us to save data to a file. So that the file can be retrieved later, for printing, adding more data at the end of the file or for modifying the saved data. The commands under Input /Output files are : OPEN OUTPUT INPUT APPEND LINE INPUT WRITE CLOSE EOF OPEN FUNCTION Open a file or device. Syntax Open <File name> FOR <MODE> AS # FILENUMBER OUTPUT FUNCTION- To create a new data file and write data. Syntax: OPEN <FILENAME> FOR <OUTPUT>AS # FILENUMBER INPUT FUNCTION : Reads the input from the keyboard or a file. Syntax: Open < FILE NAME > FOR < INPUT> AS # FILENUMBER APPEND FUNCTION : Allows to add records to an existing data file. Syntax : OPEN < FILENAME> FOR < APPEND> AS # FILENUMBER LINE INPUT FUNCTION : It reads a line of up to 255 characters from a keyboard or a file. Syntax : LINE INPUT # Filename %, Variable $ WRITE FUNCTION- It writes data to a string or a sequential file.

a. b. c. d. e. f. g. h.

Syntax- WRITE # FILENUMBER%, expression list. CLOSE FUNCTION- This command closes one or more open files or devices. CLOSE without any argument closes all open files and devices. Syntax- CLOSE # FILENUMBER, FILENUMBER n EOF FUNCTION- EOF test for the end of a file. EOF returns true if the end of the file has been reached. Syntax- EOF (FILE NUMBER %)

You might also like