Skip to main content

function

Functions in Soul are declared with the soul 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 the soul 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 the return 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

  1. Use descriptive names: calculateTotalPrice instead of calc
  2. Keep functions focused: Each function should do one thing well
  3. Use parameters instead of global variables: Makes functions more reusable
  4. Return meaningful values: Avoid functions that only have side effects
  5. Handle edge cases: Validate inputs and handle errors gracefully
Functions are the building blocks of Soul applications, providing reusable, modular code that can be combined to create complex functionality.