Sheerpower Logo

Routines and Variable Scoping


Sheerpower: Routines and Variable Scoping

As projects grow, it becomes essential to break code into smaller, reusable building blocks. In Sheerpower, these are called routines. They not only organize your code, but also control how variables are shared, isolated, or reset between calls.

Routine names: All routine names must begin with a letter and include at least one underscore (_). For example, do_taxes is valid, but taxes is not. Routine names are case-insensitive. By design, Sheerpower never uses underscores in its own keywords, so your routine names will never collide with language features.

Types of Routines in Sheerpower

Global Routines: The default type. All global variables are accessible inside them.

Private Routines: Use their own variable namespace. By default, variables declared inside are local to that routine. If you need to access a global variable, prefix it with main$.

Note: The main$ prefix allows private, scoped, or local routines to access global variables directly.

Scoped Routines: Similar to private routines, but all non-static variables are cleared on entry and exit. Prefix any variable with static to preserve it across calls.

Local Routines: Defined inside another routine. They share the parent's scope and can only be called from that parent. This makes them ideal for breaking down complex logic into smaller steps without parameter-passing overhead.

Note: The compiler ensures local routines cannot be called from outside their parent, preventing accidental coupling or misuse.

Invoking Routines with Named Parameters

Routines always use named parameters. Do not prefix the call with call. Instead, write the routine name directly, followed by with (input) and returning (output) parameter assignments.

Input parameters in the with clause are read-only - the compiler prevents any modifications. They are passed by reference for efficiency, but cannot be changed within the routine.

routine calculate_area with length, width, returning area area = length * width end routine calculate_area with length = 10, width = 5, returning area room_area print "The area is: "; room_area

Output:

The area is: 50

Returning Values from Routines

A routine can return up to 16 values using the returning clause. Each return variable must be explicitly named in the call.

Basic Return Example

routine calculate_area with length, width, returning area area = length * width end routine calculate_area with length = 10, width = 5, returning area room_area print "The area is: "; room_area

Returning Multiple Values

routine split_name with full_name$, returning first$, last$ first$ = element$(full_name$, 1, " ") last$ = element$(full_name$, 2, " ") end routine split_name with full_name$ = "Anna Rivera", returning first$ fname$, last$ lname$ print "First: "; fname$ print "Last: "; lname$

Overall Benefits

Sheerpower routines combine clarity, safety, and flexibility. Strong scoping rules reduce accidental variable misuse, while named parameters make code self-documenting and maintainable.

The next tutorial covers parameter passing in much more detail.
(Show/Hide Routine Syntax & Scoping 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.