WEB DEVELOPER SITE
htmLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Go - Functions

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

A feature statement tells the compiler about a characteristic call, return kind, and parameters. A function definition affords the real frame of the characteristic.

Functions also are called approach, sub-habitual, or system.

Defining a Function

func function_name( [parameter list] ) [return_types]

   body of the characteristic

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

  • Func − It starts offevolved the assertion of a characteristic.

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

  • Parameters − A parameter is sort of a placeholder. When a characteristic 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 characteristic 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 characteristic does.

Example

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
     else 
        end result = product2
    
    go back result

Calling a Function

Go feature, you supply a definition of what the characteristic has to do. To use a characteristic, 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 characteristic. A known as feature plays a defined mission and whilst its go back assertion is executed or when its characteristic-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 )


/* characteristic 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
     else 
        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 characteristic can go back a couple 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 characteristic 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 characteristic 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.

three Method

Methods are unique features with a receiver.