Sheerpower Logo

Variables, Data Types, Declaring Variables, Comments, and Database Access


The Fundamentals of Sheerpower Programming

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. Let's get started!

1. Understanding Variables

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 reserves two throw-away identifiers: _ for numbers and _$ for strings. They are write-only targets—assign to them when you want an expression to run but 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:

a$ = '' _ = join(a$, 'Admin: ', name$) // string build, no need to keep the returned length

2. Sheerpower's Core Data Types

Every variable holds a specific type of data. Sheerpower's core data types are:

STRING

For text.

"Hello!"

REAL

For numbers.

123.45

BOOLEAN

For true/false.

true

INTEGER

Rarely used. Use REAL instead. Sheerpower's REAL type handles whole numbers perfectly, making a separate INTEGER type unnecessary for most arithmetic.

42

DYNAMIC

Inherits the data type of the last value assigned.

Design Rationale: Why use dynamic data types?
  • Adapts to mixed data: Perfect for code that handles user input, file data, or API responses that might change type from one call to the next.
  • Type stability after assignment: Once a value is assigned, the variable behaves as that type until it is assigned something else.
declare dynamic x x = "hello" // now x is a STRING x = 3.14159 // now x is a REAL x = 42% // now x is an INTEGER

3. Two Ways to Create Variables

Variables can be created either implicitly (by using type suffixes) or explicitly (with the declare statement).

  • Implicitly (with suffixes): Fast and convenient. A suffix indicates the variable's type:
    • $ for a STRING (e.g., name$)
    • ? for a BOOLEAN (e.g., is_done?) — makes code easier to scan
    • % for an INTEGER
    • No suffix for a REAL (the default for numeric values)
  • Explicitly (with declare): Improves clarity, organizes declarations, and reduces errors.
Strict Mode Option:
Add OPTION REQUIRE DECLARE to enforce explicit declarations. The compiler will raise an error if a variable is used without being declared first.

This ensures tighter control and catches typos or undeclared variables early.
// Implicitly created variables full_name$ = "Sally Sue" is_active? = true total_cost = 19.95 // Explicitly declared variables declare string shipping_address declare real weight declare boolean item_available shipping_address = "123 Main St" weight = 2.5 item_available = true

4. A Special Note on REAL and High-Precision

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 scientific applications where accuracy is critical.

Each 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 represented as:
IP = 13
FP = 5990000000000000

This design eliminates floating-point approximation and enables efficient hardware-level 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: 0
Other Languages: 0.00000000...555

Sheerpower is strongly typed. A STRING value—even one like "123.45"—cannot be assigned to a REAL variable. This results in a compile-time error.

5. Custom Data Types

Sheerpower lets you define your own constants and custom data types. These features make code more readable, expressive, and secure.

Constants

// A constant’s value cannot be changed after definition. const everything = 42

Custom Data Types

// Define a new type named 'money' based on REAL type real money // Declare a variable using the new type declare money monthly_salary monthly_salary = 5000.00

This makes code more self-explanatory—using money signals financial data, not just a number.

Secure Custom Types

// Create a secure string type for sensitive data type nodump string secret // Declare a variable using the secure type declare secret password password = "xxyyy"

If the program crashes, password appears as <nodump> in debug output—protecting secrets from accidental exposure.

Why It Matters:
Problem: Debug output and crash logs can accidentally expose sensitive data like passwords or tokens.

Solution: The nodump modifier ensures those values are hidden, showing <nodump> instead of the actual contents.

Efficiency: This adds no runtime overhead—security is enforced automatically by the runtime.

Takeaway: Use nodump for secrets or private data to keep applications safe without extra coding effort.

6. Formatting Your Output

// This variable prints with a $ and 2 decimal places. declare format "$%.2m" price price = 123.456 print "Total price: "; price // Output: Total price: $123.46

Format Syntax

  • $ – Literal dollar sign
  • %.2m – Two decimal places in monetary format
declare format "%.1f%%" success_rate success_rate = 98.73 print "Success: "; success_rate // Output: Success: 98.7%

7. A Quick Look at Database Variables

Sheerpower provides a secure way to access table fields using table_name(field_name). This separates code from data and protects against SQL injection.

Payroll Table
id name salary
payroll(salary) = 55000.00

This is an advanced feature—beginners can skip ahead and revisit it later. To learn more, see the Integrated Database Access tutorial.

8. Documenting Your Code with Comments

Comments are ignored by the compiler and help others understand your code. Use // (preferred) or ! to begin a comment.

// This is a comment. It explains the next line. const tax_rate = 0.08 // 8% sales tax // Good comments explain the "why," not just the "what." // Calculate the final price including tax. final_price = subtotal * (1 + tax_rate)

9. Converting Between Strings and Numbers

Sheerpower enforces variable types. You cannot assign a STRING directly to a REAL or vice versa — use conversion functions.

Converting a String to a Number

declare real price declare string input$ input$ = "12.95" price = val(input$)

For a complete walkthrough, see the val() function tutorial .

Converting a Number to a String

declare string output$ declare real tax = 0.075 output$ = str$(tax)

Conversions like val() and str$() are commonly used when reading user input, importing raw data, or constructing strings for display.

10. Putting It All Together

const sales_tax_rate = 0.08 declare string item_name$ declare real item_price, quantity item_name$ = "Super Gadget" item_price = 29.95 quantity = 2 subtotal = item_price * quantity final_total = subtotal * (1 + sales_tax_rate) print sprintf("Item: %, Qty: %, Total: $%.2m", & item_name$, quantity, final_total) a$ = '' _ = join(a$, 'Admin: ', name$) // string build, no need to keep the returned length

What You've Learned

  • Variable naming rules and suffixes
  • Core types: STRING, REAL, BOOLEAN, optional INTEGER
  • Dynamic variables adapt type from assigned values
  • REAL type with exact 16-digit decimal precision
  • Explicit vs. implicit declarations
  • Strict mode with OPTION REQUIRE DECLARE
  • Constants and custom types (including nodump)
  • Formatting with declare format
  • Intro to database variables
  • Comments (! and //)
  • Conversions with val() and str$()
(Show/Hide Variable & Data Type Takeaways)

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.