473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

what is the purpose of using void keyword

What is the main purpose of using void keyword
Sep 25 '16 #1
2 1398
weaknessforcats
9,208 Expert Mod 8TB
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 Expert 2GB
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
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
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...
3
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
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...
6
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) {...
1
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...
3
by: ANURAGBAJPAI | last post by:
Please tell me what is the purpose of void in function main.
4
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.