3. Answer the following questions:
(a) What is function procedure? Write its importance in programming.
Ans:
Function procedure is a module that processes data and returns a value.
Advantages of function
-Function increase code reusability.
-The length of the source program can be reduced by using functions at appropriate places.
-The program development will be faster.
-The program debugging will be easier.
(b) Write some important features of user-defined functions of QBASIC.
Ans: Features of Function procedure :
- Arguments can be passed to the function by 'reference' or by 'value'.
- Variables used inside a function are local by default.
(c) What are library functions? Give two examples.
Ans: Ready-made functions provided by the QBASIC are library function. Example : LEN, UCASE$, LCASE$, MID$, INT, SIN,COS, TAN etc
(d) What is user-defined function? Why is it necessary to create?
Ans: Function created by users is called user-defined function. It is necessary to create to solve the specific task of the user.
(e) Write any two differences between function procedure and sub procedure.
Ans:
User Defined Function | SUB Procedure |
1. Function must return a value. | 1. Sub procedure does not return a value. |
2. A function is called by statement and expression method. | 2. Sub procedure is called by statement method i.e. CALL statement only. |
(f) Write any two differences between function 'call by reference' and 'call by value'.
Ans:
Call by reference | Call by value |
1. Function call by reference serve as a medium for sending information back to the calling procedure. | 1. Function call by value are local to the function. Changes in the variable are not affected in the calling function. |
2. To use function 'call by reference' the argument variable is not enclosed in parenthesis. | 2.To use function 'call by value' the argument is enclosed in parenthesis. |
6. Write down the programs for the following:
a. Write a program to input two different numbers and then find the sum of the square of numbers using the function procedure.
Ans:
DECLARE FUNCTION SUM_SQUARE(A,B)
INPUT "Enter two different numbers : ";A,B
PRINT "Sum of square of two numbers is ";SUM_SQUARE(A,B)
END
FUNCTION SUM_SQUARE(A,B)
SUM_SQUARE = A ^ 2 + B ^ 2
END FUNCTION
b. Write QBASIC program to input three different numbers in the main module and then find the smallest number using the function procedure.
DECLARE FUNCTION CHECK(A,B,C)
INPUT "Enter first number ";A
INPUT "Enter second number ";B
INPUT "Enter third number ";C
PRINT "Smallest number is ";CHECK(A,B,C)
END
FUNCTION CHECK(A,B,C)
IF A<B AND A<C THEN
S = A
ELSEIF B<A AND B<C THEN
S = B
ELSE
S = C
END IF
CHECK = S
END FUNCTION
c. Write QBASIC program to input three different numbers and then find the average of the numbers using the function procedure.
DECLARE FUNCTION AVERAGE(A,B,C)
INPUT "Enter first number ";A
INPUT "Enter second number ";B
INPUT "Enter third number ";C
PRINT "Average of three numbers is ";AVERAGE(A,B,C)
END
FUNCTION AVERAGE(A,B,C)
S = A + B + C
AVG = S / 3
AVERAGE = AVG
END FUNCTION
d. Write QBASIC program to input a number and check whether the number is fully divisible by 3 or not, using the function procedure.
e. Write QBASIC to input a string in main module and then print it in the reverse form using the function procedure.
Ans:
DECLARE FUNCTION REV$(W$)
INPUT "Enter a string :";W$
PRINT "Reverse of given string is ";REV$(W$)
END
FUNCTION REV$(W$)
FOR I = LEN(W$) TO 1 STEP-1
C$ = C$ + MID$(W$, I, 1)
NEXT I
REV$ = C$
END FUNCTION
f. Write QBASIC program to input a string and count the total number of vowel characters in the string using the function procedure.
Ans:
DECLARE FUNCTION VOWEL(W$)
INPUT "Enter a string :";W$
PRINT " Number of vowels is ";VOWEL(W$)
END
FUNCTION VOWEL(W$)
FOR I = 1 TO LEN(W$)
C$ = UCASE$(MID$(W$, I, 1))a
SELECT CASE C$
CASE "A","E","I","O","U"
X = X + 1
END SELECT
NEXT I
VOWEL = X
END FUNCTION
g. Write QBASIC program to input a string and then print it in alternate capital letters using the function procedure.
h. Write a user-defined function that checks whether the input number is prime or composite.
DECLARE FUNCTION CHECK(N)
INPUT "Enter a number"; N
IF CHECK(N)=2 THEN
PRINT "PRIME"
ELSE
PRINT "COMPOSITE"
END IF
END
FUNCTION CHECK(N)
FOR I = 1 TO N
R = N MOD I
IF R=0 THEN
C = C + 1
END IF
NEXT I
CHECK = C
END FUNCTION
i. Write QBASIC program to calculate factorial of a supplied number using the user defined function named fact().
j. Write QBASIC program with function procedure to calculate the sum of all the digits of a supplied number. The number must be input in the main module.
k.
l.
m.
n.