...

Web Technologies - Go Language

Back to Course

Lesson Description


Lession - #358 GO-Functions


Go - Functions

A function is a gathering of explanations that together play out an errand. Each Go program has something like one capacity, which is primary(>
. You can partition your code into independent capacities. How you split your code between various capacities really depends on you, yet consistently, the division ought to be with the end goal that each capacity plays out a particular assignment. A function announcement educates the compiler regarding a capacity name, return type, and boundaries. A capacity definition gives the real body of the capacity. The Go standard library gives various inherent capacities that your program can call. For instance, the capacity len(>
takes contentions of different sorts and returns the length of the kind. On the off chance that a string is passed to it, the capacity returns the length of the string in bytes. In the event that an exhibit is passed to it, the capacity returns the length of the cluster.

Defining a Function


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

A function definition in Go programming language comprises of a capacity header and a capacity body. Here are the pieces of a capacity −
  • Func − It begins the statement of a capacity.
    • Function Name − It is the real name of the capacity. The capacity name and the boundary list together comprise the capacity signature.
    • Paameters − A parameter resembles a placeholder. Whenever a capacity is summoned, you pass a worth to the boundary. This worth is alluded to as real boundary or contention. The boundary list alludes to the sort, request, and number of the boundaries of a capacity. Boundaries are discretionary; that is, a capacity might contain no boundaries.
    • Return Type − A capacity might return a rundown of values. The return_types is the rundown of information kinds of the qualities the capacity returns. A few capacities play out the ideal tasks without returning a worth. For this situation, the return_type is the not needed.
    • Function Body − It contains an assortment of proclamations that characterize what the capacity does.


    Calling a Function

    While making a Go function, you give a meaning of what the capacity needs to do. To utilize a capacity, you should call that capacity to play out the characterized task. Whenever a program calls a function, the program control is moved to the called work. A called work plays out a characterized errand and when its return proclamation is executed or when its capacity finishing shutting support is reached, it gets the program control once again to the principal program. To call a function, you just have to pass the necessary parameters alongside its function name. In the event that the capacity returns a worth, you can store the brought esteem back. For instance
    
    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 }


    Returning multiple values from Function

    
    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>
    }