WEB DEVELOPER SITE
htmLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Go - Functions

The function is the basic block of code used to perform a task.

The Go language has at least one main() function.

You can use functions to divide different functions, logically each function performs a specified task.

The function declaration tells the name, return type, and argument of the compiler function.

The

Go Language Standard Library provides a variety of built-in functions that can be used. For example, the len() function can accept different types of arguments and return the length of the type. Returns the length of the string if we pass in a string, and returns the number of elements contained in the array if an array is passed.


function definition

The

Go language function definition format is as follows:

func function_name( [argument list] ) [ Return_types] {
   Function body
}

Function definition parsing:

  • func: The function is declared by func
  • function_name: The function name, function name and argument list together form the function signature.
  • argument list: A list of arguments. A argument is like a placeholder. When a function is called, you can pass the value to the argument. This value is called the actual argument. The argument list specifies the argument type, order, and number of arguments. The argument is optional, which means that the function can also contain no arguments.
  • return_types: return type, the function returns a list of values. Return_types is the data type of the column value. Some features do not require a return value, in which case return_types is not required.
  • Function body: A collection of code defined by a function.

The following example is the code for the max() function, which passes in two integer arguments num1 and num2 and returns the maximum of these two arguments:

example

/* The function returns the maximum of two numbers */
func max(num1, num2 int) int {
   /* Declare local variables */
   var result int

   if (num1 > num2) {
      result = num1
   } else {
      result = num2
   }
   return result
}

Function call

When you create a function, you define what the function needs to do, by calling the function to perform the specified task.

Call the function, pass arguments to the function, and return a value, for example:

example

package main

import "fmt"

func main() {
   /* Defining local variables */
   var a int = 100
   var b int = 200
   var ret int

   /* Call the function and return the maximum value */
   ret = max(a, b)

   fmt.Printf( "The maximum is : %d\n", ret )
}

/* The function returns the maximum of two numbers */
func max(num1, num2 int) int {
   /* Defining local variables */
   var result int

   if (num1 > num2) {
      result = num1
   } else {
      result = num2
   }
   return result
}

The above example calls the max() function in the main() function, and the result is:

Maximum is :< /span> 200

function returns multiple values

The

Go function can return multiple values, for example:

example

package main

import "fmt"

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

func main() {
   a, b := swap("Google", "welookups")
   fmt.Println(a, b)
}

The above example execution result is:

Function Arguments

A function can be called a formal argument of a function if it uses a argument.

A formal argument is like a local variable defined in the body of a function.

The calling function can pass arguments in two ways:

Sr.No Call Type & Description
1 Call by using value

Value passing means passing a copy of the actual argument to the function when the function is called, so that if the argument is modified in the function, it will not affect the actual argument.

2 Call by using reference

Reference passing refers to passing the address of the actual argument to the function when the function is called. Then the modification of the argument in the function will affect the actual argument.

By default, the Go language uses value passing, which means that the actual arguments are not affected during the call.

Function Usage

A function may be used inside the following ways:

Sr.No Function Usage & Description
1 Function as Value

The function can be passed as a real argument to another function.

2 Function Closures

Closures are anonymous functions that can be used in dynamic programming

3 Method

The method is a function that contains the recipient