6

I understand that void returns no values. So how does it work in conjuncture to a function?

My understanding is that the purpose of a function is to return a piece of information after doing something with it.

so why would I want to return no value, and how would this be beneficiary?

Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
Ozymandias
  • 156
  • 2
  • 5
  • 18
  • 2
    Because you _don't_ always want to return a value. Sometime you do, other times, it would serve no purpose. Refer to Pascal's procedures and functions (procedures don't return anything) – enhzflep Nov 14 '16 at 06:28
  • 1
    A C/C++ function returning void is equivalent to a "procedure call" in other programming languages. – selbie Nov 14 '16 at 06:28
  • Actually, if you disassembly (x86/x64) a void function, I think it returns an unused value (correct me if I am wrong). As mentioned by @enhzflep, returning a value is sometimes not needed. Thus, you can also pass a parameter by reference or by pointer and achieve the same resut. – Papipone Nov 14 '16 at 06:29
  • 3
    Unfortunately not. A so called 'function' can do something on parameters (in provided by reference), have side effects (on system and peripherals). So return value is not its only purpose. – Djee Nov 14 '16 at 06:29
  • 1
    "My understanding is that the purpose of a function is to return a piece of information after doing something with it." Not necessarily. People create functions for the purpose of modularity as well. Also, you need not always return something as a result, you can store the result in one of the passed arguments as well if they are `out` params. – Arunmu Nov 14 '16 at 06:30
  • 1
    I think it's worth it to precise that constraining every function to return a value and to have no side effect is the definition of the functional paradigm, which is, well, __a__ paradigm, among others. – Right leg Nov 14 '16 at 06:34
  • Does this answer your question? [If void() does not return a value, why do we use it?](https://stackoverflow.com/questions/11560068/if-void-does-not-return-a-value-why-do-we-use-it) – user438383 Apr 10 '23 at 20:36

3 Answers3

5

My understanding is that the purpose of a function is to return a piece of information after doing something with it.

In some (most of the) programming languages, functions have side effects also. Purpose of some functions is limited only to side effects and return value is not necessary. Such functions have void return type.

Some examples of side effects may be:

  1. Update a global
  2. File operation, logging etc where user doesn't want to know the status of operation
  3. Freeing resources
Gyapti Jain
  • 4,056
  • 20
  • 40
  • 2
    *Every* function in *every* language implementation *has* some side-effect, at least heating the processor and/or losing time. What matters are the *useful* side-effects. – Basile Starynkevitch Nov 14 '16 at 06:36
  • Thanks @BasileStarynkevitch for adding another dimension. Can't agree any less with you. – Gyapti Jain Nov 14 '16 at 06:40
0

C++ Programming Language Stroustrup 4th Edition book

When declaring a function, you must specify the type of the value returned. Logically, you would expect to be able to indicate that a function didn’t return a value by omitting the return type. However, that would make a mess of the grammar (§iso.A). Consequently, void is used as a ‘‘pseudo return type’’ to indicate that a function doesn’t return a value.

Edit:

When you don't expect something in return to the calling function, we use void function.

If void() does not return a value, why do we use it?

Community
  • 1
  • 1
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
  • 3
    The question is not "Why do we precise `void` when a function does not return a value?", but "Why would we want a function not to return a value?". – Right leg Nov 14 '16 at 06:41
  • `the purpose of a function is to return a piece of information` - I don't agree with that statement much. That contradicts the concepts of `pass by reference`. – Saurav Sahu Nov 14 '16 at 06:48
  • 1
    That's not the point of my comment. Besides, this point is discussed in the comments of the question, and the other answer addresses this. – Right leg Nov 14 '16 at 06:50
0

It can be pretty useful for modularizing output. I'll provide you with an example:

#include <iostream>
#include <string>

using namespace std;

void displayMessage(string fName, string mName, string lName, string id);

int main() {
    string student[4] = { "Mike", "L.", "Jason", "c23459i" };
    displayMessage(student[0], student[1], student[2], student[3]);

    return 0;
}

void displayMessage(string fName, string mName, string lName, string id)
{
    double PI = 3.14159265359;
    cout << "Student " << " information:"
        << "\nFirst name: " << fName
        << "\nMiddle Initial: " << mName
        << "\nFirst name: " << lName
        << "\nID: " << id
        << "\nThe Circumference of a circle with the radius of 2: " << (2*PI*2);
}

You can use void functions IF you don't need to return a value. If you plan to do calculations and return a value such as an int, use a function declaration with the return type of int.

Meho buh
  • 25
  • 4
  • When I asked this question I took my 1st CS class. I now work fulltime as a software engineer. I'm unsure how people are still finding this question no less posting answers haha – Ozymandias Nov 08 '22 at 09:05