How to Add Two Numbers in Golang?


In this tutorial, we will discuss adding two numbers in Golang. We will cover two approaches first adding two numbers within the function and second creating a different function.

Adding two numbers within the function

Algorithm

  • STEP 1 − Defining the variables that we want to add.

  • STEP 2 − Initializing the variables.

  • STEP 3 − Add two numbers and store them in the third variable.

  • STEP 4 − Print the result after adding two numbers.

Example 1

In this example, we will add two integers within the function.

package main // fmt package provides the function to print anything import "fmt" func main() { // define the variables we want to add var number1, number2, number3 int // initializing the variables number1 = 99 number2 = 81 // adding the numbers number3 = number1 + number2 // printing the results fmt.Println("The addition of ", number1, " and ", number2, " is \n ", number3, "\n(Addition of two integers within the function)") }

In the above code first, we are declaring two integer variables then we are initializing the variables. In the next step, we are adding both the values and storing them into the third integer variable. In end, we are printing the result of the addition in the last step.

Output

The addition of 99 and 81 is
180
(Addition of two integers within the function)

Example 2

In this example, we will add two floats number within the function.

package main // fmt package provides the function to print anything import "fmt" func main() { // define the float32 variables we want to add var number1, number2, number3 float32 // initializing the variables number1 = 74 number2 = 22 // adding the float32 numbers number3 = number1 + number2 // printing the results fmt.Println("The addition of ", number1, " and ", number2, " is \n", number3, "\n(Adding two float numbers within the function)") }

In the above code first, we are declaring two float32 variables then we are initializing the variables. In the next step, we are adding both the values and storing them into the third float32 variable. In end, we are printing the result of the addition in the last step.

Output

The addition of 74 and 22 is
96
(Adding two float numbers within the function)

Adding two numbers outside the function

Algorithm

  • STEP 1 − Defining the variables that we want to add.

  • STEP 2 − Initializing the variables.

  • STEP 3 − Adding two numbers by calling addNumber() function and storing them in the third variable.

  • STEP 4 − Printing the result

Example 1

In this example, we will add two integers by calling a function outside of the main.

package main // fmt package provides the function to print anything import "fmt" // function to add the two integer numbers func addNumber(number1, number2 int) int { return number1 + number2 } func main() { // define the integer variables we want to add var number1, number2, number3 int // initializing the variables number1 = 18 number2 = 9 // calling the function and storing the result number3 = addNumber(number1, number2) // printing the results fmt.Println("The addition of ", number1, " and ", number2, " is \n", number3, "\n(adding two integers outside the function)") }

In the above code first, we are declaring two integer variables and initializing them in the next step. Then we are calling addNumber() function which we have created outside the function and stored in the third integer variable and last printing the result after addition.

Output

The addition of 18 and 9 is
27
(adding two integers outside the function)

Example 2

In this example, we will add two float by calling a function outside of the main.

package main // fmt package provides the function to print anything import "fmt" // function to add the two float32 numbers func addNumber(number1, number2 float32) float32 { return number1 + number2 } func main() { // define the float32 variables we want to add var number1, number2, number3 float32 // initializing the variables number1 = 2.333 number2 = 4.87 // calling the function and storing the result number3 = addNumber(number1, number2) // printing the results fmt.Println("The addition of ", number1, " and ", number2, " is \n", number3, "\n(adding two float numbers outside the function)") }

In the above code first, we are declaring two integer variables and initializing them in the next step. Then we are calling addNumber() function which we have created outside the function and stored in the third integer variable and last printing the result after addition.

Output

The addition of 2.333 and 4.87 is
7.203
(adding two float numbers outside the function)

This is all about adding two numbers. Also, If we talk about which ay is better like adding within the function or outside a function and outside the function approach is better so that we can use the function at different places. To learn more about Golang you can explore these tutorials.

Updated on: 26-Aug-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements