LESSON 21 OF 52 · CHAPTER 03

Typed Functions

Function calls, typed arguments/returns, forward calls, unit returns, pure functions

ORIGINAL SOURCE

01-typed-functions.srp

// Typed calls, forward declarations, all primitive parameter types, and unit.
print(add(10, square(5)));

funcra add(a: integer, b: integer) -> integer {
    return a + b;
}

funcra square(value: integer) -> integer {
    return value * value;
}

funcra identity(text: string) -> string {
    return text;
}

funcra greet(name: string) -> unit {
    print("Hello", name);
    return;
}

pure funcra choose(value: integer, enabled: boolean) -> integer {
    if enabled { return value * 2; }
    return value;
}

greet(identity("Scarp"));
print(choose(3, true));
EXPECTED OUTPUT

stdout

Verified
35
Hello Scarp
6

UNDERSTAND THE CODE

Explanation & practice

What this example teaches

Function calls, typed arguments/returns, forward calls, unit returns, pure functions

Expected behavior

35, Hello Scarp, 6 on separate lines

Practice: Read the source before looking at the output. Predict the values and their order, then use the pinned compiler to execute this lesson and compare the exact fixture. Identify which operation produces the result.

टाइप वाले फ़ंक्शन

add और square के पैरामीटर और रिटर्न प्रकार integer हैं। पहले फ़ंक्शन कॉल और नीचे फ़ंक्शन की घोषणा, फ़ॉरवर्ड कॉल दिखाती है। greet का रिटर्न प्रकार unit है और वह प्रिंट करता है, जबकि choose एक pure funcra है।

आउटपुट: पहली पंक्ति 35 (10 + 5 * 5) है, फिर Hello Scarp, फिर 6।

अभ्यास: greet और choose में रिटर्न टाइप और प्रभाव का अंतर समझाएँ।

Explanations are sourced from the project's English chapter guide and हिन्दी अध्याय. The Scarp program and output are unchanged.

SOURCE-BACKED SELF-CHECK · स्रोत-आधारित अभ्यास

Quick checkpoint / त्वरित अभ्यास

What is the first number printed by add(10, square(5))?add(10, square(5)) से प्रिंट होने वाली पहली संख्या क्या है?

Reveal the source-backed answer / सही उत्तर देखें

35 — square(5) returns 25; add(10, 25) returns 35. The source calls these functions before their declarations.35 — square(5) का परिणाम 25 है। फिर add(10, 25) का परिणाम 35 होता है।

This question checks understanding of a recorded compiler output. It does not run Scarp in the browser. यह प्रश्न सत्यापित आउटपुट पर आधारित है; ब्राउज़र में Scarp नहीं चलता।

TRY IT LOCALLY

Verify, then execute

Use the pinned compiler from the course root. Keep any imported _support files next to the source.

scarp verify examples/03-functions/01-typed-functions.srp scarp exec examples/03-functions/01-typed-functions.srp

The website does not run code. Read the CLI guide ↗

Pinned compiler: Scarp v0.0.6-alpha.3 · ceff07d9014f. No simulated output.