Hi Everyone,

Very beginner with C++. Reading some books and trying to write some codes as much as I can.

I'm trying to understand what voiding a function does. Now in the book and the searches I did says it doesn't return anything. But looking at the code below, doesn't it actually returns the "first" and "second" string when we call the function???

Sorry I'm really at the first step. Hope the question makes sense.

Thank you everyone in advance for answering.

#include <iostream>
using namespace std;

void PrintName(string first, string last)
{
string fullname = first + “ “ + last;
cout << fullname << endl;
}

int main()
{
PrintName(“Thomas”, “Jefferson”);
return 0;
}

returns means returning back to calling method.
cout is not returning,it is just an output stream meaning printing data in output stream.

#include <iostream>
using namespace std;

int PrintName(string first, string last)
{
string fullname = first + “ “ + last;
cout << fullname << endl;
return 5;
}

int main()
{
int value;
value=PrintName(“Thomas”, “Jefferson”);
cout<<value;
return 0;
}

Take a look on above ex,i just modified return type of method.

Thanks for the reply IIM,

So with the example you gave, I think I got it.
You can confirm,

You called it as an integer but because you put everything else as a string, it void it and just output as 5
then you called it again as integar but called it as stign and it returned it as Thomas Jefferson?

Did I explain it and got it right? Should I quit trying to program?

A function that prints something and then returns an integer is a bad example to try and understand the difference between a void function and a function that returns a value. Let's see if this clears things up, and please read the comments to understand what's going on here:

#include <iostream>

using std::cout;
using std::endl;

//Note that, by declaring a function as float, we specify that it
//RETURNS a float as its output. It doesn't matter what type of argument we
//feed it.

//This particular function just cuts an integer in half and returns it as
//a float.
float half(int input)
{
    return input / 2.0;
}

//This function down here is void. It doesn't return a value, it can't be used
//as a value in an expression. You just call it, and it does something.
void printstuff(const char* stuff)
{
    cout << stuff << endl;
    return;
}

int main()
{
    //Because half() returns a value, we can actually USE it in an expression,
    //just like we'd use any other variable or value.

    //Because half() returns a float, myresult must also be a float (or at least
    //something that can be converted from a float)
    float myresult = half(3);
    cout << myresult << endl;

    //The printstuff() function, having nothing to return, just gets called on
    //a line all by itself.
    printstuff("Hello, void!");

    return 0;
}
Comments
Going the extra mile to be helpful!

Just to add, it is not considered good programming to write void functions, even functions that are not expected to return anything must return a successful code (0) or failure (1) which should be verified for behavior. 2 things that people often do not pay attention to are 1) checking successful execution of functions with the use of return values & 2) checking for and correction of memory leaks.

Thank you everyone for the replies.

Tumlee;

Those two examples really cleared it out for me. Thank you very much.

Mav3rick;

Believe it or not the books I'm reading says that it is good to use void functions. LOL. But what you say makes sense too.

Thank you again to everyone.

void permit a function without return type....

void is the return type. You just can't do much with it.

Just to add, it is not considered good programming to write void functions

That's not true at all. Please don't conflate your own personal preferences with generally accepted good practice. Status codes are certainly a good thing (assuming you've rejected other error handling strategies), but only when their use is appropriate. Simply returning a status code for the sake of not returning void is the opposite of a good practice.

Edited 3 Years Ago by deceptikon

This article has been dead for over six months. Start a new discussion instead.