-2

What determines whether a function is void-type vs non-void type? Is it the mere presence of the keyword return in the body of the function? Resources typically say that non-void types return a value. Why and when should I return a value?

For instance, prefer the code below (code 2), I didn't need to "return" anything; yet the program worked perfectly.

Compare code 2 to code 1 where I returned a value. Ultimately, it achieved the same purpose. Expanding on the question further, what makes the return type for greet() in code 3 void?

So I ask again: What determines whether a function is void-type vs non-void type? Is it the mere presence of the keyword return in the body of the function?

code1:

#include <iostream>
#include <string>
using namespace std;


int dispNumber(int n1) {
    return n1;
}

int main() {
    int n1 = 22;
    cout << dispNumber(n1);
    return 0;
}

code 2:

#include <iostream>
#include <string>
using namespace std;


void dispNumber(int n1) {
    cout << n1;
}

int main() {
    int n1 = 22;
    dispNumber(n1);
    return 0;
}

code 3

#include <iostream>
using namespace std;

    void greet(string n1) {
    cout<<"My name is " << n1;
}

int main() {

    string n1 = "Fred";
    greet(n1);


    return 0;
}
Andy
  • 12,859
  • 5
  • 41
  • 56
Jeff Kyei
  • 33
  • 4
  • 2
    You've answered your own question, haven't you? "dispNumber()" in Code 2 is an excellent example of a "void function". "dispNumber()" in Code 1 is poorly named - because it doesn't "display" anything. It doesn't do anything, either. In c89 and later, if non-void "Flowing off the end of a function is equivalent to a return with no expression. In either case, the return value is undefined.": https://stackoverflow.com/a/10079180/421195 – paulsm4 Feb 13 '21 at 02:33
  • Umm the `void` keyword makes it a void return type. – paddy Feb 13 '21 at 02:38
  • @JaMiTgot it backwards. Corrected it – Jeff Kyei Feb 13 '21 at 02:45
  • Look at the declared return type for `dispNumber` in code 2. Do you see anything there that might indicate that the function is a "void-type" hence does not need a `return` statement? *Hint: the return type is the part that reads `void`.* – JaMiT Feb 13 '21 at 02:46
  • @JaMiT made edits to my question. Please have a look and clarify. Thanks – Jeff Kyei Feb 13 '21 at 02:50
  • @paulsm4 made edits to my question. Please have a look and clarify. Thanks – Jeff Kyei Feb 13 '21 at 02:51
  • @JeffKyei You added another example, but that does not address my previous comment. I asked if you see anything -- perhaps the word `void` -- in the declared return type of your "void-type" function that might indicate that it is "void-type". Do you? Do you see the keyword `void` anywhere in the functions you call "void-type"? *For example, is the keyword `void` present in the declaration `void dispNumber(int n1)`?* Do you see it anywhere in the functions you call "non-void type"? Is there a pattern emerging? – JaMiT Feb 13 '21 at 03:08

1 Answers1

2

Why and when should I return a value?

A function that has some resulting value that, then the function should typically return the output to the caller. Pretty much all functions that you may be familiar with from Mathematics are such functions.

Imagine for example a function that adds two input integers together. Such function has a result: The sum of the inputs. The function should return that value to the caller.

What determines whether a function is void-type vs non-void type?

A function type is a non-void type. The only void type is void which is not a function type.

what makes the return type for greet() in code 3 void?

The very first part of the function declaration is the return type:

void greet(string n1) {
^^^^

Because the return type of the function was declared to be void, the return type is void.

eerorika
  • 232,697
  • 12
  • 197
  • 326