Introduction
In this blog, we discuss Functions, Packages, Recursion & Closure. Recursion is the process of repeating objects in a self-similar manner. At the same time, a function is a sequence of statements that together execute a task., recursion is the way of repeating items in a self-similar way, A package is a group of similar kinds of classes, interfaces, and sub-packages, A closure is a combination of the function bundled together with references to its surrounding state.
Functions
A function is a collection of all statements that work together to complete a task. Every Go program must have at least one main function (). You can break up your written code into different functions. It's up to you how you divide your code into distinct functions, but logically, each function should be responsible for a specific duty.
A function declaration specifies the function name, return type, and parameters to the compiler. A function definition defines the body of the function.
Defining a Function
In the Go programming language, a function definition takes the following general form:
func function_name( [parameter list] ) [return_types]
{
body of the function
}
A function definition in the Go programming language consists of a function header and a function body. All of the components of the function are listed here.
Func- It is used to begin the declaration of a function.
Function Name - The function's name is what it says on the tin. The function signature is made by the function name and the argument list.
Parameters - A parameter is similar to a placeholder. You pass a value to the parameter when you call a function. An actual parameter or argument is the name given to this value. The type, order, and a number of parameters in a function are referred to as the parameter list. Parameters are optional, a function may or may not have them.
Return Type - A function may return a list of values as its return type. The data types of the values returned by the function are listed in return types. Some functions do what they're supposed to do yet don't return a value. The return type is used in this situation.
Example
A function called max is shown in the source code below (). This way accepts two parameters, num1 and num2, and returns the max difference between them-
// function definition
func max(n1 int, n2 int) int {
if(n1 > n2){
return n1
}
return n2
}
This example is for a function returning the max between two numbers.
Calling a Function
You declare the function's purpose when you create it in Go. To use a function, you must first call it to complete the specified task.
When a program calls a function, control is passed from the calling program to the called function. A called function completes a defined task and returns program control to the main program when its return statement or function-ending closing brace is reached.
You only need to give the relevant parameters along with the function name to call it. You can save the returned value if the function returns a value.
Example
// function definition
func max(n1 int, n2 int) int {
if(n1 > n2){
return n1
}
return n2
}
func main() {
// function call
result := max(21, 13)
fmt.Println("Maximum is:",result)
}
Output
Maximum is: 21
This example for function returns the max between two numbers.
Returning Multiple Values from a Function
Example
package main
import "fmt"
func swap(x string, y string) (string, string) {
return y, x
}
func main() {
a, b := swap("Rajesh", "Kumar")
fmt.Println(a, b)
}
Output
Kumar Rajesh
This example for the A Go function can return multiple values.
Function Arguments
If a function is going to use arguments, it has to declare variables that will receive the arguments' values. These variables are known as the function's formal parameters.
The formal parameters are produced when the function is entered and removed when it is exited, just like any local variables inside the function.
Arguments can be provided to a function in two different ways when calling it.
Call by value
This method inserts the real value of an argument into the formal parameter of a function. Changes to the parameter in the function have no effect on the argument in this situation.
Call by reference
The address of an argument is put into the formal parameter by this approach. The address is utilized within the function to obtain the actual parameter used in the call. This implies that changes to the parameter have an impact on the argument.
Function Usage
A function can use in the following ways:
Function as Value
Functions can create on the fly and can be used as values.
Function Closures
Functions closures are anonymous functions and can be used in dynamic programming.
Method
Methods are special functions with a receiver.