Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Void vs Int Functions

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?

like image 727
soniccool Avatar asked Jul 19 '26 08:07

soniccool


2 Answers

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
}
like image 151
srikanta Avatar answered Jul 21 '26 21:07

srikanta


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.

like image 36
Beta Avatar answered Jul 21 '26 22:07

Beta