Q(uick)BASIC Function: SQR
Quick View
SQR
A math function that returns the square root of numeric expression
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- SQR(numeric-expression)
Description/Parameter(s)
numeric-expression | A value greater than or equal to zero. |
Example
PRINT SQR(25), SQR(2) 'Output is: 5 1.414214
Syntax
- SQR(numeric-expression)
Description/Parameter(s)
- The argument n must be >= 0.
Example
The following program plots the graphs ofy = √-x for -9 <= x < 0, and
y = √x for 0 <= x <= 9:
'*********************************************************************
' Example program for SQR function.
'*********************************************************************
SCREEN 1 : COLOR 1 'Low-resolution color graphics mode.
WINDOW (-9,-.25)-(9,3.25) 'Convert screen to Cartesian coordinates.
LINE (-9,0)-(9,0) 'Draw X-axis.
LINE (0,-.25)-(0,3.25) 'Draw Y-axis.
FOR X = -9 TO 9
LINE(X,.04)-(X,-.04) 'Put tick marks on X-axis.
NEXT
FOR Y = .25 TO 3.25 STEP .25
LINE (-.08,Y)-(.12,Y) 'Put tick marks on Y-axis.
NEXT
PSET (-9,3) 'Plot the first point of function.
FOR X = -9 TO 9 STEP .25
Y = SQR(ABS(X)) 'SQR argument cannot be negative.
LINE -(X,Y),2 'Draw a line to the next point.
NEXT
Syntax
- SQR(numeric-expression)
Description/Parameter(s)
numeric-expression | A value greater than or equal to zero. |
Example
The following program uses the SQR function to plot the graph of
y = sqr(abs(x)) for -9 <= x <= 9.
SCREEN 1: COLOR 1 'Low-resolution color graphics mode.
WINDOW (-9, -.25)-(9, 3.25) 'Convert screen to Cartesian coordinates.
LINE (-9, 0)-(9, 0) 'Draw X-axis.
LINE (0, -.25)-(0, 3.25) 'Draw Y-axis.
FOR X = -9 TO 9
LINE (X, .04)-(X, -.04) 'Put tick marks on X-axis.
NEXT X
FOR Y = .25 TO 3.25 STEP .25
LINE (-.08, Y)-(.12, Y) 'Put tick marks on Y-axis.
NEXT Y
PSET (-9, 3) 'Plot the first point of function.
FOR X = -9 TO 9 STEP .25
Y = SQR(ABS(X)) 'SQR argument cannot be negative.
LINE -(X, Y), 2 'Draw a line to the next point.
NEXT X