Void vs Int Functions
Solution 1
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
}
Solution 2
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.
Solution 3
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.
}
Solution 4
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
' :-)
Solution 5
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.
soniccool
Updated on March 22, 2020Comments
-
soniccool over 4 years
What would be the different between void and int functions? When do i use which? Would void be used when 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?
-
Nirav Zaveri almost 10 yearsMany a times I have seen developers write functions where they didn't have to return anything, but they chose to return int and a fixed value "return 0;". Why would they do that? Does it really help in the larger picture?
-
Adriano Repetti almost 10 years@NiravZaveri (late answer in case anyone else has same question). It helps in the "larger picture" only for coherence with other functions. If, for example, all your API functions return an integer as error code then returning 0 is acceptable (even if, so far, that functions doesn't return any error code). If you'll change implementation then you won't need to recompile all code that uses it and users won't feel "strange" because function has a "known" signature.
-
COOKIE almost 6 years@srikfreak are void functions incapable of returning things, or is it just a common practice to use void when there is nothing to be returned?