Tutorial :Can I have a function with zero arguments and no return typelang?


Can I have a function with zero arguments and no return type?

If the answer is yes, then where do you use these kinds of functions?

Also what does a void function return, a null value or zero value? I am confused.


Can I have a function with Zero arguments and no return type

Yes, though many languages require you to declare a return type of void or something similar in that case.

where to use these kinds of functions,

Unless you define "function" strictly to mean "has not side effects", they can be useful useful for side effects, e.g.

public void invalidate()  {      this.invalid = true;  }  

This would be part of an object that can be invalidated (and stays invalidated permannently once this has happened).

Of course, in pure functional languages that don't allow side effects at all, this scenario is not possible - but in such a language, functions with no return value would make no sense in general.

and also my doubt is what does void function returns a null value or Zero value I am confused.

It returns nothing. If you attempt to use it anywhere it would be expected to produce a return type (e.g. as parameter to another function) you'll get a compiler error.


It makes sense only in languages that allow for side effects, and may use it when you only need a side effect. Consider, for example, a long running operation on a console application. So that the user knows that you are not blocked, you may want to print a dot each so often on the screen.

void printdot() {     printf( "." );  }  

The function does not require any arguments nor returns any value, and still makes sense in itself. You call the function to achieve the side effect (changing the screen).


That would be helpful if you could specify what language you learn.

Anyway, in the common languages that I know the answer is yes. Sometimes there is a need to have functions to do stuff for you, no arguments taking and no return values:


Sub DoAction() 'Takes 0 parameters      'Do actions      'Doesn't return a value  End Sub  


void DoAction() //Takes 0 parameters  {      //Do actions      //Doesn't return a value  }  


Don't confuse it with mathematical functions. Functions (or procedures or subroutines) are pieces of code blocks. They help the programmer to write structured code. If you want a result from a routine then you can have one. That's all.


Yes, for example, if a function is just to print out "hello world", or some calculation, then it can take no parameter, and doesn't need to return any value at all.

For example, you can have a PHP function

function printPageFooter() {      echo "<p>Thanks for coming.  Come back soon!</p>\n";  }  

that actually prints out the webpage's footer in HTML. This function takes no parameter and print something out, and doesn't return anything.

If it is a language such as C, then when you say

void PrintPageFooter() {    /* do something */  }  

you are saying this function doesn't return anything by using the "void" in front of the function definition.

I remember in Pascal, when I learned it long time ago, all functions have to return something. If it doesn't, then it is a "procedure" instead.


yea of course you can, for example it can print some calculation results to screen ;)


In programming theory and in most computer languages, the answer is no; a Function must have a return value.

However some languages, such as the C family (C, C++, Java, Javascript, C#, etc.), use a degenerate concept of a function that considers subroutines to be functions with no return value. So in these languages the answer is "yes" and you would use such functions (which are actually subroutines) to generate operational side effects driven by the environmental or contextual state.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »