Functions in SP
Functions are defined with the fn keyword, parameter types, and an explicit return type arrow -> Type.
functions.sp
1import std.io;23// Function with explicit parameters and return type4fn multiply(int a, int b) -> int {5 return a * b;6}78// Function with default parameter value9fn greet(string name, string greeting = "Welcome") -> void {10 println("${greeting}, ${name}!");11}1213// Function overloading supported by argument types14fn display(int x) -> void {15 println("Int: ${x}");16}1718fn display(string s) -> void {19 println("String: ${s}");20}2122fn 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.