Function Arguments in Golang


When it comes to writing programs in Golang, understanding how to pass values to functions is essential. In this article, we will explore the basics of function arguments in Golang and learn how to pass values to functions effectively.

What are Function Arguments?

In Golang, a function argument is a value that is passed to a function when it is called. These values are used by the function to perform a task or calculation, and they can be of any data type, such as integers, strings, or booleans.

How to pass Function Arguments in Golang?

In Golang, function arguments are passed by value, which means that a copy of the argument value is created and passed to the function. This is different from passing arguments by reference, which is commonly used in other programming languages like C++ or Java.

To pass values to a function in Golang, we simply include the argument values inside the parentheses when we call the function. Here is an example −

Example

package main

import "fmt"

func main() {
   x := 10
   y := 20

   sum := add(x, y)
   fmt.Println(sum)
}

func add(a, b int) int {
   return a + b
}

Output

30

In this example, we define a function called "add" that takes two integer arguments, "a" and "b", and returns their sum. We then call the "add" function in the "main" function and pass it the values of "x" and "y". The "add" function adds these values together and returns the result, which is then assigned to the variable "sum".

Types of Function Arguments in Golang

In Golang, there are two types of function arguments: positional arguments and named arguments.

Positional arguments are the most common type of function arguments in Golang. They are passed to the function in the order that they appear in the function definition. For example −

Example

package main

import "fmt"

func greet(name string, age int) {
   fmt.Printf("Hello, %s! You are %d years old.\n", name, age)
}

func main() {
   greet("John", 30)
}

Output

Hello, John! You are 30 years old.

In this example, we define a function called "greet" that takes two arguments, "name" and "age", and prints out a greeting message. We then call the "greet" function in the "main" function and pass it the values of "name" and "age" as positional arguments.

Named arguments are a newer feature in Golang and allow us to pass arguments to a function using their names, rather than their position. This can be useful when we want to skip optional arguments or pass them in a different order. For example −

Example

package main

import "fmt"

func greet(name string, age int, city string) {
   fmt.Printf("Hello, %s! You are %d years old and live in %s.\n", name, age, city)
}

func main() {
   greet("John", 30, "New York")
}

Output

Hello, John! You are 30 years old and live in New York.

In this example, we define a function called "greet" that takes three arguments, "name", "age", and "city", and prints out a greeting message. We then call the "greet" function in the "main" function and pass it the values of "name", "age", and "city" as named arguments.

Conclusion

In summary, function arguments in Golang are essential for passing values to functions and enabling them to perform their intended tasks. By understanding the basics of function arguments and the different types of arguments available in Golang, you can write more efficient and effective programs that meet your programming needs.

Updated on: 18-Apr-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements