Methods of Calling FUNCTION in QBASIC
By using “Expression method”
1. WAP to display the sum of input two numbers.
DECLARE FUNCTION sum (a, b)
CLS
INPUT "enter any two numbers"; a, b
s = sum(a, b)
PRINT "Sum of two numbers"; s
END
FUNCTION sum (a, b)
sum = a + b
END FUNCTION
Function called by using “statement
method”
2. WAP to display the string in reverse order.
DECLARE FUNCTION re$ (w$)
CLS
INPUT "enter any string"; w$
r$ = re$(w$)
PRINT r$
END
FUNCTION re$ (w$)
FOR x = LEN(w$) TO 1 STEP -1
m$ = m$ + MID$(w$, x, 1)
NEXT
re$ = m$
END FUNCTION
Function called by
using “Constant value”
3. WAP to display the average of two numbers.
DECLARE FUNCTION avg (a, b)
CLS
s = avg(13, 23)
PRINT "average of two numbers"; s
END
FUNCTION avg (a, b)
avg = (a + b) / 2
END FUNCTION
Function input numeric
value and return string value
4. WAP to input any number and display whether input number is add or even
DECLARE FUNCTION oes$ (n)
CLS
INPUT "Enter any number"; n
e$ = oes$(n)
PRINT "The number is="; e$
END
FUNCTION oes$ (n)
r = n MOD 2
IF r = 0 THEN
oes$ = "even"
ELSE
oes$ = "odd"
END IF
END FUNCTION
Function input string and
returns numeric value
5. WAP to input any string and count total vowels letters.
DECLARE FUNCTION vow (w$)
CLS
INPUT "Enter any 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
Array can be passed to the
function as an argument
6. WAP to input 5 different numbers in any array variable and display the sum of given numbers.
DECLARE FUNCTION arr (n())
CLS
DIM n(5)
PRINT "Enter five number"
FOR x = 1 TO 5
INPUT n(x)
NEXT
s = arr(n())
PRINT "sum="; s
END
FUNCTION arr (n())
FOR x = 1 TO 5
su = su + n(x)
NEXT
arr = su
END FUNCTION
1. WAP to display the sum of input two numbers.
DECLARE FUNCTION sum (a, b)
CLS
INPUT "enter any two numbers"; a, b
s = sum(a, b)
PRINT "Sum of two numbers"; s
END
FUNCTION sum (a, b)
sum = a + b
END FUNCTION
2. WAP to display the string in reverse order.
DECLARE FUNCTION re$ (w$)
CLS
INPUT "enter any string"; w$
r$ = re$(w$)
PRINT r$
END
FOR x = LEN(w$) TO 1 STEP -1
m$ = m$ + MID$(w$, x, 1)
NEXT
re$ = m$
END FUNCTION
3. WAP to display the average of two numbers.
DECLARE FUNCTION avg (a, b)
CLS
s = avg(13, 23)
PRINT "average of two numbers"; s
END
FUNCTION avg (a, b)
avg = (a + b) / 2
END FUNCTION
4. WAP to input any number and display whether input number is add or even
DECLARE FUNCTION oes$ (n)
CLS
INPUT "Enter any number"; n
e$ = oes$(n)
PRINT "The number is="; e$
END
r = n MOD 2
IF r = 0 THEN
oes$ = "even"
ELSE
oes$ = "odd"
END IF
END FUNCTION
5. WAP to input any string and count total vowels letters.
DECLARE FUNCTION vow (w$)
CLS
INPUT "Enter any string"; w$
PRINT "Total vowel letters="; vow(w$)
END
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
6. WAP to input 5 different numbers in any array variable and display the sum of given numbers.
DECLARE FUNCTION arr (n())
CLS
DIM n(5)
PRINT "Enter five number"
FOR x = 1 TO 5
INPUT n(x)
NEXT
s = arr(n())
PRINT "sum="; s
END
FOR x = 1 TO 5
su = su + n(x)
NEXT
arr = su
END FUNCTION
No comments:
Post a Comment