Functions
ASPL 0.7.0 · Current release
Call a typed doubling helper and distinguish its returned value from process status.
On this page
What you will learn
You will define a typed top-level helper, pass an argument, trace its returned value, and explain why that value differs from the program's process status.
Before you begin
Read bindings and arithmetic, conditions and loops. They supply named values, comparisons and ordered execution. This independent program includes its own entry. Open it as a new Home draft and choose Run explicitly; earlier work stays available.
Define and call a helper
fn / دالة declares a function. The helper double is top-level, outside the entry function and every other function body. Its parameter value: i64 / قيمة: ص64 names an incoming integer. The arrow and result type -> i64 / -> ص64 promise an integer result. Braces contain the helper's instructions, and return / أرجع sends a value back to the caller and ends that call.
double(6) is a call. The argument 6 supplies the parameter's value for this invocation. Arguments pass by value. Inside the helper, value * 2 computes 12; the return sends 12 back to the call site. The caller stores it in the immutable result binding. A call must supply the declared number and types of arguments.
Execution: Browser and native · ASPL 0.7.0
- Read the example explanation in its lesson before running.
Input: No input lines are required.
fn double(value: i64) -> i64 {
return value * 2;
}
fn start_main() -> i64 {
let result = double(6);
print(format_i64(result) + "\n");
return 0;
}
Expected outcomes
{
"browser": {
"check": "success",
"class": "success",
"output": "12\n",
"processStatus": 0
},
"native": {
"check": 0,
"status": 0,
"output": "12\n"
}
}Execution begins at the reserved start_main() -> i64 / ابدأ_الرئيسية() -> ص64 entry, not at the first helper declaration. Defining double alone does not run its body. The call temporarily enters it, then execution resumes in the entry. format_i64 / نسق_ص64 converts result to text, and print / اطبع writes it followed by the explicit \n newline.
The helper returns 12 to its caller. The entry prints exactly 12\n, then its own return 0 / أرجع 0 gives normal process status 0. The helper's return neither prints automatically nor sets process status. Returning double(6) directly from the entry would instead give status 12 and, without the print, no output.
Follow the returned value
Predict the helper result, visible output and process status separately.
| Call in the entry | Parameter value |
Helper result | Exact printed output | Entry status |
|---|---|---|---|---|
double(6) |
6 |
12 |
12\n |
0 |
double(9) |
9 |
18 |
18\n |
0 |
The new argument changes the computation and the printed data. It does not change the entry's final return.
Change the argument
Change only double(6) to double(9). Expected output is exactly 18\n and normal status 0.
Call double with 9. Explain the helper result, printed output and process status.
Hint: Change the argument at the call site. Keep the multiplication by 2 and the entry's final return 0.
Show worked answer
The parameter receives 9. The helper returns 9 * 2, which is 18, and the entry stores that value in result. Formatting and printing produce 18 followed by one newline. The entry still returns 0, so process status stays 0.
Execution: Browser and native · ASPL 0.7.0
- Read the example explanation in its lesson before running.
Input: No input lines are required.
fn double(value: i64) -> i64 {
return value * 2;
}
fn start_main() -> i64 {
let result = double(9);
print(format_i64(result) + "\n");
return 0;
}
Expected outcomes
{
"browser": {
"check": "success",
"class": "success",
"output": "18\n",
"processStatus": 0
},
"native": {
"check": 0,
"status": 0,
"output": "18\n"
}
}Return on every required path
A function with a non-unit result must return a value of that type on every required path. Our helper has one unconditional return. If you use if and else, every reachable alternative must return or lead to a later return. A closing brace supplies no implicit integer result.
This deliberately broken helper returns only for positive values. Even though the entry calls it with 6, its zero and negative paths can fall through. Checking reports missing-return before any program execution. Run reports the same source error, with no output or process status.
Execution: Browser and native · ASPL 0.7.0
- Read the example explanation in its lesson before running.
Input: No input lines are required.
fn double(value: i64) -> i64 {
if value > 0 {
return value * 2;
}
}
fn start_main() -> i64 {
let result = double(6);
print(format_i64(result) + "\n");
return 0;
}
Expected outcomes
{
"browser": {
"check": "source-error",
"class": "source-error",
"output": "",
"reason": "missing-return"
},
"native": {
"check": 1,
"status": 1,
"output": "",
"reason": "missing-return"
}
}Restore the unconditional return value * 2 shown in the starter to cover all inputs. The corrected complete starter prints 12\n and returns status 0. For the full rules, read the functions reference.
Optional recursion
A helper may call itself. The existing factorial example recursively computes factorial of 5 as 120, stopping at a value of 1 or below. Its entry returns that result directly. It prints nothing and ends with process status 120; that number is neither printed output nor a quiz score. You can leave recursion for later.
Check your understanding
Point to the declaration, parameter, argument, call and both returns. Trace where 12 goes after the helper returns and why the entry status remains 0. Compare the changed argument with your prediction after an explicit Run. The self-check stores no grade or progress and keeps every lesson open. Return to the Tutorial path.