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

<div class="execute"></div><div class="code-mirror  language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">// GOLANG PROGRAM TO CREATE A FUNCTION WITHOUT</span>
<span class="token comment">// ARGUMENT AND WITHOUT A RETURN VALUE</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>

<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>

<span class="token comment">// Starting the function main()</span>
<span class="token comment">// GO program execution starts with the function main()</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

   <span class="token comment">// Function Calling</span>
   <span class="token comment">// Function Definition</span>
   <span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
func <span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token comment">// declare and initialize the variables</span>
   <span class="token keyword">var</span> a <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token number">30</span>
   <span class="token keyword">var</span> b <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token number">60</span>
   <span class="token keyword">var</span> c <span class="token keyword">int</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Golang program to create a function without argument and without a return value"</span><span class="token punctuation">)</span>
	
   <span class="token comment">// Print the result</span>
   c <span class="token operator">=</span> a <span class="token operator">+</span> b
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"Addition : %d"</span><span class="token punctuation">,</span>c<span class="token punctuation">)</span>	
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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

<div class="execute"></div><div class="code-mirror  language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">// GOLANG PROGRAM TO CREATE A FUNCTION WITHOUT</span>
<span class="token comment">// ARGUMENT AND WITHOUT A RETURN VALUE</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>

<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>

<span class="token comment">// function prototype</span>
<span class="token comment">// GO program execution starts with the function main()</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

   <span class="token comment">// function call</span>
   <span class="token function">area</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
func <span class="token function">area</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Golang program to create a function without argument and without a return value"</span><span class="token punctuation">)</span>
   <span class="token comment">// declare and initialize the variables</span>
   <span class="token keyword">var</span> square_area <span class="token keyword">int</span>
   <span class="token keyword">var</span> square_side <span class="token keyword">int</span>
   square_side <span class="token operator">=</span> <span class="token number">7</span>
   square_area <span class="token operator">=</span> square_side <span class="token operator">*</span> square_side
	
   <span class="token comment">// print the result</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"Area of the Square is %d"</span><span class="token punctuation">,</span>square_area<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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: 2022-10-25T11:41:08+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements