function
Functions in Soul are declared with thesoul
keyword and are first-class citizens, meaning they can be assigned to variables, passed as arguments, and returned from other functions.
Basic Function Declaration
Functions are declared using thesoul
keyword followed by the function name and parameters:
Function Parameters
Functions can have zero or more parameters:Default Parameters
Functions can have default parameter values:Function Return Values
Functions can return values using thereturn
statement:
First-Class Functions
Functions can be assigned to variables and passed around:Anonymous Functions
Functions can be created without names:Higher-Order Functions
Functions that take other functions as parameters:Closures
Functions can capture variables from their enclosing scope:Function Scope
Functions create their own scope for variables:Recursive Functions
Functions can call themselves:Static Functions
Functions can be marked as static, typically used within classes:Async Functions
Functions can be declared as async for asynchronous operations:Function Expressions
Functions can be used as expressions:Function with Variable Arguments
While not directly supported, you can handle variable arguments:Function Composition
Combining functions to create new functionality:Function Validation
Functions with input validation:Best Practices
- Use descriptive names:
calculateTotalPrice
instead ofcalc
- Keep functions focused: Each function should do one thing well
- Use parameters instead of global variables: Makes functions more reusable
- Return meaningful values: Avoid functions that only have side effects
- Handle edge cases: Validate inputs and handle errors gracefully