Table of Contents
Overview
Below is a QBASIC program to find whether a number entered by the user is even or odd using SUB Procedure i.e., SUB…END SUB.
Using FUNCTION…END FUNCTION: Even or Odd using FUNCTION ProcedureWithout Parameters
REM Program to input a number and check if it is even or odd
DECLARE SUB EVENORODD
CLS
CALL EVENORODD
END
SUB EVENORODD
INPUT "Enter a number";a
IF a MOD 2 = 0 THEN
PRINT a;" is an even number"
ELSE
PRINT a;" is an odd number"
END IF
END SUB
With Parameters:
REM Program to input a number and check if it is even or odd
DECLARE SUB EVENORODD(a)
CLS
INPUT "Enter a number";a
CALL EVENORODD(a)
END
SUB EVENORODD(a)
IF a MOD 2 = 0 THEN
PRINT a;" is an even number"
ELSE
PRINT a;" is an odd number"
END IF
END SUB
Output – Even or Odd using SUB Procedure
Explanation
Notice there is no any dollar ($) sign at the end of the function name. There is no need of a dollar sign in SUB Procedure as the SUB procedure doesn’t return anything. Instead, we can do the processing and display the output in the SUB procedure. The main module will use CALL keyword to call the SUB Procedure into the main module.