Golang Program to Create a Function without Argument and Without a Return Value


In this tutorial we will learn how to create a function without argument and without a return value in Go Programming Language.

When a function has no arguments, it does not receive any data from the calling function. Similarly when it does not return any value, the calling function does not receive any data from the called function. So there is no data transfer between calling and called function.

Add two Numbers

Algorithm

  • Step 1 − Import the package fmt package

  • Step 2 − Start function main ()

  • Step 3 − Calling the function add ()

  • Step 4 − Start the add () function

  • Step 5 − Declare and initialize the variables

  • Step 6 − Print the result on the console screen using fmt.Printf ()

Example

We will add two numbers by creating a function without any argument and without a return value

// GOLANG PROGRAM TO CREATE A FUNCTION WITHOUT // ARGUMENT AND WITHOUT A RETURN VALUE package main // fmt package provides the function to print anything import "fmt" // Starting the function main() // GO program execution starts with the function main() func main() { // Function Calling // Function Definition add() } func add() { // declare and initialize the variables var a int = 30 var b int = 60 var c int fmt.Println("Golang program to create a function without argument and without a return value") // Print the result c = a + b fmt.Printf("Addition : %d",c) }

Output

Golang program to create a function without argument and without a return value
Addition : 90

Description of the Code

  • In the above program, we first declare the package main.

  • We imported the fmt package that includes the files of package fmt

  • Now start the function main ().GO program execution starts with the function main ()

  • Next we call the function add ().

  • Now we start the function add (). Declare and initialize the integer variables

  • The variables ‘a’ and ‘b’ corresponding to the two integer variables that are added. The integer variable ‘c’ corresponds to the result after the calculation.

  • The final result is printed on the console screen using the built-in function fmt.Printf ().This function is defined under the fmt package and it helps to write standard output.

  • In the above program, add ( ); function performs addition and no arguments are passed to this function. The return type of this function is void and hence return nothing.

Find Area of a Square

Algorithm

  • Step 1 − Import the package fmt package

  • Step 2 − Start function main ()

  • Step 3 − Calling the function area ()

  • Step 4 − Start the area () function

  • Step 5 − Declare and initialize the variables

  • Step 6 − Print the result on the console screen using fmt.Printf ()

Example

We will find area of a square creating a function without any argument and without a return value

// GOLANG PROGRAM TO CREATE A FUNCTION WITHOUT // ARGUMENT AND WITHOUT A RETURN VALUE package main // fmt package provides the function to print anything import "fmt" // function prototype // GO program execution starts with the function main() func main() { // function call area() } func area() { fmt.Println("Golang program to create a function without argument and without a return value") // declare and initialize the variables var square_area int var square_side int square_side = 7 square_area = square_side * square_side // print the result fmt.Printf("Area of the Square is %d",square_area) }

Output

Golang program to create a function without argument and without a return value
Area of the Square is 49

Description of the Code

  • In the above program, we first declare the package main.

  • We imported the fmt package that includes the files of package fmt

  • Now let us start the function main(). GO program execution starts with the function main()

  • Next we call the function area ().

  • Now we start the function area (). Declare and initialize the integer variables square_area and square_side

  • The variable square_side corresponds to the value given to side of the square and square_area corresponds to the result of the calculation to find the area of a square

  • In the above program, area ( ), the function performs the calculation and no arguments are passed to this function. The return type of this function is void and hence no return.

  • The final result is printed on the console screen using the built-in function fmt.Printf (). This function is defined under the fmt package and it helps to write standard output.

Conclusion

In the above two examples we have successfully compiled and executed the Golang program code to create a function without argument and without a return value. In both the examples of golang program, we have shown that; after calling, the return type function performs the calculation and the result is printed on the screen and no arguments are passed to this function. The return type of this function is void and hence no return.

Updated on: 25-Oct-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements