Table of Contents
- Overview
- Check Even or Odd Without Parameters
- Check Even or Odd With Parameters
- Output – Even or Odd using Function Procedure
- Explanation
Overview
Below is a QBASIC program to find if a number entered by the user is even or odd using FUNCTION Procedure i.e., FUNCTION…END FUNCTION.
Using SUB…END SUB: QBASIC – Even or Odd Using SUB ProcedureCheck Even or Odd Without Parameters
REM Program to input a number and check if it is even or odd
DECLARE FUNCTION EVENORODD$
CLS
PRINT "The number is "; EVENORODD$
END
FUNCTION EVENORODD$
INPUT "Enter a number";a
IF a MOD 2 = 0 THEN
result$ = "even"
ELSE
result$ = "odd"
END IF
EVENORODD$ = result$
END FUNCTION
Check Even or Odd With Parameters
REM Program to input a number and check if it is even or odd
DECLARE FUNCTION EVENORODD$(a)
CLS
INPUT "Enter a number";a
PRINT "The number is "; EVENORODD$(a)
END
FUNCTION EVENORODD$(a)
IF a MOD 2 = 0 THEN
result$ = "even"
ELSE
result$ = "odd"
END IF
EVENORODD$ = result$
END FUNCTION
Output – Even or Odd using Function Procedure
Explanation
Notice the dollar ($) sign at the end of the function name. The dollar($) sign represent the string in QBASIC. Here, the dollar sign at the end of the function name is used to return any string value from the function procedure to any main module. As the program requires to display either even or odd from the submodule to the main module, we are using EVENORODD$ = result$ to return the value of result$ (even or odd) to the main module.