Go - Functions

Every Go application has as a minimum one feature, that's major(). You can divide your code into separate capabilities.

A feature declaration tells the compiler about a function name, return type, and parameters. A function definition affords the real frame of the function.

For example, the function len() takes arguments of various types and returns the length of the type.

Functions are also known as method, sub-routine, or procedure.

Defining a Function

The simple form of a function definition in Go programming language is as follows −

func function_name( [parameter list] ) [return_types]

   body of the function

A feature definition in Go programming language consists of a feature header and a function frame.

  • Func − It starts offevolved the declaration of a function.

  • Function Name − It is the real call of the function. The function name and the parameter list together represent the function signature.

  • Parameters − A parameter is sort of a placeholder. When a function is invoked, you bypass a fee to the parameter. This price is called actual parameter or argument. The parameter listing refers to the type, order, and wide variety of the parameters of a function. Parameters are optional; this is, a function may also contain no parameters.

  • Return Type − A function may additionally return a listing of values. The return_types is the list of facts varieties of the values the function returns. Some functions perform the desired operations without returning a fee. In this example, the return_type is the no longer required.

  • Function Body − It incorporates a collection of statements that define what the function does.


The following source code suggests a function known as max(). This feature takes parameters product1 and product2 and returns the most among the two
/* feature returning the max among two productbers */
func max(product1, product2 int) int 
    /* local variable declaration */
    result int

    if (product1 > product2) 
        end result = product1
        end result = product2
    go back result

Calling a Function

Go feature, you supply a definition of what the function has to do. To use a function, you'll have to call that feature to perform the described venture.When a program calls a function, this system manage is transferred to the called function. A known as feature plays a defined mission and whilst its go back declaration is executed or when its function-finishing ultimate brace is reached, it returns the program manage returned to the main program.

package deal main
import "fmt"

func principal() 
    /* neighborhood variable definition */
    var a int = 300
    var b int = two hundred
    var ret int

    /* calling a feature to get max cost */
    ret = max(a, b)

    fmt.Printf( "Max value is : %dn", ret )

/* function returning the max between two productbers */
func max(product1, product2 int) int 
    /* nearby variable announcement */
    var end result int

    if (product1 > product2) 
        end result = product1
        end result = product2
    return result

We have kept the max() function in conjunction with the principle() feature and compiled the source code. While strolling the final executable, it would produce the following end result
$move run foremost.Cross
Max value is : three hundred

Returning multiple values from Function

A Go function can go back a multiple of values. For example
package important

import "fmt"

func swap(x, y string) (string, string) 
   return y, x

func fundamental() 
   a, b := swap("India", "Summar")
   fmt.Println(a, b)

When the above code is compiled and executed, it produces the following end result -
$pass run fundamental.Pass
Summar India

Function Arguments

If a feature is to use arguments, it need to claim variables that receive the values of the arguments. These variables are known as the formal parameters of the feature. While calling a feature, there are methods that arguments can be surpassed to a feature-
Sr.No Call Type & Description
1 Call by using cost

This technique copies the real cost of an issue into the formal parameter of the feature. In this situation, changes made to the parameter inside the function don't have any effect at the argument.

2 Call by using reference

This method copies the address of a controversy into the formal parameter. Inside the function, the deal with is used to access the real argument used within the call. This method that adjustments made to the parameter have an effect on the argument.

Function Usage

A function may be used inside the following ways:

Sr.No Function Usage & Description
1 Function as Value

Functions may be created at the fly and may be used as values.

2 Function Closures

Functions closures are nameless features and can be utilized in dynamic programming.

3 Method

Methods are unique features with a receiver.