Golang (Go) Tutorial - Functions
โ๏ธ Functions - Reusable Code
๐ Real Life:
๐งฎ Calculator:
Input: 5, 3 โ Add function โ Output: 8
๐ฑ Phone Call:
Input: number โ Dial function โ Output: connected
๐ Function Syntax
func functionName(param type) returnType {
// code
return value
}
// Example
func add(a int, b int) int {
return a + b
}
๐ฏ Function Types
| Type | Example |
|---|---|
| No parameters | func greet() { } |
| With parameters | func add(a, b int) { } |
| Return value | func add() int { } |
| Multiple returns | func divide() (int, error) |
Example
func add(a, b int) int {
return a + b
}
result := add(5, 3)