How functions are defined for modularity in different languages.
Functions
Go Language
Function Definition with Arguments and a Return Type
How to pass arguments to a function and express the return type.
Error Loading
Missing
Function Definition with Arguments and Return Type
Golang requires explicit type declarations for function parameters and return values.
func add(x int, y int) int { return x + y } func greet(name string) string { return fmt.Sprintf("Hello, %s!", name) }
- Parameters and return types are explicitly defined.
- Multiple parameters of the same type can be grouped:
func add(x, y int) int
. - Functions can return multiple values.
func divide(a, b int) (int, int) { return a / b, a % b }
Function Overloading
Provide multiple implementations with different sets of arguments.
Missing
No function overloading, but can emulate it with interfaces
import ( "fmt" ) func Add(x, y interface{}) interface{} { switch x.(type) { case int: return x.(int) + y.(int) case string: return x.(string) + y.(string) default: return nil } } func main() { fmt.Println(Add(5, 10)) // 15 fmt.Println(Add("Hello, ", "World!")) // "Hello, World!" }
Default or Optional Arguments
Provide defaults or express optionality in arguments.
Missing
Default Arguments
Not supported by Go, but you can use variadic parameters
func greet(name string, greetings ...string) { greeting := "Hello" if len(greetings) > 0 { greeting = greetings[0] } fmt.Printf("%s, %s!\n", greeting, name) } func main() { greet("Alice", "Hi") // Uses "Hi" greet("Bob") // Uses default "Hello" }
Or simulate it with a struct:
type Config struct { Greeting string Name string } func greet(cfg Config) { greeting := cfg.Greeting if greeting == "" { greeting = "Hello" } fmt.Printf("%s, %s!\n", greeting, cfg.Name) } func main() { greet(Config{Name: "Alice", Greeting: "Hi"}) greet(Config{Name: "Bob"}) // Greeting defaults to "Hello" }
Lambda Functions
Define a lambda function.
Missing
Lambda Function
In Go, lambda functions (also called anonymous functions) are defined inline without a name and can be immediately invoked or assigned to a variable.
Example:
func main() { // Immediately invoked function func() { fmt.Println("Hello from a lambda function!") }() // Assigning a lambda function to a variable add := func(a, b int) int { return a + b } fmt.Println("Sum:", add(3, 4)) // Output: Sum: 7 }
Lambda Function Capture
Use local in-scope variables inside the lambda function.
Missing
Lambda Functions Capturing Values
Lambda functions in Go capture variables from their surrounding scope by reference, meaning changes in the function affect the original variable.
Example:
func main() { x := 10 // Lambda function capturing 'x' from outer scope modifyX := func() { x += 5 // Modifies x from the outer scope } modifyX() // Call lambda function fmt.Println("Modified x:", x) // Output: Modified x: 15 }
However, since Go passes function parameters by value (unless using pointers), lambda functions do not automatically modify arguments unless explicitly passed by reference:
func main() { x := 10 // Lambda function that takes an argument (no capture) increment := func(n int) { n += 5 // This modifies only the local copy of 'n' } increment(x) fmt.Println("Unchanged x:", x) // Output: Unchanged x: 10 }
To modify x
, pass it by reference using a pointer:
func main() { x := 10 // Lambda function using pointer to modify the actual variable increment := func(n *int) { *n += 5 } increment(&x) fmt.Println("Modified x:", x) // Output: Modified x: 15 }
Function Type Parameter (Generics)
Define a function with one or more types as an argument.
Missing
Function Generics
import "fmt" func identity[T any](value T) T { return value } func main() { fmt.Println(identity(42)) // 42 fmt.Println(identity("Hello")) // Hello }
Function Type Parameter Constraints
Define constraints on the type parameter.
Missing
Function Generics with Constraints
And with constraints:
import "fmt" // Constraint: only types that support the + operator type Number interface { int | float64 } func add[T Number](a, b T) T { return a + b } func main() { fmt.Println(add(5, 10)) // 15 fmt.Println(add(5.5, 2.3)) // 7.8 }