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


This tutorial will teach us how to create a function with arguments and a return value. This tutorial involves a gist about the function, and syntax for the function with an argument and a return type in Golang, then last we will see two different examples of a function with arguments and with a return type. In one example we will return the sum of two numbers and in the other one, we will return the area of the circle.

Functions in the Programming language.

Let us first see what function is. The function is a subset of a program that makes the code modular. Also, the functions make specific codes reusable. The function supports the argument while calling them and can also have a return type as per our requirement.

Function with Argument and with a Return Type

Syntax

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

func functionName(argumentName1 argumentType, argumentName2 argumentType, …) (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 the arguments between curve braces() starting with the name of the argument and then its datatype.

  • 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 with 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 the last.

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

Example 1

In this example, we are going to create a function to find the sum of two numbers where we are passing the number as an argument and returning the sum.

package main
import (

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

// declaring a function with argument and with return value
func sumOfTwoNumbers(number1, number2 int) int {

   // returning the sum
   return number1 + number2
}
func main() {

   // declaring the variables
   var number1, number2, sumOfNumbers int
   
   // initializing the variables
   number1 = 21
   number2 = 32
   fmt.Println("Golang program to learn how to create a function with argument and with the return value.")
   fmt.Println("Finding sum of two numbers.")
   
   // calling the function by passing both the numbers as an argument and storing the value return by function in a variable
   sumOfNumbers = sumOfTwoNumbers(number1, number2)
   
   // printing the result
   fmt.Printf("The sum of %d and %d is %d. \n", number1, number2, sumOfNumbers)
}

Output

Golang program to learn how to create a function with argument and with the return value.
Finding the sum of two numbers.
The Sum of 21 and 32 is 53.

Example 2

In this example, we are going to create a function to find the area of the circle where we are passing the radius as an argument and returning the area.

package main
import (

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

// declaring function with argument and with return value
func AreaOfCircle(radius float32) float32 {

   // returning the area of the circle
   return (22 / 7.0) * radius * radius
}
func main() {

   // declaring the variable
   var radius, areaOfCircle float32
   
   // initializing the variable
   radius = 3.5
   fmt.Println("Golang program to learn how to create a function with argument and with the return value.")
   fmt.Println("Finding the area of the circle.")
   
   // calling the function by passing radius as argument and storing the value area return by function in a variable
   areaOfCircle = AreaOfCircle(radius)
   
   // printing the result
   fmt.Printf("The area of the circle with radius %f cm is %f cm^2. \n", radius, areaOfCircle)
}

Output

Golang program to learn how to create a function with argument and with return value.
Finding the area of the circle.
The area of the circle with radius 3.500000 cm is 38.500000 cm^2.

Conclusion

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

Updated on: 11-Jan-2023

625 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements