tags:

views:

549

answers:

7

Hi friends

I'm new to programming. Can I have a function with Zero arguments and no return type if the answer is yes then where to use these kinds of functions, and also my doubt is what does void function returns a null value or Zero value I am confused. Thanks in advance.

+1  A: 

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.

動靜能量
+2  A: 

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:

VB:

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

C#:

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

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

ArsenMkrt
+5  A: 

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).

David Rodríguez - dribeas
+1  A: 

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.

Nick D
+2  A: 

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.

Michael Borgwardt
The example code I think is not quite appropriate. A member function is never a function with no arguments, there is always an implicit 'this' pointer/reference being passed around. Anyway +1
David Rodríguez - dribeas
A: 

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.

RBarryYoung