Why use void with a function? [duplicate]
Asked Answered
W

3

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?

Winifred answered 14/11, 2016 at 6:26 Comment(7)
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)Duodenal
A C/C++ function returning void is equivalent to a "procedure call" in other programming languages.Burletta
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.Davison
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.Algol
"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.Akeylah
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.Baten
Does this answer your question? If void() does not return a value, why do we use it?Veronaveronese
F
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
Felicidadfelicie answered 14/11, 2016 at 6:28 Comment(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.Quintie
Thanks @BasileStarynkevitch for adding another dimension. Can't agree any less with you.Felicidadfelicie
C
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?

Calypso answered 14/11, 2016 at 6:38 Comment(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?".Baten
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.Calypso
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.Baten
P
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.

Parasol answered 7/11, 2022 at 8:58 Comment(1)
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 hahaWinifred

© 2022 - 2024 — McMap. All rights reserved.