473,699 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is the purpose of using void keyword

1 New Member
What is the main purpose of using void keyword
Sep 25 '16 #1
2 1435
weaknessforcats
9,208 Recognized Expert Moderator Expert
In C you cannot have multiple functions with the same name. Like this:

Expand|Select|Wrap|Line Numbers
  1. int Area(Circle* x);
  2. int Area(Square* x);
  3. int Area(Triangle* x);
The workaround is to use a void pointer. The keyword void just means "no-type". The you supply a second argument to explain the void pointer:

Expand|Select|Wrap|Line Numbers
  1. int Area(void* x, int y);
May y=1 means the x argument is a Circle whereas y = 2 may mean the x argument is a Square.

This allows the function to typpecast the void* to the correct type and do the calculation.

This situation does not arise in C++ where function names are identitifed by their signature and not by their names.

The other place you see void is as a function return type:

Expand|Select|Wrap|Line Numbers
  1. void func(int arg);
This tells the compiler that no type is returned making this an error:

Expand|Select|Wrap|Line Numbers
  1. int val = func(10);
Without the void return, the function is assumed to return an int. Now the code passes the compile OK but crashes at run time when val is garbage.
Sep 25 '16 #2
donbock
2,426 Recognized Expert Top Contributor
A void* pointer can be assigned a value that points to any data type. You should not assign a function pointer value to it.

This function prototype declares a function that takes no arguments. You will get a compiler error if you pass any arguments to it.
int func(void);

Notice that this declaration is different than the previous case.
int func();
In this case, there is no function prototype. The compiler will allow any number of arguments of any type.

You can cast a statement to void to instruct the compiler that you are intentionally discarding the value of the statement. You get the same effect whether you cast to void or not. I suppose the point of being explicitly intentional is to make your intentions clear to the next person to read your code; doing so may also prevent some compiler warnings. Some examples of this usage:
(void) printf("%d\n", var);
(void)0;

That last one is an idiom that shows up from time to time when you want a statement that does nothing. You almost always want your statements to do something, so this idiom is only seen in very uncommon special situations.
Sep 28 '16 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

23
22189
by: Ian Tuomi | last post by:
Hello, I was wondering, does it make any difference if you write void foo(int x) { /* insert code here */ } or foo(int x)
3
2941
by: Bob | last post by:
C# newbie here.... studying about delegates and now events. I just don't get the purpose of declaring events using the event keyword. What does this do for me that can't be done using only delegates?
3
4350
by: Scott | last post by:
Can anyone please give me a broef explanation of what the void keyword is for? What does it do? Do I need to use it? Thanks, Scott
4
2018
by: NetMasker | last post by:
How can I reference a form (from withing a module for example) and make it visible without using the keyword "Me" ? I tried with the following code but nothing happened: Public myform As Form1 myform = New Form1 myform.Show() Of course my application defines a form called Form1.
6
3678
by: semkaa | last post by:
Can you explain why using ref keyword for passing parameters works slower that passing parameters by values itself. I wrote 2 examples to test it: //using ref static void Main(string args) { List<TimeSpantimes = new List<TimeSpan>(); DateTime start; DateTime end; for (int j = 0; j < 1000; j++)
1
1373
by: SMH | last post by:
Hi All, I am currently making the switch from VB to C#. I understand that the VOID keyword allows creation of a function which doesnt require any parameters and doesn't return a value. Is this then, the equivalentof a SUB in VB? Thank you.
3
2626
by: ANURAGBAJPAI | last post by:
Please tell me what is the purpose of void in function main.
4
14319
by: ANURAGBAJPAI | last post by:
This is a question in my text book, and the answer is- The keyword void in a function declaration has a significant purpose.When the return type of a function is void , that means the function doesn't return anything.Thus we can say that the purpose of specifying the return type void is that it doesn't return anything. Please explain the answer.
0
8630
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9055
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8944
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8899
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3075
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2364
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2016
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.