Functions in SP

Functions are defined with the fn keyword, parameter types, and an explicit return type arrow -> Type.

functions.sp
1import std.io;
2
3// Function with explicit parameters and return type
4fn multiply(int a, int b) -> int {
5 return a * b;
6}
7
8// Function with default parameter value
9fn greet(string name, string greeting = "Welcome") -> void {
10 println("${greeting}, ${name}!");
11}
12
13// Function overloading supported by argument types
14fn display(int x) -> void {
15 println("Int: ${x}");
16}
17
18fn display(string s) -> void {
19 println("String: ${s}");
20}
21
22fn main() -> void {
23 let result = multiply(6, 7);
24 println("6 * 7 = ${result}");
25 greet("Alice");
26 display(100);
27 display("SP Language");
28}

Key Function Features

  • Static Return Path Analysis: All non-void functions are statically verified to return a value along every code execution branch.
  • Function Overloading: Multiple functions may share the same name provided their argument type signatures differ.
  • Default Arguments: Trailing parameters may specify default values.