Golang Tutorial Index


In the Go programming language, a function is a collection of statements that performs a specific task and can be reused throughout the program. Functions are used to break down large programs into smaller, more manageable pieces of code, which helps improve the code's readability and maintainability.



Creating a Function

To create a function in Go, first, define its name using the "func" keyword, followed by a set of parentheses that enclose its parameters. The parameters specify the inputs that the function expects. You can omit the parentheses if the function doesn't expect any inputs. After the parameters, you specify the function's return type, followed by curly braces {} enclosing the function body. The function's body contains the actual code that performs the task assigned to the function. You can also specify a return statement that returns values from the function.

Syntax:

func functionName(parameter1 type, parameter2 type) returnType {
   // Function body: code to execute
   return value // optional return statement
}

In the above syntax, "functionName" is the function's name. The parameters are listed inside the parentheses, separated by commas. Each parameter is declared with its type. The "returnType" specifies the type of data that the function returns. The function body contains the code that performs the task assigned to the function. Finally, the return statement returns a value from the function.

For example, consider the following example of a simple function that takes two integers as input and returns their sum:

Example:

func add(x int, y int) int {
    return x + y
}

Calling a Function

To call a function in Go, you need to use its name, followed by a set of parentheses that enclose its parameters. For example, to call the "add" function, you can pass two integers as inputs and assign the function's return value to a variable. Then you can print the variable to display the output:

Example:

package main
import "fmt"

func add(x, y int) int {
    return x + y
}

func main() {
    sum := add(3, 5)
    fmt.Println(sum) // Output: 8
}

Function Parameters

In Go, you can define a function with zero or more parameters. Parameters are the inputs that the function expects. They are declared inside parentheses after the function name. Each parameter is defined with a comma-separated name and type. Here's an example of a function that takes three integers as input and returns their product:

Example:

func multiply(x, y, z int) int {
    return x * y * z
}

Function Return Values

Functions in Go can return zero or more values. The function's return type is specified after the parameters inside the parentheses. You can omit the parentheses if the function doesn't return any values. Here is an example of a function that takes two integers as inputs and returns their quotient and remainder:

Example:

func divide(x, y int) (int, int) {
    return x / y, x % y
}

In the above example, the function returns two integers - the quotient and the remainder of dividing x by y. A comma inside the parentheses separates these values.

Function Variables

In Go, you can define variables inside a function. These variables are visible only within the function and are destroyed after the function completes. Here's an example of a function that defines a variable and returns its value:

Example:

package main
import "fmt"

func greet(name string) string {
    greeting := "Hello, " + name + "!"
    return greeting
}

func main() {
    fmt.Println(greet("W3schools")) // Output: Hello, W3schools!
}

In the above example, the function defines a string variable called "greeting", which is initialized with the value "Hello, " + name + "!". The function then returns the value of the greeting variable.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram