Bindings and arithmetic
ASPL 0.7.0 · Current release
Initialize named values, multiply 3 by 7, print 21, and correct immutable assignment.
On this page
What you will learn
You will give values names, calculate with small integers, print a numeric result, and decide when a name may be assigned a new value.
Before you begin
Read first output for the reserved entry, printing, explicit newlines and status 0. This lesson supplies a complete program of its own. Opening it creates a new draft and preserves earlier edits. Run remains your choice.
Name values and calculate
A binding associates a name with an initialized value. let / دع introduces an immutable binding: you cannot later assign that name a new value. var / متغير introduces a mutable binding, which permits later assignment. Both require a starting value. In a declaration, = supplies that initial value. The values 3 and 7 are small integers; ASPL infers their type as i64 / ص64, so you need not write a type here.
+ adds two numbers. * multiplies them. An expression such as left + right computes a value using the two named values. The names are user identifiers, not keywords. The supplied Arabic source uses أيسر, أيمن and إجمالي; changing documentation language does not rename your identifiers.
print takes text, so first use format_i64 / نسق_ص64 to turn an i64 number into its decimal text. format_i64(total) reads total and returns that text. The outer print writes it. A second print writes "\n" to end the line. Formatting and printing are separate operations; neither changes total.
Execution: Browser and native · ASPL 0.7.0
- Read the example explanation in its lesson before running.
Input: No input lines are required.
fn start_main() -> i64 {
let left = 3;
let right = 7;
let total = left + right;
print(format_i64(total));
print("\n");
return 0;
}
Expected outcomes
{
"browser": {
"check": "success",
"class": "success",
"output": "10\n",
"processStatus": 0
},
"native": {
"check": 0,
"status": 0,
"output": "10\n"
}
}The entry first initializes left to 3 and right to 7. It then initializes total to their sum, 10, and prints 10\n. Finally, return 0 ends normally. The calculated value is output, not the process status.
Predict before running
What value does total hold, and what exact text is printed? If you replace + with *, predict the new result before Run. Addition gives 10; multiplication gives 21. Each is printed with one newline, while the status stays 0.
Multiply instead of adding
Change only the operator in the declaration of total to calculate 3 * 7. Expected output is exactly 21\n and the normal status is 0.
Make the program multiply 3 by 7 and print 21 followed by one newline.
Hint: Change left + right to left * right. Keep numeric formatting and the separate newline print.
Show worked answer
The new expression multiplies the two initialized values, so total is 21. format_i64 returns the text 21, and the following print adds the newline. All three bindings can remain immutable because none needs reassignment.
Execution: Browser and native · ASPL 0.7.0
- Read the example explanation in its lesson before running.
Input: No input lines are required.
fn start_main() -> i64 {
let left = 3;
let right = 7;
let total = left * right;
print(format_i64(total));
print("\n");
return 0;
}
Expected outcomes
{
"browser": {
"check": "success",
"class": "success",
"output": "21\n",
"processStatus": 0
},
"native": {
"check": 0,
"status": 0,
"output": "21\n"
}
}Understand an immutable-assignment error
This intentionally failing variant calculates 21, then tries to assign total + 1 back to the immutable name total.
Execution: Browser and native · ASPL 0.7.0
- Read the example explanation in its lesson before running.
Input: No input lines are required.
fn start_main() -> i64 {
let left = 3;
let right = 7;
let total = left * right;
total = total + 1;
print(format_i64(total));
print("\n");
return 0;
}
Expected outcomes
{
"browser": {
"check": "source-error",
"class": "source-error",
"output": "",
"reason": "immutable-assignment"
},
"native": {
"check": 1,
"status": 1,
"output": "",
"reason": "immutable-assignment"
}
}Checking reports immutable-assignment at total on line 5, column 5. Select that diagnostic to find the name being assigned. No print runs, so the failure produces no program output. The problem is permission to reassign the binding, not the arithmetic or formatting.
For intended mutation, change the declaration of total from let to var, or دع to متغير in the supplied Arabic source. Leave the other bindings immutable. The later = is now assignment: it replaces the stored value with 22.
Execution: Browser and native · ASPL 0.7.0
- Read the example explanation in its lesson before running.
Input: No input lines are required.
fn start_main() -> i64 {
let left = 3;
let right = 7;
var total = left * right;
total = total + 1;
print(format_i64(total));
print("\n");
return 0;
}
Expected outcomes
{
"browser": {
"check": "success",
"class": "success",
"output": "22\n",
"processStatus": 0
},
"native": {
"check": 0,
"status": 0,
"output": "22\n"
}
}The correction checks and prints 22\n with normal status 0. A binding declared with var still needs its initial value. You do not need mutation just to calculate a new result once. See the binding and control-flow reference for fuller rules.
Check your understanding
Explain why both declaration forms need initial values, why the multiplication prints 21, and why only the mutation example needs var. Repair the failing assignment and compare 22\n with your prediction. No grade or progress is stored; navigation stays open. Continue to the conditions whenever you choose.