473,320 Members | 1,876 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,320 software developers and data experts.

void function

is it mandatory to use void function?and why should we use it
Oct 10 '06 #1
5 29412
Not sure exactly what you're referring to, the question is a little vague, but if you are referencing something like the following:

Expand|Select|Wrap|Line Numbers
  1.  
  2. void someFunction(void)
  3.  
  4.  
The first void is the return type of the function, i.e. what will be returned when the function is called. Personally, I don't use void too often, I prefer to use int for error-checking. Void simply indicates that the function does not return a value.

There are cases, however, when you do need to return a value, such as the following:

Expand|Select|Wrap|Line Numbers
  1.  
  2. int addNumbers(int i, int j, int k)
  3. {
  4.      return (i + j + k);
  5. }
  6.  
  7.  
Useless function, yes, but hopefully it shows why you would use return types on your functions. In this case, when you used addNumbers(1, 2, 3) it would return 6, which could be used for later calculations.

Hope this helps.
Oct 10 '06 #2
No. In fact, for a main function, it is not recommended because it uses excess memory.

void is a variable type just like int, char, double, float, and bool. However, unlike the other variables, void is usually used to hold an unknown (or unwanted) value.

Rule of thumb for using void: If your function returns a value, you must not use void.


Acceptable:
int main()
{
return 1; // use int because returning an integer
}

void main()
{
return; // use void because not returning a value
}

void add( int a, int b) {return;};
add(a, b) // this add does not return anything

Unacceptable:
void main()
{
return 1; // Will generate "Looking for variable type void not found" error.
}

void add( int a, int b) {return;};
c = add( a, b) // Will generate "Looking for variable type void not found" error.

- Miles
Oct 10 '06 #3
vninja
40
Hey,
here's my 2 cents.
nothing is manditory and yet everything is. your question is a little vague but if we extend it to a more complex functions void functions become a little more practicle to use.
ex
simple:
void intro(void)
{
cout<< "Welcome to my program";
}
//this will make int main() func easier to read and fix if you have a program
//not doing what it should

more complex
void distance(string /*in*/ num, double /*out*/ &thrw)
{
cout << "Please enter the distance for throw # "
<< num << ": ";
cin >> thrw;
cin.ignore(100, '\n');
}
//This void function is great for a get value and it could be rewritten with an int
// function that would return the var thrw. but if we need more variables to change
// then we would need more returned values

//also the ampersand(&) tells the program to replace whatever was in value thrw
//with whatever is put in (kinda like a value returning func's return command)
Oct 10 '06 #4
vninja
40
and if we go furthur
void distance(string /*in*/ num, double /*out*/ &thrwx, double /*out*/ &thrwy)
{
cout << "Please enter the x distance for throw # "
<< num << ": ";
cin >> thrw;
cout << "Please enter the y distance for throw # "
<< num << ": ";
cin >> thrwy;
cin.ignore(100, '\n');
}

//this way you can get two values from one func than 1 value that you could get
//from a value returning func()

now we could also change the void to an int like so:
int distance(string /*in*/ num, double /*out*/ &thrwx)
{
cout << "Please enter the x distance for throw # "
<< num << ": ";
cin >> thrw;
cout << "Please enter the y distance for throw # "
<< num << ": ";
cin >> thrwy;
cin.ignore(100, '\n');
return thrwy;
}
//and this would get the same effect as the code before
the only difference would be in the main where for the 1st (void) func the call would be
distance(num, thrwx, thrwy);
and 2nd(value returning func) call would be
thrwy = distance(num, thrwx);


so what it all boils down to is preference
if you prefer to use value returning func its ok
if you prefer to use void func its ok too

hope this helps you n srry for splittin it into 2 posts
V
Oct 10 '06 #5
In this execrise, you create a simple payroll program using a main() fuction and four void function.
A. Fingure 10.25 shows the partially completed IPO charts for the payroll program.complete the Input and out put colums of the IPO charts for the four void functions named getInput( ),CalcFedTaxes( ),CalNetpay( ),and displayInfo( ).The FWT(Federal Withholding Tax) rate is 20% of the weekly salary, and the
FICA (Federal Insurance contributions Act) rate is 8% of the weekly salary.

figure 10-25

Input processing output

name processing item:none name
weekly salary 1.getlnput(name,weeklysalary) FWI
FWT rate (.2) 2.CalcFedTaxes(weekly salary) FICA
FICA rate (.08) FWT rate,FICA rate,FWT,FICA) weekly net pay
3.calcNetpay(weelky salary,FWT,FICA,weekly net pay)
4.displayinfo(name,FWT,FICA,weekly net pay)


getInput ( ) fuction

Input processing output
processing item: none
Algorithm:
1.enter the name and weekly salary

calcFedTaxes ( )fuction

Input Processing output
Processing item:none
Algorithm:
1.calculate the FWT by multiplying the weekly salary by the FWT rate
2. calculate the FICA by multiplying the weekly salary by the FICA rate


calcNetpay( )function

Input processing output
Processing item:none
Algorithm:
1.calculate the weekly net pay by subtracting the FWT and FICA from
the weelky salary


displayInfo( )function

input Processing output
Processing item:none
Algorithm:
1.display the name,FWT,FICA and weekly net pay

B.Display the taxes and net pay with two decimal places.
Nov 14 '06 #6

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

Similar topics

3
by: Jochen Zeischka | last post by:
I'm puzzled. When compiling this: template<class ValRes, class Val1, class Val2> Veld<ValRes>& mult(Veld<ValRes>& res, const Veld<Val1>& v1, const Veld<Val2>& v2) { // something return res; }...
6
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
16
by: | last post by:
I saw this code in C++ but when tried to C causes an error: ------------- void function(int &a) { a = 5; } ------------- with this, passed in "function" the "a" pointer instead of "a" value, BUT...
3
by: Don | last post by:
I am a beginner in C. A student I should say. Is it possible to retrieve a value from a void function? A have a void function that reads a string or number from a file. I call that function from...
4
by: Roman Mashak | last post by:
Hello, All! Assume I wrote function like: /* authenticate client */ int cli_auth(char *user_id, char *user_pass) { MYSQL conn; if ( db_init(&conn) == 0 ) { if ( db_search(&conn, user_id,...
5
by: Giannis Papadopoulos | last post by:
I have the following code #include <stdio.h> void a(void) { printf("a called.\n"); } int b(void) { printf("b called.\n");
8
by: Thomas Barth | last post by:
Hi could someone explain to me why the compiler outputs a warning that the control reaches the end of a non void function? int main(void) { ... pthread_t thrCallOp, thrHangUp;...
2
by: lostmountainman | last post by:
I gotta void function that is taking two string files from the main function and inputting it into the main function....the line of code that keeps showing up as errors is: scanner1(name1, name2) ...
6
by: Daniel Rudy | last post by:
Hello Everyone, How do I return early from a void function? The following code does not compile. strata:/home/dr2867/c/modules 735 $$$ ->./compile unpifi.c unpifi gcc -W -Wall -Wshadow...
3
by: socondc22 | last post by:
Im stuck at this point. The way i was trying to do this was running if statement around my void function and then a else if statement around a different void function but it didnt work... right now...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

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.