In depth analysis of the use of functions in golang programming

  • 2020-05-30 20:20:38
  • OfStack

The function is a set of statements that execute the task. Each Go program has at least one function, which is generally main(), and all the most trivial programs can define additional functions.

You can put code into separate functions. How to partition different functions between code, but the logical division is usually to have each function perform a specific task.

The function declaration tells the compiler about the function name, return type, and parameters. A function definition provides the actual body of the function.

The Go language standard library provides a number of built-in functions that programs can call. For example, the function len() requires different types of arguments and the length of the type of the return value. For example, if a string is passed to it, it will return the length of the string in bytes, and if an array is passed to it, it will return the length of the array as the number of elements it has.

Functions are called methods or subroutines or programs with various names, etc.

Define 1 function:
The function definition in the Go programming language has the following general form:


func function_name( [parameter list] ) [return_types]
{
   body of the function
}

Functions in the Go programming language are defined by function headers and function bodies. Here are all the parts of a function:

Declaration of the func start function. Function Name: this is the actual name of the function. The function name and parameter list 1 together form the function signature. Parameters: the parameter is like a placeholder. When you call a function, you pass a parameter of a value. This value is called the actual parameter or parameter. The parameter list refers to the type, order, and number of function parameters. Parameters are optional; That is, a function can contain any argument. Return Type: a list of values that the function may return. return_types is a list of the data types that the function returns. Some functions perform required operations without returning a value. In this case, return_type is not required.

Function Body: the function body contains a collection of statements that define function operations.

Example:
Below is the source code for a function called max(). This function takes two parameters, num1 and num2, and returns the maximum value between them:


/* function returning the max between two numbers */
func max(num1, num2 int) int
{
   /* local variable declaration */
   result int    if (num1 > num2) {
      result = num1
   } else {
      result = num2
   }
   return result
}

Call 1 function:
One definition is required to create a function programmed by Go. To use a function, you need to call it to perform the specified task.

When a program calls a function, program control is transferred to the function being called. When the function definition is called to perform a task, the return statement that is executed or the closing bracket that reaches the end of its function is returned to the main program.

To call a function, simply take the necessary arguments and the name of the function, and if the function returns a value, store the return value. Such as:


package main import "fmt" func main() {
   /* local variable definition */
   var a int = 100
   var b int = 200
   var ret int    /* calling a function to get max value */
   ret = max(a, b)    fmt.Printf( "Max value is : %d\n", ret )
} /* function returning the max between two numbers */
func max(num1, num2 int) int {
   /* local variable declaration */
   var result int    if (num1 > num2) {
      result = num1
   } else {
      result = num2
   }
   return result
}

Keep the max() and main() functions and compile the source code. When running the final executable, it produces the following results:


Max value is : 200

Returns multiple values from a function
Go language functions can return multiple values. Such as:


 package main import "fmt" func swap(x, y string) (string, string) {
   return y, x
} func main() {
   a, b := swap("Mahesh", "Kumar")
   fmt.Println(a, b)
}

Let's compile and run the above program, which will produce the following results:


Kumar Mahesh



Related articles: