GoLang Functions
Introduction:
Functions are building blocks of a program. They encapsulate logic, help in reusable code, and also organize the code into simple and manageable pieces. In this section, we will explore how functions work in GoLang.
What are Functions in Go?
Like all other functions, a function here is a block of code that performs a particular task. Function accepts the input, which is passed as parameters, processes them, and returns values as output.
Basic Syntax:
func functionName(parameter1 type1, parameter2 type2) returnType {
// function body
return value
}
Example:

Output:
Hello, Divya!
Function Parameters:
A function can accept zero or more parameters. Parameters are given as a name followed by their data type.
Single Parameter:
func square(x int) int {
return x * x
}
Here variable or parameter name is x, and its data type is “int”.
Multiple Parameters:
func add(a int, b int) int
{
return a + b
}
Inside the function, we are passing two parameters, a
and b
. Instead of specifying the data type separately for each variable, we can declare a common type for both.
func add(a, b int) int {
return a + b
}
Variadic Functions:
Go supports variadic functions, which allow a function to accept a variable number of arguments as parameters.
Example:

Output:
6
15
Return Values:
Go functions can return zero, one, or multiple values.
The examples shared earlier return a single value as the output.
One of Go’s unique features is that functions can return more than one value. This is particularly useful when you want to return a result along with an error or status indicator.

Output:
Error: division by zero
In this example, an error is returned because a number cannot be divided by zero. However, if valid values are passed (where the divisor is not zero), the function returns the correct result.

Output:
Result: 5
This pattern is common in Go and is preferred over exceptions. It encourages developers to explicitly handle errors, improving code reliability and readability.
Named Return values:
In Go, we can name the return values in a function’s signature. It enhances readability and simplifies the function structure. This approach allows to assign values directly within the function body without the need to declare additional variables. Additionally, it enables the use of a naked return—a return statement without any arguments—since the named return variables are automatically returned. This feature is particularly useful in short functions where the role of each return value is obvious, making the code more concise and easier to understand.
Example:

Output:
Area:15
Perimeter:16
Higher-Order Functions:
Functions that take other functions as arguments or return functions as results are called Higher-order functions.
Example:

Output: 5
Here, applyorder is a higher-order function because it accepts both integers and a function as arguments.
Conclusion:
In this section, we explored how functions are used in GoLang.
Previous post: https://pheonixsolutions.com/blog/go-lang-variables/