Variables And Functions
Now that we know that every Scheme statement is enclosed in parentheses,
and that the function name/operator is listed first, we need to know how
to create and use variables, and how to create and use functions. We'll
start with the variables.
Although there are a couple of different methods for declaring
variables, the preferred method is to use the let* construct. If
you're familiar with other programming languages, this construct is
equivalent to defining a list of local variables and a scope in which
they're active. As an example, to declare two variables, a and b,
initialized to 1 and 2, respectively, you'd write:
(+ a b)
or, as one line:
(let* ( (a 1) (b 2) ) (+ a b) )
You'll have to put all of this on one line if you're using the
console window. In general, however, you'll want to adopt a similar
practice of indentation to help make your scripts more readable.
We'll talk a bit more about this in the section on White Space.
This declares two local variables, a and b, initializes them, then
prints the sum of the two variables.
What Is A Local Variable?
You'll notice that we wrote the summation
(+ a b) within
the parens of the
let* expression, not after it.
This is because the
statement defines an area in your script in which the declared
variables are usable; if you type the (+ a b) statement after the
(let* ...) statement, you'll get an error, because the declared
variables are only valid within the context of the
statement; they are what programmers call local variables.
The General Syntax Of
The general form of a
let* statement is:
where variables are declared within parens, e.g., (a 2), and
expressions are any valid Scheme expressions. Remember that the
variables declared here are only valid within the
let* statement -- they're local variables.
Previously, we mentioned the fact that you'll probably want to use
indentation to help clarify and organize your scripts. This is a good
policy to adopt, and is not a problem in Scheme -- white space is
ignored by the Scheme interpreter, and can thus be liberally applied
to help clarify and organize the code within a script. However, if
you're working in Script-Fu's Console window, you'll have to enter an
entire expression on one line; that is, everything between the opening
and closing parens of an expression must come on one line in the
Script-Fu Console window.
Assigning A New Value To A Variable
Once you've initialized a variable, you might need to change its value
later on in the script. Use the set! statement to change the
(let* ( (theNum 10) ) (set! theNum (+ theNum theNum)) )
Try to guess what the above statement will do, then go ahead and enter
it in the Script-Fu Console window.
The “\” indicates that there is no line break. Ignore it (don't type
it in your Script-Fu console and don't hit Enter), just continue
with the next line.
Now that you've got the hang of variables, let's get to work with some
functions. You declare a function with the following syntax:
name is the name assigned to this
param-list is a space-delimited
list of parameter names, and
is a series of expressions that the function executes when it's
called. For example:
(define (AddXY inX inY) (+ inX inY) )
AddXY is the function's name and
are the variables. This function takes its two parameters and adds
If you've programmed in other imperative languages (like C/C++, Java,
Pascal, etc.), you might notice that a couple of things are absent in
this function definition when compared to other programming languages.
First, notice that the parameters don't have any "types" (that is,
we didn't declare them as strings, or integers, etc.). Scheme is a
type-less language. This is handy and allows for quicker script
Second, notice that we don't need to worry about how to "return"
the result of our function -- the last statement is the value
"returned" when calling this function. Type the function into the
console, then try something like:
(AddXY (AddXY 5 6) 4)