Void vs Int Functions
What would be the different between void and int functions? When do i use which? Would void be used whe开发者_JAVA百科n just printing out a message or a variable value? like cout << value;
Or would it just be text?
And is int used when you actually do calculations within it?
void
is used when you are not required to return anything from the function to the caller of the function.
for eg.
void no_return_fn()
{
cout<< "This function will not return anything" << endl;
return; // returns nothing or void
}
int
is used when you have to return an integer value from the function to the caller of the function
for eg.
int return_sum_of_integers_fn(int a, int b)
{
cout<< "This function returns an integer" << endl;
return (a + b); // returns an integer
}
Do you want the function to return anything? If not, then it should be void
. If you want it to return an int
, then it should be int
. If you want it to return something else, then it should have some other return type.
Some prefer using functions that return int to indicate some errors or special cases. Consider this code:
int print (int* ptr)
{
if (ptr == NULL)
{
return -1; // Error code.
}
std::cout << *ptr;
return 1; // Success code.
}
when you use void, it means you don't want anything returned from the function. while an int return may be a result of calculation in the function, or indicate the status when it returning, such as an error number or something else that can tell you what has happened when the function executing.
Like you heard above, void
doesn't return value, so you use it when you don't need to do this.
For example, you can use 'void' not only to print text, but mainly to modificate a parameters (change something you already have), so you don't need to return a value.
Let's say you want to find int c
, which is sum two (integer) numbers, a
and b
(so a+b=c). You can write a function which adds these numbers and assign it's value to c
int c;
int sum (int a, int b)
{
return a+b;
}
c = sum(a,b);
While using void
, you will just modificate c
, so instead of writing c = function(arguments)
, you will have function(c)
which modifies c
, for example:
int c;
void sum(int a, int b, int c)
{
c = a+b;
}
sum(a,b,c);
After the last line, the 'c
' you have is equal the sum of 'a
' and 'b
' :-)
You return void
to indicate that nothing is returned.
An int return may or may not indicate that calculations have been performed (e.g. could just be a return code).
Another difference is that void function do not require to have a return inside it: This 2 snippets of code are valid and do not generate a compiler warning: Snippet 1
void msg1()
{
cout<< "This is a message." << endl;
return; // returns nothing or void
}
Snippet 2
void msg2()
{
cout<< "This is a message." << endl;
}
Both have the same behavior and no warning is displayed.
If you declare a function non void and you do not return a value your compiler is likely to display a WARNING like " warning: no return statement in function returning non-void".
It's depends on modifier parameters sent to compiler if this error is displayed or not.
This code is likey to display a compiler warning since no return :
int test(){
printf("test");
}
Actually,It is necessary if you are compiling your code for a hosted system, such as PC, Linux, Mac, Android. Then main must return int. If you are compiling code for a freestanding system, such as embedded microcontrollers, or if you are making an OS, then the return type of main can be anything.
Though no error will occur if you use int or void but its not compliance according to standard C.
精彩评论