Algorithm
In Go, a function is a group of statements that together perform a task. Here's a basic structure of a function in Go:
go
func functionName(parameters) returnType {
// function body
// code to perform the task
return result // optional return statement
}
Let's break down the components:
func
: Keyword used to declare a function.functionName
: Name of the function. It should be a valid identifier.parameters
: Input values that the function takes. It's a comma-separated list of parameter names and their types.returnType
: The type of the value that the function returns. If a function doesn't return any value, you can omit this part.function body
: The block of code enclosed in curly braces{}
that defines what the function does.return
: Keyword used to specify the value to be returned by the function. This part is optional.
Code Examples
#1 Create a Go Lang Function
Code -
Golang Programming
package main
import "fmt"
func add(x, y int) int {
return x + y
}
func main() {
result := add(3, 5)
fmt.Println("The result is:", result)
}
In this example, the add function accepts two parameters (x and y) of type int and returns their sum, also of type int. The main function then calls add with the arguments 3 and 5, and prints the result to the console.
Copy The Code &
Try With Live Editor
#2 Code Example- Golang Function
Code -
Golang Programming
package main
import "fmt"
// addNumbers is a function that takes two integers as parameters and returns their sum.
func addNumbers(a, b int) int {
return a + b
}
func main() {
// Calling the addNumbers function with arguments 5 and 3
result := addNumbers(5, 3)
// Printing the result
fmt.Println("The sum is:", result)
}
Copy The Code &
Try With Live Editor
Output
Function Parameters in Golang
Code -
Golang Programming
func functionName(parameter1 type1, parameter2 type2, ...) returnType {
// function body
}
func: The keyword used to declare a function. functionName: The name of the function. (parameter1 type1, parameter2 type2, ...): The parameters of the function, where each parameter is declared with its name followed by its type. returnType: The type of the value that the function returns. If a function doesn't return any value, you can omit this part.
Copy The Code & Try With Live Editor#4 Function Parameters in Golang
Code -
Golang Programming
package main
import "fmt"
// add is a function that takes two integers as parameters and returns their sum.
func add(a int, b int) int {
return a + b
}
func main() {
result := add(3, 5)
fmt.Println("Sum:", result)
}
In this example, the add function takes two parameters (a and b), both of type int, and returns an int. The main function then calls add with the arguments 3 and 5, and prints the result.
Copy The Code & Try With Live Editor#5 Function Parameters in Golang
Code -
Golang Programming
func sum(numbers ...int) int {
total := 0
for _, num := range numbers {
total += num
}
return total
}
func main() {
result := sum(1, 2, 3, 4, 5)
fmt.Println("Sum:", result)
}
Copy The Code &
Try With Live Editor
#6 Return Value from Go Function
Code -
Golang Programming
package main
import "fmt"
// Function that returns a string
func greet(name string) string {
return "Hello, " + name + "!"
}
func main() {
// Calling the greet function and printing the returned value
message := greet("John")
fmt.Println(message)
}
Copy The Code &
Try With Live Editor
#7 Return Value from Go Function
Code -
Golang Programming
package main
import "fmt"
// Function that returns two integers
func addAndSubtract(a, b int) (int, int) {
sum := a + b
difference := a - b
return sum, difference
}
func main() {
// Calling the addAndSubtract function and capturing both returned values
result1, result2 := addAndSubtract(10, 5)
// Printing the results
fmt.Println("Sum:", result1)
fmt.Println("Difference:", result2)
}
In this example, the addAndSubtract function takes two integer parameters and returns two integers. The main function calls addAndSubtract with the arguments 10 and 5, and captures both returned values to print them separately.
Copy The Code & Try With Live Editor#8 Return Multiple Values from Go Function
Code -
Golang Programming
// Program to return multiple values from function
package main
import "fmt"
// function definition
func calculate(n1 int, n2 int) (int, int) {
sum := n1 + n2
difference := n1 - n2
// return two values
return sum, difference
}
func main() {
// function call
sum, difference := calculate(21, 13)
fmt.Println("Sum:", sum, "Difference:", difference)
}
Copy The Code &
Try With Live Editor
Output
#9 Benefits of Using Functions
Code -
Golang Programming
// Program to illustrate code reusability in function
package main
import "fmt"
// function definition
func getSquare(num int) {
square := num * num
fmt.Printf("Square of %d is %d\n", num, square)
}
// main function
func main() {
// call function 3 times
getSquare(3)
getSquare(5)
getSquare(10)
}
Copy The Code &
Try With Live Editor
Output
Square of 5 is 25
Square of 10 is 100
Demonstration
Go Lang Programming Functions