How to create a Function with Arguments but without a return value in Golang?


This tutorial will teach us how to create a function with arguments but without a return value. This tutorial involves a gist about the function, and syntax for the function with argument and without return type in Golang, then last we will see two different examples of a function with arguments but without a return type. In the first example, we are going to print the argument passed in the function with the respective statement. In the other example, we are going to add the numbers passed as arguments and print the sum in the same function.

Function with argument and without return type.

Syntax

Now we will see the syntax and explanation for the function with argument and without return type.

func functionName(argumentName1 argumentType, argumentName2 argumentType, …) {
}

Explanation

  • func is the keyword in Golang that tells the compiler that a function is defined.

  • Just next to the function we write the name of the function that must start with the letter.

  • Now we will write the arguments between curve braces() starting with the name of the argument and then its datatype.

  • As we don’t want any return type so we will not write anything between curve braces and curly braces.

  • In between curly braces we can write the logic of the function.

Algorithm

  • Step 1 − Define the variable that you want to pass with the function.

  • Step 2 − Initialize the variable.

  • Step 3 − Call the function with the respective argument.

  • Step 4 − Declare the function and write the logic in the function body.

Example 1

In this example, we are passing the argument that has the count of a number of mangoes and then printing them in the function with an argument and without a return value.

package main
import (

   // fmt package provides the function to print anything
   "fmt"
)

// declare the function with argument and without return value
func PrintTotalMangoes(numberOfMangoes int) {
   fmt.Println("The total number of mangoes is ", numberOfMangoes)
}
func main() {

   // declaring the variable
   var numberOfMangoes int
   
   // initializing the variable
   numberOfMangoes = 10
   fmt.Println("Golang program to learn how to create a function with an argument but no return value.")
   fmt.Println("Print the total number of mangoes.")
   
   // calling the function with arguments
   PrintTotalMangoes(numberOfMangoes)
}

Output

Golang program to learn how to create a function with an argument but no return value.
Print the total number of mangoes.
The total number of mangoes is 10

Example 2

In this example, we are passing the two arguments that have the value of two numbers whose sum we are printing in the function with an argument and without a return value.

package main
import (

   // fmt package provides the function to print anything
   "fmt"
)

// declare the function with argument and without return value
func Addition(number1, number2 int) {

   // declaring the variable
   var number3 int
   
   // adding value of two variables and storing in the third variable
   number3 = number1 + number2
   fmt.Printf("The addition of %d and %d is %d.\n", number1, number2, number3)
}
func main() {

   // declaring the variables
   var number1, number2 int
   
   // initializing the variable
   number1 = 10
   number2 = 8
   fmt.Println("Golang program to learn how to create a function with an argument but no return value.")
   fmt.Println("Print the addition of two numbers.")
   
   // calling the function with arguments
   Addition(number1, number2)
}

Output

Golang program to learn how to create a function with an argument but no return value.
Print the addition of two numbers.
The addition of 10 and 8 is 18.

Conclusion

These are the two examples of a function with arguments and without a return value. The main user-case of these types of functions is that if you want to perform some operation on variables in a function and want to print in the same function then the function with argument and without return value is the right choice. To learn more about Golang you can explore these tutorials.

Updated on: 17-Jan-2023

219 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements