Jump to content
  • Advertisement
Sign in to follow this  
zero_hour

confused on what a void function is

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

If you intended to correct an error in the post then please contact us.

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 this post


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

Share this post


Link to post
Share on other sites
Quote:
Original post by zero_hour
So 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by zero_hour
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?


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 this post


Link to post
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 pointer

PointerOfDoom = &i; //void pointer points to an integer
PointerOfDoom = &c; //same void pointer points to a char
PointerOfDoom = &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 declarator
When 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by zero_hour
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?

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!