# confused on what a void function is

This topic is 4278 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

so as the title explains, am am confused as to what a void function/operator/command is, and what it does, can anyone help clarify it for me?

##### Share on other sites
A "void function" is a function that does not actually return a value: a void return type means "nothing is returned".

##### Share on other sites
So what purpose does it serve, and how would i use it in a program?

##### Share on other sites
Quote:
 Original post by zero_hourSo what purpose does it serve, and how would i use it in a program?

You use it when you care about a function for its side effects, and not for an eventual return value

#include <iostream>void print_rectangle(unsigned int width, unsigned int height){   for(int h=0; h<height; ++h)   {      for(int w=0; w<width; ++w)      {         std::cout << '*';      }      std::cout << std::endl;   }}int main(){   print_rectangle(10,5);   print_rectangle(5,10);   print_rectangle(10,10);   print_rectangle(3,3);   // and so on and so forth.   for(int x=1;x<10;++x)      print_rectangle(x,x);}

##### Share on other sites
So... The function itself doesn't return anything, but can be used kind of like a shortcut, instead of having to write the code out for each one of those examples?

##### Share on other sites
In Fruny's example you can see how it makes the code more clear and maintainable.

If you wrote out the function separately for each print_rectangle(), not only would people not know what you were doing, but if you made a change to how you "print a rectangle" then you have to change each part.

With a function, you change it and it automatically "updates" for whenever you call it.

Although you shouldn't worry about it, calling a function has some "overhead" but you can put the keyword "inline" before the function to "suggest" to the compiler that it should be copied (as if you wrote it out everytime yourself!).

##### Share on other sites
Quote:
 Original post by zero_hourSo... The function itself doesn't return anything, but can be used kind of like a shortcut, instead of having to write the code out for each one of those examples?

That's essentially the purpose of functions, whether they return anything or not. They provide a means for you to organize your code in such a way that you do not need to repeat the same sequence of statements again and again. Put your statements in a function and you can call that instead.

##### Share on other sites
Quote:
 so as the title explains, am am confused as to what a void function/operator/command is, and what it does, can anyone help clarify it for me?

A void function is a function which does something without returning a value, as Fruny already mentioned. You could use it like shown in the example below:
class myStringClass{ char *string; void print(); //this is a void function};void myStringClass::print(){ printf( "%s", string ); //display the string on the screen.}//exit and return nothing!

Note that myStringClass::print() has no other purpose than displaying the string of its class on the screen. As you can see, there is no need for any value to be returned (yes, you could return a value whether the printf() function succeeded or not, but this isnt neccessary in general).

There are also other possible uses of the void-statement:

1) Functions which have no parameters at all, like myStringClass::print() in the example above, are sometimes defined like this:

myStringClass::print( void );

The void parameter simply means: there is no parameter at all. It it not neccessary to fill an empty parameter list with a void-statement, but some people do it.

2) If a pointers type is void*, the pointer can point to ANY datatype you like. A void pointer can be converted into any other type of data pointer. The following syntax is valid:
int i;char c;myStringClass string;void *PointerOfDoom;		//void pointerPointerOfDoom = &i;		//void pointer points to an integerPointerOfDoom = &c;		//same void pointer points to a charPointerOfDoom = &string;	//same void pointer points to a class

Although it is sometimes useful to store/copy/move pointers without regard to the type it references, be aware that such operations can become VERY UNSAVE AND UNPREDICTABLE! For this reason, just dont use void pointers.

To sum it up, here is an excerpt from the MSDN Library:
Quote:
 void declaratorWhen used as a function return type, the void keyword specifies that the function does not return a value. When used for a function's parameter list, void specifies that the function takes no parameters. When used in the declaration of a pointer, void specifies that the pointer is "universal."

regards,
Phex

[EDIT: err... I was a bit slow, I started writing this when there was only 1 answer.]

##### Share on other sites
OK, So instead of a function taking an input, running it through its... function, and spitting out a new value, the void function serves primarily just to display lines of code that would be universal to many different inputs?

In addition, what are some other types of functions, besides the void function?

