Void Function

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;
}
Try this:
1
2
3
4
5
6
7
int main()
{
    string result;
    result = PrintName("Thomas", "Jefferson");
    cout << "result = " << result << endl;
    return 0;
}

Well, actually, this doesn't compile.

At line 4 it attempts to assign to the variable result whatever value is returned by the function PrintName().
But because the return type of the function is void the compiler knows that there is no returned value, that line of code cannot work.

doesn't it actually returns the "first" and "second" string when we call the function???


No. It prints the first and second string. It does not return them.

Printing outputs something to the screen.
Returning gives information back to the calling code.


Return values are used for when a function has output (not screen output, but computation/logic/code output). Example, let's say you have a function that cubes a number:

1
2
3
4
int cubeit(int num)
{
  return num * num * num;
}


Here, the return value is 'num' cubed. Note it doesn't print anything to the screen... it's just "returning" the value.

The returned value can be used by the code that calls this function:
1
2
3
4
5
6
int main()
{
  int val = cubeit( 5 );  // assigns the returned value to 'val'

  cout << val;  // prints '125'
}



The type (in this case, int) determines what type of value the function returns. Here, we are return an int, but you can also return a string, or any struct, class, or any other variable/object type.

A void function indicates that the function does not return any value. And therefore it would be illegal to try to assign the return value to something:

1
2
3
4
5
6
7
void noreturn()
{
}

int main()
{
  int foo = noreturn();  // error, noreturn doesn't return anything -- it returns void 




@ Chevril:

Your example is confusing... I would expect a function named "PrintName" to print the name, not return it.
Last edited on
Thank you for the reply Chervil,

So from what I understand, it voids if we try to assign something to whatever is in the parentheses?

Should I give up on programming instead???
I didn't entirely get void at first either. The point of void is that it doesn't return anything. It's handy for me if I have a function that just prints out some text or something.
Sorry if my example was confusing. I tried to be brief, but was actually meaning the same function was used as in the previous post.

hckrwb wrote:
So from what I understand, it voids if we try to assign something to whatever is in the parentheses?
That sentence doesn't really mean much to me. You're using void as if it was a verb, an action. That's not what it means. It's just a word meaning "there's nothing there", its a noun like vacuum or "empty space".

Maybe if we think of various datatypes like integer or double as being musical notes, then void is like silence.

Sorry, I'm probably just piling on the confusion here. I'm pretty sure I should stop typing.
Topic archived. No new replies allowed.