FUNCTION Procedure in QBASIC for Class 10 and SEE
Function procedure is like a sub procedure but it returns a value.
The functions are call from the main program or main module. The return value
of the function is either string or numeric. Function procedures are called by
using PRINT statement or the return value can be assigned to a variable and
later can be used for other purpose. The function procedure are created using
FUNCTION………..END FUNCTION statement.
Features of
Function Procedure:
i) Function procedure returns a value
ii) Function procedure name has type declaration characters
iii) A function name can be used in an expression
iv) Function procedure can be recursive
a) Declaring a
Function Procedure:
Declaration of function procedure specifies the function procedure’s
name, the number and type of arguments used to call the procedure. This is done
with DECLARE FUNCTION statement.
Syntax:
DECLARE
FUNCTION function_name[(parameter_lists)]
Where,
Function_name is
the name of the user defines function. The function name assigned the return
value of the function. Hence the function name should be of the same data type
similar to the return value. For example, function name that ends with “$”
returns a string value, and one that ends without “$” sign or ends with “%”
signs returns integer value.
Parameter lists indicates the number and type of arguments
that will be used to call the procedure.
Example:
DECLARE
FUNCTION char$(a$)
Note: In above example function name is char
with parameters a$. The function name is written with a $ sign because the
return value of the function is string.
b) Defining a Function
After declaration of the function procedure in the main module,
the body of function procedure begins with a FUNCTION statement and ends with
END FUNCTION. Between these two keywords a number of statements are written.
Note: to return main module press F2 and
vice versa
Syntax:
FUNCTION function_name(parameter_lists)
[statement blocks]
Function_name=expression
END FUNCTION
Where,
function-name specifies the name of the function
procedure. The name of the function procedure is written with the type of
declaration character according to the type of the returned value. If the function
returns numeric value, the name must be followed by %, !, & or # and if the
function returns string value, the name must be followed by $(dollor) sign.
parameter_lists are the lists of variables that are passed
to it when function is called in the main program.
expression : Return value of the function is expression
c) Calling a Function
Once a function is declared and defined, it is invoked in the main
program which is also called ‘calling a function’. A function procedure is
similar to the library function. A function can be called as a statement or
expression method. It may appear anywhere in the main module or within other
functions.
Syntax:
Variable=function_name(argument_lists)
Where,
variable is the name of
the variable to which the return value of the function is to be assigned.
function_name is the name of the function to be called.
arguemt_lists are the lists of variables or constants on
which the function procedure operates on.
Example:
S=sum(a,b)
Example: 1 WAP to calculate the area of rectangle
Example: 2 (Function called by using “statement
method”)
WAP to input any string
and display it in reverse order.
DECLARE FUNCTION rev$
(w$)
CLS
INPUT "enter any
string"; w$
r$ = rev$(w$)
PRINT r$
END
FUNCTION rev$ (w$)
FOR x = LEN(w$) TO 1
STEP -1
m$ = m$ + MID$(w$, x, 1)
NEXT
rev$ = m$
END FUNCTION
Example: 3 (Function
called by using “Constant value”)
WAP to display the
average of two numbers.
DECLARE FUNCTION avg (a,
b)
CLS
s = avg(23, 43)
PRINT "average of
two numbers"; s
END
FUNCTION avg (a, b)
avg = (a + b) / 2
END FUNCTION
Example: 4 (Function input numeric value and
return string value)
WAP to input any number
and display whether input number is add or even
DECLARE FUNCTION oe$ (n)
CLS
INPUT "Enter number"; n
e$ = oe$(n)
PRINT "The number
is="; e$
END
FUNCTION oe$ (n)
r = n MOD 2
IF r = 0 THEN
oe$ = "even"
ELSE
oe$ = "odd"
END IF
END FUNCTION
Example: 5 (Function
input string and returns numeric value)
WAP to input any string
and count total vowels letters.
DECLARE FUNCTION vow
(w$)
CLS
INPUT "Enter string"; w$
PRINT "Total vowel
letters="; vow(w$)
END
FUNCTION vow (w$)
FOR x = 1 TO LEN(w$)
m$ = MID$(w$, x, 1)
SELECT CASE m$
CASE "a",
"e", "i", "o", "u"
v = v + 1
END SELECT
NEXT
vow = v
END FUNCTION
Example: 6 (Array can be
passed to the function as an argument)
WAP to input 5 different
numbers in any array variable and display the sum of given numbers.
DECLARE FUNCTION ar
(n())
CLS
DIM n(5)
PRINT "Enter
five number"
FOR x = 1 TO 5
INPUT n(x)
NEXT
s = ar(n())
PRINT "sum=";
s
END
FUNCTION ar (n())
FOR x = 1 TO 5
su = su + n(x)
NEXT
ar = su
END FUNCTION
No comments:
Post a Comment