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


This tutorial will teach us how to create a function without arguments and with a return value. This tutorial involves a gist about the function, and syntax for the function without an argument and with a return type in Golang, then last we will see two different examples of a function without arguments and with a return type. In both examples, we are going to create two functions which are returning a string whenever called.

Function without argument and with a return type.

Syntax

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

func functionName( ) (returnType1, returnType2, …) {
   return returnValue1, returnValue2, …
}

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 empty curve braces () as we don’t want the function with the argument.

  • To mention the return type we will write them in separate curve braces() just after the declaration of arguments.

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

  • Once the whole logic is written we will use the return keyword to return all the values that we want to return.

Algorithm

  • Step 1 − Start the main() function.

  • Step 2 − Call the function without argument and with return value and store the value in a respective variable.

  • Step 3 − Declare the function, write the logic in the function body, and return the value in last.

  • Step 4 − Perform the required operation on the value returned by the function.

Example 1

In this example, we are creating two functions FirstSmall() and SecondSmall() returning the string that is called if the first number is small or the second number is small respectively.

package main
import (

   // fmt package provides the function to print anything
   "fmt"
)
func FirstSmall() string {
   return "First number is smaller than the second."
}
func SecondSmall() string {
   return "Second number is smaller than the first."
}
func main() {

   // declaring the variables
   var firstNumber, secondNumber int
   
   // initializing the variable
   firstNumber = 10
   secondNumber = 8
   fmt.Println("Golang program to learn how to create a function without argument but with return value.")
   fmt.Println("Comparing the numbers and printing the message by calling a function.")
   if firstNumber < secondNumber {
      fmt.Println(FirstSmall())
   } else {
      fmt.Println(SecondSmall())
   }
}

Output

Golang program to learn how to create a function without argument but with return value.
Comparing the numbers and printing the message by calling a function.
Second number is smaller than the first.

Example 2

In this example, we are creating two functions evenNumber() and oddNumber() returning the string that is called if the number is even or if the number is odd respectively.

package main
import (

   // fmt package provides the function to print anything
   "fmt"
)
func evenNumber() string {
   return "Number is even."
}
func oddNumber() string {
   return "Number is odd."
}
func main() {

   // declaring and initializing the array
   numberArray := [5]int{32, 45, 67, 3, 88}
   fmt.Println("Golang program to learn how to create a function without argument but with return value.")
   fmt.Println("Checking the array element is odd or even.")
   
   // running for loop on the array
   for i := 0; i < 5; i++ {
   
      // checking the number at index i is odd or even
      if numberArray[i]%2 == 0 {
         fmt.Println(numberArray[i], evenNumber())
      } else {
         fmt.Println(numberArray[i], oddNumber())
      }
   }
}

Output

Golang program to learn how to create a function without argument but with return value.
Checking the array element is odd or even.
32 Number is even.
45 Number is odd.
67 Number is odd.
3 Number is odd.
88 Number is even.

Conclusion

These are the two examples of a function without arguments and with a return value. The main use case of these types of functions is that if you want to perform some operation of the same type multiple times and return the same value then we can create a function without argument and with the return value. To learn more about Golang you can explore these tutorials.

Updated on: 11-Jan-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements