Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
87 views

Structure of Qbasic Functions

Uploaded by

ADAMU MUSA SANI
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)
87 views

Structure of Qbasic Functions

Uploaded by

ADAMU MUSA SANI
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/ 3

7.

FUNCTIONS AND SUBROUTINES


PROCEDURE OR MODULE
A procedure or module is a separate block of code which performs specific tasks. A procedure can be called by its
name.
QBASIC has two types of procedure.

FUNCTION SUBROUTINE or SUB


It is a built-in formula to perform a specific task and It is a small part of a program which performs the
returns a value to the main program. specific task and does not return any value.
It can be used by specific calls in the main program. It can be used by specific calls in the main program.
It is used for calculation and data manipulation. It is used for performing program validations,
printing error messages and other routine tasks.
It is defined by using FUNCTION ……. END It is defined by using SUB ……. END SUB statement.
FUNCTION The SUB statement is used to define a sub
Syntax: FUNCTION NAME <PARAMETERLIST> procedure.
< STATEMENTS > Syntax: SUB NAME <PARAMETERLIST>
NAME = <EXPRESION> STATEMENT BLOCK
END FUNCTION END SUB
[Parameter list is the list of variables that accepts the
values, passes to it when the function is called.
Expression is the value that has to be returned to the
calling module.]
Syntax: Syntax :
DECLARE {FUNCTION/SUB} NAME (PARAMETER CALL NAME <PARAMETERLIST>
LIST) [Transfers control to a SUB procedure.]
[Declares a FUNCTION or SUB procedure and invokes
argument, data type checking.]
Example: Program to find the product of two values Example: Program to find the product of two values
using FUNCTION …… END FUNCTION using
Program: SUB …… END SUB
DECLARE FUNCTION PRODUCT ( A, B ) Program:
CLS REM PARAMETER PASSING BY REFERENCE
INPUT “ ENTER THE FIRST VALUE”; A DECLARE SUB PRODUCT (A,B)
INPUT “ENTER THE SECOND VALUE”;B CLS
PRINT” THE PRODUCT OF TWO VALUES”; INPUT “ENTER FIRST VALUE”;A
PRODUCT (A,B) INPUT”ENTER SECOND VALUE”;B
END CALL PRODUCT (A,B)
END
FUNCTION PRODUCT (A,B)
PRODUCT= A*B SUB PRODUCT (A,B)
END FUNCTION PRODUCTT=A*B
PRINT “THE PRODUCT OF TWO
VALUES”;PRODUCTT
END SUB

QBASIC supports two types of functions

BUILD-IN OR LIBRARY FUNCTIONS USER DEFINED FUNCTIONS


These are predefined functions which are written at the These functions are created by the programmers to
time of development of QBASIC by the developer. perform a specific task when it cannot be performed
Programmers can use these functions according to the by any library function.
requirement. FUNCTION……………END FUNCTION statement
can be used to define the function.

Types of library functions


STRING FUNCTIONS / MANIPULATORS MATHEMATICAL / NUMERICAL FUNCTIONS
These are alphanumeric characters, enclosed within These are numeric characters. Some of them are:
quotation marks. Some of them are:
ASC ABS
DATE$ ASC
INKEY$ CDBL
INPUT$ CHR$
INSRT CINT
LCASE$ CSNG
LEFT$ EXP
LEN FIX
LTRIM$ HEX$
MID$ INT
RIGHT$ LOG
RTRIM$ MOD
SPACE$ OCT$
STRING$ SGN
STR$ SPC
TIME$ SQR
UCASE$ TAB
VAL RND
Trigonometric functions: SIN, COS, TAN, ATN
Passing parameters/arguments to a function or procedure:
There are two ways to pass parameters to a function or procedure. They are:

Passing by value Passing by reference


arguments are passed by value The address of the variables are passed to the procedure
It does not make any effect to the value of theIf t If the value of variable is changed in the procedure then it
variables which are passed to a procedure even if they changes the value of the variable in the main module.
are changed in the procedure.
Concept of local and global variable:
Local variable Global variable
Declared within a subroutine or program. Declared at the start of a program.
Can only be used within the subroutine or program Can be used in any procedure or subroutine in the program.
block where it is declared. We use SHARED statement to declare a global
variable inside a module (sub-procedure or function).
We use DIM SHARED statement to declare global
variables in the main module.
Example:
COMMON SHARED <VARIABLE LIST>
DIM SHARED <VARIABLE LIST>

Questions for practice:

A. Fill in the blanks.

1. A __________________or __________________ is a separate block of code which performs specific tasks.

2. ___________________ is a built-in formula to perform a specific task and returns a value to the main program.

3. ___________________ is a small part of a program which performs the specific task and does not return any
value.
4. ____________________ variables Declared at the start of a program.

5. _______________________ variables can only be used within the subroutine or program block where it is
declared.

B. Answer the following questions.


1. What are the two types of procedure in QBASIC?
2. What are the two types of functions?
3. What are the two types of library function?
4. What are the two ways to pass parameters to a function or procedure? Explain them.
5. What are the differences between local and global variables?

C. Complete it:

Procedure

Function _____________________

__________________________________ User defined function

String function / Manipulator ____________________________

_____________________________________________________________________________________________________________________________

You might also like