|
Variables, Data Types, Declaring Variables, Comments, and Database Access |
Welcome to the fundamentals of Sheerpower! In this tutorial, you'll learn how to store information in variables, understand the different types of data you can use, and how to document your code with comments. Along the way, you'll also see how Sheerpower's naming conventions and type system keep programs clear and easy to read.
Variables are containers for storing data. In Sheerpower, a
variable name must begin with a letter and can contain letters,
numbers, and underscores. Variable names are case-insensitive and
typically use underscores to improve readability, like
tax_rate or full_name$.
Sheerpower also reserves two special throw-away identifiers:
_ for numbers and _$ for strings.
They are write-only targets—assign to them when you want an
expression to run but intentionally ignore its return value.
This makes your intent clear at a glance.
For example, the join() function quickly joins
strings together and returns the final length. If we don't need
the length, we can do this:
Every variable holds a specific type of data. Sheerpower's core data types are:
For text.
"Hello!"
For numbers.
123.45
For true/false.
true
Rarely used. Use REAL instead. Sheerpower's REAL type handles whole numbers perfectly, making a separate INTEGER type unnecessary for most arithmetic.
42
Inherits the data type of the last value assigned.
dynamic data types?
Variables can be created either implicitly (by using type suffixes)
or explicitly (with the declare statement).
Sheerpower supports both styles, so you can choose the one that
fits your code and team conventions.
$ for a STRING (e.g., name$)? for a BOOLEAN (e.g., found?) — read it as a question% for an INTEGER (e.g., count%)declare): Can improve clarity,
organizes declarations, and reduces errors. This is especially helpful
in larger programs or shared codebases.
OPTION REQUIRE DECLARE to enforce explicit
declarations. The compiler will raise an error if a variable
is used without being declared first.
declare
if you like the clarity they provide (for example, declare boolean found?).
Sheerpower’s REAL type is designed for exact decimal
arithmetic. Unlike IEEE floating-point types used in most languages,
REAL values deliver 16 digits of true decimal precision,
avoiding the rounding errors that plague floating-point math.
This makes REAL ideal for financial, accounting,
and other applications where accuracy is critical.
REAL is stored as two 64-bit integers:
one for the integer part (IP) and one for the fractional
part (FP), scaled by 1016.
For example, 13.599 is internally represented as:
This design eliminates floating-point approximation and enables
efficient comparisons and arithmetic.
Expressions like (0.1 + 0.2) - 0.3 evaluate to
0 in Sheerpower.
By contrast, the same expression in IEEE math yields:
0.00000000000000005551115123125783
(0.1 + 0.2) - 0.3
Sheerpower is strongly typed. A STRING value—even one
like "123.45"—cannot be assigned directly to a REAL
variable. This results in a compile-time error unless you explicitly
convert it.
Sheerpower lets you define your own constants and custom data types. These features make code more readable, expressive, and secure.
This makes code more self-explanatory—using money
signals financial data, not just a number.
If the program crashes, password appears as
<nodump> in debug output—protecting secrets
from accidental exposure.
nodump modifier ensures those
values are hidden, showing <nodump> instead of
the actual contents.nodump for secrets or private
data to keep applications safe without extra coding effort.
$ – Literal dollar sign%.2m – Two decimal places in monetary format
Sheerpower provides a secure way to access table fields using
table_name(field_name). This separates code from data
and protects against SQL injection.
Database access is an advanced feature. To learn more, see the Integrated Database Access tutorial.
Comments are ignored by the compiler and help others understand your code.
Use // (preferred) or ! to begin a comment.
Sheerpower enforces variable types. You cannot assign a STRING directly
to a REAL or vice versa — use conversion functions.
For a complete walkthrough, see the val() function tutorial .
Conversions like val() and str$() are
commonly used when reading user input, importing raw data, or
constructing strings for display.
OPTION REQUIRE DECLAREnodump)declare format! and //)val() and str$()$, ?, %) or be created with declare.REAL is the default numeric type with exact 16-digit precision.OPTION REQUIRE DECLARE) enforces explicit declarations.val() to convert strings to numeric REAL values.str$() to convert numbers/booleans to strings.const) cannot be changed after declaration.money or nodump).declare format sets the default printed output style for a variable.table(field) syntax._ and _$ as write-only variables to discard results intentionally.! or // for meaningful comments that explain why, not just what.|
Hide Description
|
|
|
Enter or modify the code below, and then click on RUN |
|
Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use. |