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
14.1k9 gold badges73 silver badges85 bronze badges
asked Nov 14, 2016 at 6:26
7
  • 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) Commented Nov 14, 2016 at 6:28
  • 1
    A C/C++ function returning void is equivalent to a "procedure call" in other programming languages. Commented Nov 14, 2016 at 6:28
  • 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. Commented Nov 14, 2016 at 6: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. Commented Nov 14, 2016 at 6: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. Commented Nov 14, 2016 at 6:34

3 Answers 3

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
answered Nov 14, 2016 at 6:28
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Thanks @BasileStarynkevitch for adding another dimension. Can't agree any less with you.
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?

answered Nov 14, 2016 at 6:38

3 Comments

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?".
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.
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.
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.

answered Nov 7, 2022 at 8:58

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.