##### Share on other sites
Quote:
 Original post by zero_hourOK, So instead of a function taking an input, running it through its... function, and spitting out a new value, the void function serves primarily just to display lines of code that would be universal to many different inputs?In addition, what are some other types of functions, besides the void function?

You may better take a tutorial or book first. that explained in more detail.

##### Share on other sites
Quote:
Original post by savedbygamesandcodes
Quote:
 Original post by zero_hourOK, So instead of a function taking an input, running it through its... function, and spitting out a new value, the void function serves primarily just to display lines of code that would be universal to many different inputs?In addition, what are some other types of functions, besides the void function?

You may better take a tutorial or book first. that explained in more detail.

I have a book, and I dont get it, thats why i am asking here

##### Share on other sites
Quote:
Original post by zero_hour
Quote:
Original post by savedbygamesandcodes
Quote:
 Original post by zero_hourOK, So instead of a function taking an input, running it through its... function, and spitting out a new value, the void function serves primarily just to display lines of code that would be universal to many different inputs?In addition, what are some other types of functions, besides the void function?

You may better take a tutorial or book first. that explained in more detail.

I have a book, and I dont get it, thats why i am asking here

Sorry... which book you have? is it good for you?

##### Share on other sites
Im using the one by michael dawson, and so far its been great, it goes into a good amount of detail, but the only problem is that if you don't get something, you can really ask the book a direct question.

##### Share on other sites
A function may have a return value or may not.

Here are some examples:

this function here is very mathematical, it equivalent to a f(x) = x^2

float myfunction(float x){   return x * x;}//and you'd use it like thisfloat y = myfunction(10.0f);

Here's another one:

int counter = 0;void increasecounter(int value){   counter += value;   if(counter > 1000)counter = 0;}// and here is how you use itincreasecounter(2);

So the first case is simple. I guess that you have learned about functions in school, so a programmed function can have a very similar function. Consider them as "take in some value, do something with it, and return the result"

The other case is about that you don't want to write the same function to many places. The function doesn't return anything, so it uses "void" as return value. These functions could be considered as "changing some state of the program". They can return values too if there is a need.

"void" is the only type which doesn't require the function to return anything. The compiler will give an error if you try to return a value from a "void" function.

Sometimes you may notice that you are writing the same or a similar code to many places. So then it comes to the question "should I make a function out of this?" instead of writing it to million times. And usually the answer is yes. Programs have usually hundreds or thousands of functions in them.

##### Share on other sites
Quote:
 Original post by zero_hourOK, So instead of a function taking an input, running it through its... function, and spitting out a new value, the void function serves primarily just to display lines of code that would be universal to many different inputs?In addition, what are some other types of functions, besides the void function?

What you are describing as the "void function" doens't actually exist as a seperate type.

What you are talking about is when a function has a return value of "void" which means that it does not return anything. That's all. It simply means that the function does not return any value. That's all there is to it.

You can do anything inside a "void function" that you can do inside a normal function. There isn't any specific use of the type "void" except to say "I'm not returning anything."

Quote:
 ... primarily just to display lines of code that would be universal to many different inputs ...

This is what a function is for. Not any specific kind of function, but just a function. It's like using printf. If you want to print out the string "hello world", you could call printf, or you could write all the code necessary to find the video buffer for the window and then write the string, character by character into the video buffer.

A function is simply a series of instructions that you'll be using more than once, so it is convenient to organise them in a way which makes them easy to use.

##### Share on other sites
Quote:
Original post by zero_hour
Quote:
Original post by savedbygamesandcodes
Quote:
 Original post by zero_hourOK, So instead of a function taking an input, running it through its... function, and spitting out a new value, the void function serves primarily just to display lines of code that would be universal to many different inputs?In addition, what are some other types of functions, besides the void function?

You may better take a tutorial or book first. that explained in more detail.

I have a book, and I dont get it, thats why i am asking here

http://www.gamedev.net/reference/list.asp?categoryid=20#214

Check out that section, it contains numerous references to various resources related to C/C++ programming.